Consulting

Results 1 to 10 of 10

Thread: SQL Statement Help

  1. #1

    SQL Statement Help

    I want to use Excel SQL to query a table in access (I have googled and learned I can do this via DAO or ADO). So this is my SQL statement:
    [vba]
    "Select possibleFields from FieldsinDatabase;"
    [/vba]

    This would then return the possible fields that could be in the main database. Ideally, I would like to store all of these fields in one variable if possible, and append them to the end of a seperate SQL string that I would be running. So maybe something like:
    [vba]
    fieldNames = "Select possibleFields from FieldsinDatabase;"
    [/vba]

    then append fieldNames to a seperate SQL statement so it would be:
    [vba]
    "Select ReferreeNum, RefereeFName, RefereeLName, RefereeAddress, RefereePhone," & fieldNames & " from MainDatabase
    [/vba]

    How can I accomplish this via VBA?

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    It's not necessary to make an inventory of all fields in a database to retrieve them.
    You can use a wildcard character *

    If you want all the fields in table 'tblCustomerInfo' in database 'Test1.mdb' you can use:



    [vba]
    Sub snb()


    ' set reference to Microsoft ActiveX Data Objects 2.0 Library
    With New Recordset
    .Open "SELECT * FROM tblCustomerInfo", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\Test1.mdb"
    Sheets(1).Cells(1).CopyFromRecordset .DataSource
    End With
    End Sub

    [/vba]



  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Surely, you know the names of the field in your database?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    I agree the set-up is atrocious! However, there are about 500 databases that were set up this way, so I was wanting to do something similar to what I described above, and use an Array to cycle through each database, and of course the possibleFields in FieldsIndatabase would vary depending on what database I was connecting to, so that was the best solution I could come up with.

    The Referee fields are the same for every database...that's why I was thinking to store all of the possibleFields in a variable and append the variable to my "Standard SQL Statement (Referee Fields)

    Is it possible to do what I am wanting, or can someone come up with a better way to go about this? Please let me know if you need more information, or if I wasn't descriptive enough.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    To geneate an inventory of all fields:

    [VBA]
    Sub snb()
    With New Recordset
    .Open "SELECT * FROM TABEL1", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\Access\fiets.mdb"
    For Each fld In .Fields
    c01 = c01 & "_" & fld.Name
    Next
    End With

    Cells(1).Resize(, UBound(Split(c01, "_"))) = Split(Mid(c01, 2), "_")
    End Sub
    [/VBA]

    to retrieve a certain selection of fields:
    [VBA]
    Sub snb()
    With New Recordset
    .Open "SELECT " & Join(Array("address", "location", "email"), ",") & " FROM TABEL1", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\Access\fiets.mdb"
    Sheets(1).Cells(20, 1).CopyFromRecordset .DataSource
    End With
    End Sub
    [/VBA]

  6. #6
    Quote Originally Posted by snb
    To geneate an inventory of all fields:

    [vba]
    Sub snb()
    With New Recordset
    .Open "SELECT " & Join(Array("address", "location", "email"), ",") & " FROM TABEL1", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\Access\fiets.mdb"
    Sheets(1).Cells(20, 1).CopyFromRecordset .DataSource
    End With
    End Sub
    [/vba]
    This code looks like what I would want to accomplish. I am trying to break it down piece by piece to make sure I have a full understanding before I execute the code. I want to take the fieldNames that are housed in table1 and pull the data from those fields from table 2.

  7. #7
    Quote Originally Posted by snb
    To geneate an inventory of all fields:

    [vba]
    Sub snb()
    With New Recordset
    .Open "SELECT " & Join(Array("address", "location", "email"), ",") & " FROM TABEL1", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\Access\fiets.mdb"
    Sheets(1).Cells(20, 1).CopyFromRecordset .DataSource
    End With
    End Sub
    [/vba]
    This code looks like what I would want to accomplish. I am trying to break it down piece by piece to make sure I have a full understanding before I execute the code. I want to take the fieldNames that are housed in table1 and pull the data from those fields from table 2.

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Do table1 and table2 contain the same fields ?

  9. #9
    In a sense yes. table1 contains the name of the fields. table2 contains the same fields but with data. So for example table1 would have fields, id, fieldNames. And of course id would be an autonumber and fieldNames would be PhoneNum, Address, EmployeeNumber etc. Then table2 would be EmployeeNumber, PhoneNumber, and Address.

    Does that make sense? I uploded a sample database that contains a few junk entries to see if that will help shed light on it.
    Attached Files Attached Files

  10. #10
    BUMP --- Any idea's guys?

Posting Permissions

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