Consulting

Results 1 to 3 of 3

Thread: Outlook : Combobox to SQL query

  1. #1

    Outlook : Combobox to SQL query

    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

  2. #2

    Passing to SQL String

    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

  3. #3
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •