Home > Software engineering >  Database sum() values together if string matches in VBA
Database sum() values together if string matches in VBA

Time:07-05

Sorry for the basic question, I've spent the best part of a hour figuring this out and I'm at my limit. I'm trying to add all values together if a string matches a certain size, for context I'm adding wall tiles together for a client, here's an example table;

Code TileSize TileQty
BG3215 8mm 1
BG3545 10mm 3
BG3246 8mm 4
BG3745 8mm 1
BG3255 12mm 2

My VBA:

Dim db As DAO.Database
Set db = CurrentDb

Dim eight As DAO.Recordset
Dim strSQLeight As String
strSQLeight= "SELECT SUM(TileQty) FROM PickQuery WHERE TileSize = '8mm'"

Set eight= db.OpenRecordset(strSQLeight)
'Error line ^

sometextbox = eight.Fields(0).Value

Run-Time error: Too few parameters expected 3

Intended result is to return 6 (6 x "8mm" strings)

I assume there should be another argument in there but after looking in stack overflow and on google for help, I can't see another way to write this.

CodePudding user response:

Your code works perfectly for me (running in an MSAccess DB, vba module):

Sub foo()
    
    Dim db As DAO.Database
    Set db = CurrentDb
    
    Dim eight As DAO.Recordset
    Dim strSQLeight As String
    strSQLeight = "SELECT SUM(TileQty) FROM PickQuery WHERE TileSize = '8mm'"
    Set eight = db.OpenRecordset(strSQLeight)
    sometextbox = eight.Fields(0).Value

End Sub

Is there something we are missing? How are you running this code? Is it in an access vba module? Somewhere else?

CodePudding user response:

Too few parameters expected 3

This means, that your query PickQuery is missing three parameter values.

Most likely, these are values fetched from the form where you call this code.

  • Related