Results 1 to 4 of 4

Thread: SQL and VBA

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    SQL and VBA

    Can someone show me how I can use a SELECT statement from within VBA (in Access). All I want to do is retrieve all of the names from a table and add a specific field from each returned rows into a ComboBox.

    e.g.

    Perform the query...

    SELECT empid, name
    FROM employees;

    ... and then add all 'name' fields from the returned rows to a ComboBox.

    Thanks.
    Using Visual Studio .NET 2005

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: SQL and VBA

    Here's a sample piece of code that populates Combo1 when the form is opened.

    VB Code:
    1. Private Sub Form_Open(Cancel As Integer)
    2.     With Me.Combo1
    3.         .ColumnCount = 2
    4.         .Width = 2440
    5.         .ColumnWidths = "0;2440"
    6.         .BoundColumn = 1
    7.         .RowSource = "SELECT BRAND_ID,BRAND_DESC FROM PH_BRAND"
    8.     End With
    9. End Sub

    I've used a select from my table, just change the SQL statement to point to your table
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: SQL and VBA

    Ah, that seems simple enough. Thanks.

    Could you also tell me how to do the same, but receive the data as an array of strings? I can't see how I would adapt your example to make it do this...
    Using Visual Studio .NET 2005

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: SQL and VBA

    Use ADO and a recordset object.

    Search the forums or tutorials online there are loads of examples.


    Using the array to fill the combobox might be a problem though in access requires special coding.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

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



Click Here to Expand Forum to Full Width