Share via

VBA sum access column with variable result

Hussein Ali 1 Reputation point
Dec 7, 2022, 3:50 PM

i have column name (a1) on table (num_emp) ~ AccessDb
i want sum and get variable result

Private Sub ???5_Click()
Dim dbs As Database, rst As Recordset
Set dbs = OpenDatabase("currentdb")
Set rst = dbs.OpenRecordset("SELECT sum(a1) from num_emp")
MsgBox rst

dbs.Close
End Sub

268218-screenshot-2022-12-07-184825.png

0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Oskar Shon 866 Reputation points
    Dec 7, 2022, 5:08 PM

    Your commend should be like sting
    If you Use "SELECT sum(a1) from num_emp" then a1 is a part of string, not connection with cell value.

    You should do your string from parts like: "SELECT sum(" & a1.value & ") from num_emp"
    Of course sum with one cell can't work properly.

    Then i guess:
    "SELECT field from num_emp where field = " & sum(a1:a4)
    Check if that make sense on your site. Better to check your select on DB first, before you translate to VBA sting.

    Regards


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.