put text box value into query

fanfan2

Registered User.
Local time
Today, 00:56
Joined
Feb 26, 2007
Messages
61
Hi, I'm new to VBA. I have 2 text boxes in a form, I'd like to get the user inputs from those 2 text boxes and then use it in a query, then use a command button to run the query. How can I do that?

In my query SQL view, I used forms!txtbox1 and forms!txtbox2 to get the values, but it didn't seem to work.

Do I have to use VBA to do it? How can I run a sql query in VBA?

Any help would be appreciated.
 
You need to specify which form the textboxes are located on:

[Forms]![YOURFORMNAMEHERE]![txtbox1]

[Forms]![YOURFORMNAMEHERE]![txtbox2]
 
I did specify, but I'm sorry I forgot to put it in my question above. I did use [forms]![formName]![textbox1].

Thanks for pointing that out.
 
Thanks. I used those references in an expression. It didn't seem to have got the value from the txtbox. Should I write VBA code for the text boxes s.a. "after update"?
 
Are you sure the form is open when you run the query? (Don't laugh...seen stranger things happen). Do you get an error message or a prompt when you run the query?
How can I run a sql query in VBA?

Sorry, missed that part of the question before. It's pretty staright formward to run sql in vba.

first declare a string variable to hold the sql

Dim strSQL as String

then write your SQL

strSQL= "Select ...whatever...From ...TableOrQueryname... Where [FieldName]=" & Me.TextBox1 & " And [FieldName2] =" & Me.Textbox2 & ";"

then run it using

Docmd.RunSQL strSQL
 
Last edited:

Users who are viewing this thread

Back
Top Bottom