Results 1 to 3 of 3

Thread: Executing Access queries from VB code

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 1999
    Location
    Thessalon, ON, Canada
    Posts
    2

    Post

    I was wondering if anybody could tell me how to execute queries that are stored in an Access database from my VB application. Any help would be greatly appreciated.

    Thanks

  2. #2
    New Member
    Join Date
    Feb 1999
    Location
    Palmdale, CA US
    Posts
    14

    Post

    There's lots of ways - here's any easy one that uses ADO and no code. The key here is that VB treats Access stored queries like another table.

    In access save the query with a name that make sense. In VB create an ADO data control, in the properties dialog of the data control, on the RecordSource tab, select adCmdTable for the Command Type, and then select your query from the drop down box titled Table or Stored Procedure.


  3. #3
    Lively Member
    Join Date
    Feb 1999
    Location
    Leicester, UK
    Posts
    123

    Post

    One method that I use quite often is

    If the query has a parameter:

    Dim strParam As String
    Dim strSQL As String
    Dim qdf As QueryDef
    Dim rs as Recordset

    strParam = "PARAMETERS [Enter Parameter:] LONG; "
    strSQL = dbData.QueryDefs("qryNameOfQuery").SQL
    Set qdf = dbData.CreateQueryDef("", strParam & strSQL)
    qdf("Enter Parameter:") = lngParamValue
    Set rs = qdf.OpenRecordset(dbOpenDynaset)

    If the query does not have any parameters:

    Dim strSQL As String
    Dim qdf As QueryDef
    Dim rs as Recordset

    strSQL = dbData.QueryDefs("qryNameOfQuery").SQL
    Set qdf = dbData.CreateQueryDef("", strParam & strSQL)
    Set rs = qdf.OpenRecordset(dbOpenDynaset)

    In both examples dbData is the opened database object

    This assumes you are using DAO. I have not used this with ADO but if you can get the SQL text then you can just plug the string into the rs.Open statement.

    Hope this helps.

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