Results 1 to 7 of 7
  1. #1
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34

    Run a query from VBA

    Hi,



    I have a query in which I want to run from VBA.

    The code in the query is:

    SELECT DISTINCT tblScN_View.SN, tblSN_View.CPref Left(CPref ,PosOfFirstDigit("ABC123")-1) AS Expr1
    FROM tblScN_View
    WHERE (((tblSN_View.CPref)=Left([Forms]![Main]![txtCaseN],PosOfFirstDigit([Forms]![Main]![txtCaseN])-1)));

    Therefore when a button is click in the form, I want it to bring up the results of this query.

    Is this is possible....

    Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Save the SQL as a query then run
    docmd.openquery "qsMyQry"

  3. #3
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34
    sorry could you explain in a bit more detail, I am new to VBA Access

    thank you

  4. #4
    NoellaG's Avatar
    NoellaG is offline VIP
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Hi,

    the easiest way: save the query as qryMyQuery
    and then use the code:
    Docmd.Openquery "qryMyQuery"

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    tblSN_View.CPref Left(CPref ,PosOfFirstDigit("ABC123")-1) AS Expr1

    There is a space between the f and L
    that will give an error.

    Does the query run properly from the query window?

  6. #6
    NoellaG's Avatar
    NoellaG is offline VIP
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Not much to do than: start a new query in design mode, Create -> query design. Build your query in the design grid, or go to the SQL view and paste your SQL statement there.
    Then create the button, and in the event properties On Click -> Event Procedure and there you type the code line

  7. #7
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34
    Hi Yes the query works fine in a query window, however just struggling how to get VBA to run this query for me

    thanks

Please reply to this thread with any new information or opinions.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums