Following the example here: https://support.microsoft.com/en-us/help/304302/how-to-build-a-dynamic-query-with-values-from-a-search-form-in-access I have created a search button that searches a table and seem to pull in the correct SQL statement. However, on this form I have several textboxes (UserID, FirstName, LastName, Department) which are tied to their respective columns in the database. How to I update these textboxes inside the form frmSearchUsers to reflect the results of the filtered/queried table?
I would have thought a Me.Requery would be sufficient, however the textboxes remain blank (save for txtSQL)
Private Sub cmdSearch_Click()
On Error Resume Next
Dim ctl As Control
Dim sSQL As String
Dim sWhereClause As String
'Initialize the Where Clause variable.
sWhereClause = " Where "
'Start the first part of the select statement.
sSQL = "select * from customers "
'Loop through each control on the form to get its value.
For Each ctl In Me.Controls
With ctl
'The only Control you are using is the text box.
'However, you can add as many types of controls as you want.
Select Case .ControlType
Case acTextBox
.SetFocus
'This is the function that actually builds
'the clause.
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & BuildCriteria(.Name, dbtext, .Text)
Else
sWhereClause = sWhereClause & " and " & BuildCriteria(.Name, dbtext, .Text)
End If
End Select
End With
Next ctl
'Set the forms recordsource equal to the new
'select statement.
Me.txtSQL = sSQL & sWhereClause
Me.RecordSource = sSQL & sWhereClause
Me.Requery
End Sub
there are 4 textboxes, UserID, FirstName, LastName, Department.
Let's say I knew that in table Customers there was a Jane Doe, but did not know her ID or department.
Typing Jane into the FirstName textbox and Doe into the LastName textbox, and hitting search seems to yield the appropriate SQL query (and have confirmed that this correctly filters in SQL view on the table): SELECT * FROM Customers Where FirstName="Jane" and LastName="Doe"
However the additional fields will not update - what am I doing wrong here? Is it because of how I have the control source for the textboxes I have tied to the table columns?