Results 1 to 5 of 5
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    241

    Using a numeric value result from a query in VBA Code


    Hi to All,

    I run a SELECT query that counts a number of records and it gives the right answer.
    When I tried to use it in a VBA Code it gives the following error:

    Error 2342: A RunSQL action requires an argument consisting of an SQL statement

    Below is my code:
    Code:
    Dim db As DAO.Database
            Set db = CurrentDb
            Dim intSQL As String
            
            intSQL = "SELECT Count(fkBookId) AS CountOffkBookId FROM tblBorrowedBooks WHERE fkBookId=" & Forms!frmBooks!pkBookId & " AND DateReturned Is Null;"
            DoCmd.RunSQL intSQL
    Running the query alone gives me number 2 which is the correct number of records.

    Any help is highly appretiated

    Khalil

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Use Dcount()
    Runsql is for action queries.
    https://learn.microsoft.com/en-us/of...s.docmd.runsql

    Why does no one ever lookup the syntax?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    1. theres never a reason to invoke DAO inside an Access db. It's built in.
    2. you dont need the DB variable. Just run the query


    Code:
    Dim db As DAO.Database
            Set db = CurrentDb

    3. sql is a string: Dim sSQL As String
    not an integer


    all you need to run a query:


    sSQL = "SELECT Count(fkBookId) AS CountOffkBookId FROM tblBorrowedBooks WHERE fkBookId=" & Forms!frmBooks!pkBookId & " AND DateReturned Is Null;"

    DoCmd.RunSQL sSQL

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You can't RUN as select query in that way, you only RUN an action query.
    UPDATE, INSERT, DELETE are action queries that are RUN.

    As previously suggested just use a DCount()

    You could open a recordset and count the records, but that is nowhere near as efficient as the DCount.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    241
    Hi,
    Thank you all for your collaboration.
    Solved using DCount().

    Khalil

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

Similar Threads

  1. Numeric Result for Combinations
    By Micron in forum Code Repository
    Replies: 4
    Last Post: 01-22-2019, 08:12 PM
  2. Replies: 1
    Last Post: 09-14-2017, 10:19 AM
  3. creation alpha numeric code
    By Jen0dorf in forum Access
    Replies: 1
    Last Post: 05-09-2016, 11:52 AM
  4. Setting Code to Simple Numeric Age
    By wes9659 in forum Forms
    Replies: 6
    Last Post: 08-14-2014, 01:22 PM
  5. Can't get one date result - trying this code
    By Ruegen in forum Programming
    Replies: 9
    Last Post: 11-05-2013, 03:43 PM

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