Results 1 to 3 of 3
  1. #1
    tonygg is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    115

    Using Select Sum .... queries

    Hi. I am trying to use a query in VBA to find out the number of records and the sum of the record values in a table.

    The SQL is:

    SELECT Sum(TONYWorldPayTransactions.[Merchant Commission Amount]) AS Expr1, Count(TONYWorldPayTransactions.[Merchant Commission Amount]) AS Expr2 FROM TONYWorldPayTransactions


    WHERE (((TONYWorldPayTransactions.[Batch Id])>=6 And (TONYWorldPayTransactions.[Batch Id])<=26) AND ((TONYWorldPayTransactions.WPBarclayFlag)='B') AND ((TONYWorldPayTransactions.[Merchant Commission Amount])<>0) AND ((TONYWorldPayTransactions.[Event Type])='SETTLED') AND ((TONYWorldPayTransactions.CountryofCard)='GB') AND ((TONYWorldPayTransactions.CardType)='VISA_CREDIT-SSL'));

    In native access query design it returns 2 values. The sum of the merchant commission amounts and the number of records. All good. My issue is I don't know how to retrieve/use these values in a vba sub routine. I just need to display the resulting 2 values

    I tried:

    Set rsOrders = Nothing

    SQLOrderString = "SELECT Sum(TONYWorldPayTransactions.[Merchant Commission Amount]) AS Expr1, Count(TONYWorldPayTransactions.[Merchant Commission Amount]) AS Expr2" & _
    " WHERE TONYWorldPayTransactions.[Batch Id] >= " & Me.PaymentProviderBatchNumber & " AND TONYWorldPayTransactions.[Batch Id] <= " & Me.PaymentProviderBatchTo & _
    " AND (TONYWorldPayTransactions.[WPBarclayFlag] = 'B' AND TONYWorldPayTransactions.[Merchant Commission Amount] <> 0 AND TONYWorldPayTransactions.[Event Type] = 'SETTLED' AND TONYWorldPayTransactions.[CountryofCard] = 'GB' AND TONYWorldPayTransactions.[CardType] = 'ECMC_CREDIT-SSL');"

    Set rsOrders = CurrentDb.OpenRecordset(SQLOrderString)

    Me.MCCreditAmount = rsOrders![Merchant Commission Amount]

    However this gets a compile error on the final line. I guess this is not the correct method.

    I would be very grateful if someone could help me get the VBA sorted out.

    Many thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The expression is not referencing the field in the query. Need to reference the alias field name.

    Me.MCCreditAmount = rsOrders!Expr1
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    tonygg is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    115
    Great Thankyou.

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

Similar Threads

  1. select top queries
    By sarsmelt in forum Queries
    Replies: 4
    Last Post: 11-17-2014, 02:49 PM
  2. User select queries
    By jcrump230 in forum Queries
    Replies: 15
    Last Post: 07-14-2014, 10:03 AM
  3. Replies: 5
    Last Post: 05-14-2014, 01:17 PM
  4. Queries to select MAX
    By johnseito in forum Queries
    Replies: 20
    Last Post: 09-15-2013, 07:13 AM
  5. Queries based on a Select Case
    By dccjr in forum Queries
    Replies: 3
    Last Post: 03-05-2013, 04:12 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