Results 1 to 3 of 3

Thread: Calling Access Queries

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Posts
    46
    Is there a way in Vb to call a query from an Access database?

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Absolutely, take your pick at what object library you're going to use(ADO, RDO, DAO) and go ahead. I would suggest using ADO, since according to Microsoft, ADO is going to be the ultimate data access library (until they'll have something better).
    Add a reference to Microsoft ActiveX Data Objects and do something like this (I assume that you query is meant to bring a recordset back i.e. it is a Select statement):
    Code:
    Dim cn As New ADODB.Connection
    Dim cm As New ADODB.Command
    Dim rs As New ADODB.Recordset
    
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDatabase.mdb"
    
    With cm
        Set .ActiveConnection = cn
        .CommandType = adCmdStoredProc
        .CommandText = "YourQueryName"
        'If you have parameter(s) then you have to do something like this
        'In this example I'm passing an Integer type parameter with value of 5
        .Parameters.Append .CreateParameter("ParamName", adInteger, adParamInput, , 5)
        Set rs = cm.Execute
    End With
    
    If Not rs.EOF Then
        'Do your stuff with recordset here
    End If
    
    
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

    Regards,

  3. #3
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    You can also do it by opening an instance of Access, if Serge hasn't given you an approach you like...

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