Results 1 to 6 of 6
  1. #1
    Budatlitho is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2016
    Posts
    36

    Syntax to use variable in DSum()

    Hello:

    I have this function in Access VB:
    Code:
    Public Function JumbulaTotals()
    Dim db As Database
    Dim rs As Recordset
    Dim eid As Long
    Dim strSql As String
    Dim amountdue As Long
    Dim amountpaid As Long
    eid = 1049523
    
    amountdue = DSum("[Order amount]", "Jumbula", _
                "[MemberID_1] =1049523")
                Debug.Print "Order amount = " & amountdue
    It executes OK, but when replacing 1049523 as the criteria value with the variable eid,
    Code:
                "[MemberID_1] = eid")
    I get
    run-time error '2471'"


    The expression you entered as a query parameter produced this error:
    'eid'

    Obviously, it's a syntax error but I've tried many additions of brackets and punctuation with no success.
    Groan!

  2. #2
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Try:
    Code:
    amountdue = DSum("[Order amount]", "Jumbula", _
                "[MemberID_1] =" & eid)
                Debug.Print "Order amount= " & amountdue
    Assumes eid is numeric, if text you wrap it in single quotes (or double double-quotes):
    Code:
    amountdue = DSum("[Order amount]", "Jumbula", _
                "[MemberID_1] =""" & eid & """")
                Debug.Print "Order amount= " & amountdue
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    You need to learn how to concatenate variables.
    Use a variable to store the criteria. Then you can debug.print it until you get it correct, then use that variable in the function.
    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

  4. #4
    Budatlitho is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2016
    Posts
    36
    Eureka! Thank you.

  5. #5
    Budatlitho is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2016
    Posts
    36
    Thanks. Will do. Any reference material online?

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    My first point of call is always Google.

    https://www.google.com/search?q=conc...e-gws-wiz-serp
    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

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

Similar Threads

  1. DSUM Syntax with dates
    By LonghronJ in forum Modules
    Replies: 3
    Last Post: 07-19-2018, 11:50 AM
  2. Variable concatenation woes on DSum
    By newbieX in forum Programming
    Replies: 2
    Last Post: 03-26-2015, 12:43 PM
  3. Dsum, Syntax error
    By mike02 in forum Queries
    Replies: 5
    Last Post: 05-24-2013, 09:52 AM
  4. DSum syntax for report
    By dwilson in forum Access
    Replies: 2
    Last Post: 09-08-2011, 07:31 PM
  5. DSum Syntax Help
    By desk4tbc in forum Programming
    Replies: 2
    Last Post: 07-01-2011, 02:31 PM

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