Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query based on user input

Status
Not open for further replies.

Vard

Technical User
Jan 14, 2005
10
US
I need to do something very simple: on a form I have a text box and a button, and I want to get the value the user enters in text box to search in a table (or query) and output only those records that contain such a value. I know it should be pretty simple, but I got confused with the VBA syntax and I couldn’t figure it out.

Can anyone post a block of code in VBA that in SELECT statement in WHERE criteria uses the text box value to compare?
SELECT *
FROM tableName
WHERE fieldName like '% (here the value from the text box) %'

Any help would be much appreciated.
 
SELECT * FROM tablename
WHERE fieldname = [Forms]![formname]![textboxname];
 
Thanks a lot for your quick reply. That should be the solution, but I am afraid I could not run the statement. Am I doing something wrong in DoCmd.RunSQL(queryStatement)?



----------------------------------
Private Sub ButtonSearch_Click()
On Error GoTo Err_ButtonSearch_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, 2, , acMenuVer70


Dim queryStatement
queryStatement = "SELECT * FROM query_Image WHERE ImageName = [Forms]![frm_Image]![TextBoxImageName]"


DoCmd.RunSQL(queryStatement)

Exit_ButtonSearch_Click:
Exit Sub

Err_ButtonSearch_Click:
MsgBox Err.Description
Resume Exit_ButtonSearch_Click

End Sub
 

How about...
Code:
queryStatement = "SELECT * FROM query_Image WHERE ImageName = [COLOR=red]'" & [/color][Forms]![frm_Image]![TextBoxImageName][COLOR=red] & '[/color]"



Randy
 
Mine was for a numeric. Randy700 is for alpha.
 
The RunSQL method allows ACTION queries only.
You may try something like this:
Code:
Me.Filter = "ImageName Like '%" & [Forms]![frm_Image]![TextBoxImageName] & "%'"
Me.FilterOn = True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top