I am using below code in access vba. this code give me the input box to entered the value which updates the value inputed in access table. is it possible to give combobox instate of input box. I can select desise input from that combobox. pls let me know if it is possible.
Dim qry As String
qry = "UPDATE Data_Table SET Data_Table.Workgroup = [Workgroup Name]WHERE (((Data_Table.Workgroup) Is Null));"
DoCmd.RunSQL qry
The input box that pops up when you execute your code is "created" by MS Access on the fly, because of the [Workgroup Name] value in the query.
It's not possible to make Access pop up a ComboBox directly from the query instead.
(how should Access know which options to put into the ComboBox?)
But of course you can create a ComboBox yourself (somewhere on a form) and insert the value from that ComboBox into the query.
For example, you could put your code from the question into a function which gets the workgroup name passed as a parameter, like this:
Public Function RunQuery(ByVal WorkgroupName As String)
Dim qry As String
qry = "UPDATE Data_Table SET Data_Table.Workgroup = '" & WorkgroupName & "' WHERE (((Data_Table.Workgroup) Is Null));"
DoCmd.RunSQL qry
End Function
Then you create a ComboBox in a form (and populate it with options) and pass the value of the ComboBox to the RunQuery function:
Private Sub SomeComboBox_AfterUpdate()
If Nz(Me.SomeComboBox.Value) > "" Then
RunQuery Me.SomeComboBox.Value
End If
End Sub
Related
I have an MS Access list box that is being populated through DAO Recordset. When the data is displayed the values are not sorted. The code that I have is not working.
Dim sqlString As String
sqlString = "SELECT ReportName FROM MetadData.ReportDB_Test ORDER BY " & Me.ListReports.Value & " ASC;"
Me.ListReports.RowSource = sqlString
If you're just setting the rowsource of a listbox, there's no need to use VBA. Just set the rowsource property of the listbox to the SQL you need (you can even use Access's query builder).
I would like to be able to open a record (find record) on my form from a list box and combo box I have on the left side of my form. I want to use them as a means a navigation.
Ive Included a picture: My Form and navigation set up
First off, write a function that fetches the data that you want to populate into the fields on the right. Have it take the ID as a parameter, like:
public function FillWithData(recordId as int)
dim s as string
dim r as new adodb.recordset
s = "SELECT * FROM MyTable WHERE IdField =" & recordId & ";"
r.Open s, CurrentProject.Connection, adOpenDynamic, adLockReadOnly, dbSQLPassThrough
Exit Function
End Function
Secondly, add an On Click event to the list.
Private Sub myList_Click()
dim id as int
id = myList.Column(1) 'the number here is the index of the column that contains the record id in the RowSource of the List.
Call FillWithData(id)
Exit Sub
End Sub
I have a table and a Form in MS Access. Data is added through the Form. I want to be able to track user changes. I have a column User in my table where the user name will be added after update of the Form. For this purpose I have the following VBA code which will be fired After Update of the form, however it doesnt work and I dont know where the mistake is. Help will be appreciated. Thanks
Private Sub Form_AfterUpdate()
Dim UserLogin As String
Dim MyId As Integer
Dim strSQL As String
Dim rs As Recordset
UserLogin = Environ("Username")
MyId = Me!ID.Value
Set strSQL = "Update [MyTable] SET [MyTable]!User = '" & UserLogin & "' WHERE [MyTable]!ID = '" & MyId & "';"
'Set rs = dbs.OpenRecordset(strSQL)
DoCmd.RunSQL strSQL
You can track changes not only in particular form, but any change made in data will be registered, even if it was done directly on table. Just create Before Change data macro for your table ("Create Data Macro" menu in table design mode.
In Data Macro designer add the action SetField with parameters: Name = User ("User" is your field name for storing user name) and Value = GetUserName().
In any standard module create the function like this:
Public Function GetUserName() As String
GetUserName = Environ("Username")
End Function
After this any change in your table will update changed record by name of user, who did this change.
Please note, in case of split database this function should be available in front end and in back end databases if you want to edit data also in backend.
As of your code, check data type of ID field. If it's Number, remove single quotes. Also replace "!" by "." in SQL query text.
Just insert a textbox with controlsource as "User"
and then instead of after update use
Private Sub Form_BeforeUpdate()
me.user = Environ("Username")
end sub
no need for separate SQL operation
I'm a newcomer to Access trying to cobble things together from helpful information I've found here.
I have a form that needs to populate the fields based on a combo box selection in the form header. The form is based on an underlying query with the following criteria for field "StudID" [Forms]![frmStudConsentUpdate]![cmbStud] where cmbStud is my combo box. The combo box pulls in StudID, StudFN, StudLN with StudID as the bound columnn. The after update event requeries the form (Me.Requery). This works beautifully, but only if I first open the form in design view, open the Record Source, and save it. I don't make any changes at all, but once I've done this the form works. Otherwise, nothing happens when I select a student in the combo box. Any thoughts on what I need to do to make this work without having to re-save the underlying query?
This is old bug in MS Access, I have no idea why they still didn't fix it:
If underlying form's query has in criteria form's control and the form was filtered once (at start or manually/using VBA), it doesn't accept new values from form's control and uses old value.
Workaround: create public function, which returns control's value and use it in criteria instead of [Forms]![frmStudConsentUpdate]![cmbStud]. You will need to create function for each control or use this function:
Public Function GetControlValue(strFormName As String, strControlName As String, Optional strSubFormControlName As Variant, Optional varDefault As Variant) As Variant
' Returns : Variant, value of control in form/subform
' Comments:
' Params :
' strFormName
' strControlName
' strSubFormControlName
' varDefault - value returned if control is not accessible
'----------------------------------------------------------------------------
On Error GoTo ErrorHandler
If IsMissing(strSubFormControlName) Or Nz(strSubFormControlName, "") = "" Then
GetControlValue = Forms(strFormName).Controls(strControlName).Value
Else
GetControlValue = Forms(strFormName).Controls(strSubFormControlName).Form.Controls(strControlName).Value
End If
ExitHere:
On Error Resume Next
Exit Function
ErrorHandler:
If Not IsMissing(varDefault) Then
GetControlValue = varDefault
End If
Resume ExitHere
End Function
In criteria use function call GetControlValue("frmStudConsentUpdate","cmbStud") instead of [Forms]![frmStudConsentUpdate]![cmbStud]
In your afterupdate for your cmbStud combobox, create code that refreshes the recordsource to
me.recordsource = "SELECT * FROM {yourQueryName} WHERE StudID = '" & me!cmbStud & "'"
Using: MS-Access 2013; Windows 8.1 Professional
I am trying to update the displayed values in a ListBox in Access by setting the RowSource property in VBA to an SQL statement. However its not working. The listbox does not show the results of the SQL.
Here is my VBA code:
Private Sub cmbStudent_Change()
Dim s As Integer
s = cmbStudent & ""
' cmbTerm is a ListBox
cmbTerm.RowSourceType = "Table/Query"
cmbTerm.RowSource = "SELECT DISTINCT Terms.TermID, Terms.TermName " & _
" FROM Terms " & _
" ORDER BY Terms.TermCode;"
cmbTerm.Requery
End Sub
After cmbTerm.Requery, the listbox is still empty ie. its not drawing the values from the TERMS table.
Without using a RecordSet (ADODB.RecordSet) object is there any way to make this work?
Thank you in advance for any helpful inputs.
I have been having the same issue I had to fix it by putting this code at the end.
Me.Listbox.RowSourceType = "Value List"
Me.Listbox.RowSourceType = "Table/Query"
Note I had to also put it in a sub above the one that declared the RowSource.
so the RowSource is in a sub that is called by the main RowSource