Search Button Not Finding Record and populating the text boxes on form.

Ecc_9:10

New member
Local time
Today, 07:02
Joined
May 19, 2024
Messages
17
Hi all,

I need a form to populate once an option is chosen from a listbox.

What I having working:
User Types into a textbox
The list box filters based on value in the text box
The user chooses item from the list
The text box now matches this

Where I am experiencing problems:
The user clicks on the search button and the form populates with the correct record based on the value in the textbox.

Code I have:

Private Sub cmd_UpdateSearch_Click()
Dim rst As DAO.Recordset
Dim strsql As String
strsql = "Select * from Mastertbl where FName = " & Search.Value & ""
Set rst = CurrentDb.OpenRecordset(strsql)
If rst.EOF Then
MsgBox " Not in database"
FirstName.Value = Nothing
LastName.Value = Nothing
Number.Value = Nothing
Email.Value = Nothing
Address.Value = Nothing
Else
FirstName.Value = rs.field("First Name")
LastName.Value = rs.field("Last Name")
Number.Value = rs.field("Phone Number")
Email.Value =rs.field("Email Address")
Address.Value = rs.field("User Address")
End If
rst.Close
Set rst = Nothing
End Sub

I keep getting a run time error Runtime Error 3075: Syntax Error (missing operator) in query expression '[FName] = Sarah'

When I click debug is shows me this line:

Set rst = CurrentDb.OpenRecordset(strsql)

What is wrong here - please explain in lay man terms if possible.

Thank you in advance.
 
If FName is a Text field, you'll need to enclose the value in quotes. For example:

Code:
strsql = "select * from mastertbl where fname='" & me.search & "'"
 
You put a quote after your control name but not before. :(
Always debug.print your sql string until you get it correct, then either comment out that line or remove it.

Please put your code within code tags, make it easier to read and those single quotes stand out better.
 
Last edited:
So how is your code going to work when you have more than one person with the same FName?
Looks like you will only ever see the first one.
 
Why are you even using a recordset, could be asked as well? Are the forms not bound?
 
I am not allowed to post links to promote their work
Really?, I do it all the time, not paticularly from them, just a quick search on YouTube.
 
Last edited:
So how is your code going to work when you have more than one person with the same FName?
Looks like you will only ever see the first one.
I don't have more than one person with the same name - the FName was just an example its not the actual data that is in that field. There can be no duplicates in the field i am using.
 
Why are you even using a recordset, could be asked as well? Are the forms not bound?
The form is bound.

I think I just got frustrated because I couldn't figure out the code for the search button (on click) after the text box matches the users choice from the list box

If any one could help me with this I would very grateful.
 
I could not do better than Allen Browne

Please tell us the actual data, not make up examples, poor examples at that. :(

Social Security Number would have been a better example?
 

Users who are viewing this thread

Back
Top Bottom