Results 1 to 4 of 4
  1. #1
    gmaster is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    May 2014
    Posts
    32

    Picking a value from a VBA table

    Hi guys, i have this VBA script that creates a table with some Counting:
    strSQL = "SELECT Sum(C1 + C2 + C3) AS Tot"
    strSQL = strSQL & "FROM (SELECT Count(Tbl1.Campo_1) AS C1, Count(Tbl1.Campo_2) AS C2, Count(Tbl1.Campo_3) AS C3 FROM Tbl1)"



    Now, my question is: how can i use VBA for picking the value in "Tot" and use it later?
    As in: Total = 10 + Tot
    Can i just write like this? Or there's another syntax?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Dont use vba.
    Create a query with this sql, THEN if you need to fill the result into a text box (or msgbox)

    txtBox = Dlookup("[tot]","qsMyTotalQry")

    (ok, the dlookup is vba, but dont use vba for queries, make a query"

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    First, terminology.

    Hi guys, i have this VBA script that creates a table with some Counting:
    strSQL = "SELECT Sum(C1 + C2 + C3) AS Tot"
    strSQL = strSQL & "FROM (SELECT Count(Tbl1.Campo_1) AS C1, Count(Tbl1.Campo_2) AS C2, Count(Tbl1.Campo_3) AS C3 FROM Tbl1)"
    This does not "create a table". It is a text string that would create a query... if there was more commands.

    This is what the code might look like:
    Code:
        Dim rst As DAO.Recordset
        Dim strSQL As String
        Dim tot As Long
    
        tot = 0
    
        strSQL = "SELECT Sum(C1 + C2 + C3) AS Tot"
        strSQL = strSQL & " FROM (SELECT Count(Tbl1.Campo_1) AS C1, Count(Tbl1.Campo_2) AS C2, Count(Tbl1.Campo_3) AS C3 FROM Tbl1)"
    
        'open recordset
        Set rst = CurrentDb.OpenRecordset(strSQL)
        'check to see if there are records returned
        If Not rst.BOF Then
            tot = rs.Fields(0)
            '       or
            '        tot = rs!tot
        End If
        
        'other code
        
        
        'clean up
    rst.Close
    Set rst = Nothing
    
    End Sub

  4. #4
    gmaster is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    May 2014
    Posts
    32
    Thanks a lot guys! With your help i made it work!!

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

Similar Threads

  1. Recordsetclone NOT picking up records from linked table
    By CementCarver in forum Programming
    Replies: 1
    Last Post: 10-15-2013, 10:01 AM
  2. Replies: 1
    Last Post: 06-25-2012, 06:48 AM
  3. Queries only Picking up some information
    By Lois in forum Queries
    Replies: 5
    Last Post: 11-22-2011, 05:08 AM
  4. Picking up Date on Import
    By DonL in forum Import/Export Data
    Replies: 4
    Last Post: 08-10-2011, 07:06 AM
  5. Me not picking up new columns in table
    By asterismw in forum Programming
    Replies: 3
    Last Post: 03-11-2011, 02:00 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