Make a simple select query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stephane
    New Member
    • Feb 2007
    • 35

    Make a simple select query

    how?
    i need get some data from table, at the action
    i try anything DAO and ADO, it doesn't works((

    for example, this code
    Code:
    Dim WorkBase as Database 
    Dim WorkRS1 as Recordset 
    Dim SQL as String 
    
    Set WorkBase = OpenDataBase(“voyages.mdb”) 
    
    SQL = “Select init from Managers WHERE contact=” & manager 
    Set WorkRS1 = WorkBase.OpenRecordset(SQL) 
    
    WorkRS1.Close 
    WorkBase.Close
    it returns error at the first line Dim WorkBase as Database
    "User-defined type not defined"

    what's wrong?
    help me pls
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Originally posted by stephane
    how?
    i need get some data from table, at the action
    i try anything DAO and ADO, it doesn't works((

    for example, this code
    Code:
    Dim WorkBase as Database 
    Dim WorkRS1 as Recordset 
    Dim SQL as String 
    
    Set WorkBase = OpenDataBase(“voyages.mdb”) 
    
    SQL = “Select init from Managers WHERE contact=” & manager 
    Set WorkRS1 = WorkBase.OpenRecordset(SQL) 
    
    WorkRS1.Close 
    WorkBase.Close
    it returns error at the first line Dim WorkBase as Database
    "User-defined type not defined"

    what's wrong?
    help me pls
    In the VBA Editor check Tools - References. Make sure there is a DAO library ticked on the list.

    Mary

    Comment

    • stephane
      New Member
      • Feb 2007
      • 35

      #3
      i turn on it,
      now, other error at line
      Set WorkRS1 = WorkBase.OpenRe cordset(SQL)
      "Runtime error 13, Type Mismatch"

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        If manager is a control on your form and a text data type, try ...

        Code:
        SQL = "Select init from Managers WHERE contact='" & manager & "'"
        Mary

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by stephane
          how?
          i need get some data from table, at the action
          i try anything DAO and ADO, it doesn't works((

          for example, this code
          Code:
          Dim WorkBase as Database 
          Dim WorkRS1 as Recordset 
          Dim SQL as String 
          
          Set WorkBase = OpenDataBase(“voyages.mdb”) 
          
          SQL = “Select init from Managers WHERE contact=” & manager 
          Set WorkRS1 = WorkBase.OpenRecordset(SQL) 
          
          WorkRS1.Close 
          WorkBase.Close
          it returns error at the first line Dim WorkBase as Database
          "User-defined type not defined"

          what's wrong?
          help me pls
          Here is workable code that Opens an External Database (C:\Test\Test.m db), creates a Recordset based on a simple SQL Statement, then loops through all Records in the Recordset outputting the First and Last Name Fields to the Immediate Window.
          Code:
          Dim MyDB As DAO.Database, MyWks As DAO.Workspace
          Dim MyRS As DAO.Recordset, strSQL As String
          
          strSQL = "SELECT * FROM tblEmployee;"
          
          Set MyWks = CreateWorkspace("", "admin", "", dbUseJet)
          Set MyDB = MyWks.OpenDatabase("C:\Test\Test.mdb", True)     'Open Exclusive
          Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
          
          Do While Not MyRS.EOF
            Debug.Print MyRS![FirstName] & " " & MyRS![LastName]
            MyRS.MoveNext
          Loop
          
          MyRS.Close
          MyDB.Close
          MyWks.Close

          Comment

          • stephane
            New Member
            • Feb 2007
            • 35

            #6
            Originally posted by ADezii
            Code:
            Dim MyDB As DAO.Database, MyWks As DAO.Workspace
            Dim MyRS As DAO.Recordset, strSQL As String
            
            strSQL = "SELECT * FROM tblEmployee;"
            
            Set MyWks = CreateWorkspace("", "admin", "", dbUseJet)
            Set MyDB = MyWks.OpenDatabase("C:\Test\Test.mdb", True)     'Open Exclusive
            Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
            
            Do While Not MyRS.EOF
              Debug.Print MyRS![FirstName] & " " & MyRS![LastName]
              MyRS.MoveNext
            Loop
            
            MyRS.Close
            MyDB.Close
            MyWks.Close
            thanks, this code is works.
            i think the reason of error was at the dbOpenDynaset key. i don't assign it to function befor

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by stephane
              thanks, this code is works.
              i think the reason of error was at the dbOpenDynaset key. i don't assign it to function befor
              The reason why it didn't work before was probably because this line was omitted from the code. You must create a Workspace Object to open a Database within:
              Code:
              Set MyWks = CreateWorkspace("", "admin", "", dbUseJet)

              Comment

              Working...