Hi
I was wondering if anyone could give me an example of how to pass the results of combox selection to the where clause of a sql query on the click of a command button.
Many Thanks
Hi
I was wondering if anyone could give me an example of how to pass the results of combox selection to the where clause of a sql query on the click of a command button.
Many Thanks
Good Afternoon.
The best way to compose a SQL Statement is to assign it to a string variable. This allows you to quickly and easily manipulate the statement.
For this example, the information we are requiring is in a table called "Companies" and has Fields "CompanyID", "Name", "Address1", "Address2", "City", "State", and "Postal"
I am using a ComboBox named cboCompany and it will return the selected company's name.
[VBA]
Public Sub Compose_Statement()
Dim sqlQuery as String
sqlQuery = "SELECT * FROM [Companies] WHERE Name = '" & cboCompany.Text & ';"
Debug.Print sqlStatemen
End Sub
[/VBA]
Hope this helps. If you need any more help, let me know.
Scott
Welcome to the forum- always good to see new members.
Scott's got it right, but there's something else you need to be aware of when you run hard-coded SQL statements in VBA.
Notice that he wrapped the combobox object with single quotes:
[vba]'" & cboCompany.Text & "';"[/vba]
This is important because it tells SQL that it's working with a String data type. If you're working with number data type, then you would not wrap the object with anything. If you were using Date data type you would use pound signs (#) instead of single quotes.
HTH
-Randy Shea
I'm a programmer, but I'm also pro-grammar!
If your issue is resolved, please use Thread Tools to mark your thread as Solved!
PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003