Results 1 to 2 of 2

Thread: Make an Query

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    Malang, Indonesia
    Posts
    15

    Make an Query

    how to make an query using Access database that opened using OPENDTABASE command in VB

  2. #2
    Helger
    Guest
    It's somewhat more complicated than just using opendatabase. I hope this is what you need. Just copy your SQL-text from ACCESS to strQuery.

    VB Code:
    1. Option Explicit
    2.  
    3. Function fGetRstDao(strQuery As String, Optional strPar1 As String, Optional strPar2 As String, Optional strPar3 As String) As DAO.Recordset
    4.  
    5. End Function
    6.  
    7. Dim wrkJet As DAO.Workspace
    8. Dim db As DAO.Database
    9. Dim qdf As DAO.QueryDef
    10. Dim rstDAO As DAO.Recordset
    11.  
    12. 'open workspace
    13. Set wrkJet = CreateWorkspace("", "Admin", "", dbUseJet)
    14. 'open DB
    15. Set db = wrkJet.OpenDatabase("c:\myDB.mdb")
    16. 'create an empty Query
    17. Set qdf = db.CreateQueryDef("")
    18.  
    19. 'define Query
    20. qdf.SQL = strQuery
    21. 'check how many parameters have been put to the query and assign the values:
    22. If Not IsMissing(strPar1) And qdf.Parameters.Count >= 1 Then
    23.     qdf.Parameters(0) = strPar1
    24.    
    25.     If Not IsMissing(strPar2) And qdf.Parameters.Count >= 2 Then
    26.         qdf.Parameters(1) = strPar2
    27.  
    28.         If Not IsMissing(strPar3) And qdf.Parameters.Count >= 3 Then
    29.             qdf.Parameters(2) = strPar3
    30.         End If
    31.     End If
    32. End If
    33. 'next line will open a recordset based on the above query and
    34. 'assign it to the function as its return value to get one that
    35. 'you can actually work with call the function as seen in
    36. 'the sub "Sub Test" below...
    37. Set fGetRstDao = qdf.OpenRecordset
    38. End Function
    39.  
    40. 'this function can be called like this:
    41. 'example with one parameter. no parameters or 2 or 3 are possible too
    42. 'for more change the above function accordingly
    43.  
    44. Sub Test()
    45. Dim strQuery As String
    46. Dim strParameter As String
    47. Dim rst As DAO.Recordset
    48.  
    49. strSQL = "SELECT * FROM tblMyTbl WHERE fld1 = ?;"
    50. strPar1 = "xyz" 'this will transport the value for the parameter set to '?' above. Can't do it directly!
    51.  
    52. Set rst = fGetRstDao(strQuery, strParameter)
    53.  
    54. End Sub

    regards,

    Helger

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