PDA

View Full Version : Outlook : Combobox to SQL query



ReportTeam
07-23-2008, 04:10 AM
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

Demosthine
09-27-2008, 06:42 PM
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.


Public Sub Compose_Statement()
     Dim sqlQuery as String

     sqlQuery = "SELECT * FROM [Companies] WHERE Name = '" & cboCompany.Text & ';"

     Debug.Print sqlStatemen
End Sub


Hope this helps. If you need any more help, let me know.
Scott

CreganTur
10-03-2008, 06:06 AM
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:
'" & cboCompany.Text & "';"
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:thumb