Results 1 to 4 of 4

Thread: [RESOLVED] Use DAO to query a database

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Resolved [RESOLVED] Use DAO to query a database

    Hi,

    I am using MS Access 2003. I am trying to query a table in another database, but can't seem to get it... This is my first time trying to run a query against any database other than the one I'm working in. I've read through some of the Object Viewer documentation, but I can't seem to get a good overview of how it should all come together.

    When I try to run the following code I receive an error:
    Operation is not supported for this type of object
    VB Code:
    1. Public Function QueryOtherDB()
    2.     Dim dbc As DAO.Workspace
    3.     Dim rsInfo As DAO.Recordset
    4.     Dim conn As DAO.Connection
    5.    
    6.     'This line throws the error
    7.     Set conn = OpenConnection("myConn", , False, "ODBC;DATABASE=Q:\path\myDB.mdb")
    8.     Set rsInfo = conn.OpenRecordset("SELECT * FROM tblErrorLog")
    9.    
    10.     While Not rsInfo.EOF
    11.         MsgBox rsInfo.Fields(0) & " " & rsInfo.Fields(1) & " " & rsInfo.Fields(2)
    12.     Wend
    13.    
    14.     rsInfo.Close
    15.     conn.Close
    16.     dbc.Close
    17. End Function

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Use DAO to query a database

    you can run the query with just sql

    SELECT * FROM TABLE IN 'Q:\path\myDB.mdb'
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Re: Use DAO to query a database

    Thanks. How would I fill the recordset with the results? I can't just say rsInfo.OpenRecordset("SELECT * FROM tblErrorLog in 'Q:\path\file.mdb'"). Is there another object I would use to fill it?

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Re: Use DAO to query a database

    Alright!
    I found out that I can use DoCmd.RunSQL and use the IN clause to specify the database file if I am running an action query. To run the select query I am using the following.

    VB Code:
    1. Public Function QueryOtherDB()
    2.     Dim dbc As Database
    3.     Dim rsInfo As Recordset
    4.    
    5.     Set dbc = DBEngine.Workspaces(0).OpenDatabase("q:\path\file.mdb")
    6.     Set rsInfo = dbc.OpenRecordset("SELECT * FROM tblErrorLog")
    7.    
    8.     Do Until rsInfo.EOF
    9.         MsgBox rsInfo.Fields(0) & " " & rsInfo.Fields(1) & " " & rsInfo.Fields(2)
    10.         rsInfo.MoveNext
    11.     Loop
    12.    
    13.     rsInfo.Close
    14.     dbc.Close
    15.    
    16. End Function

    Thanks again for your help. I have marked this thread resolved.

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