Knife1
05-31-2011, 08:50 AM
Hi.
This question has 2 parts really. I have a form with a command button which runs a sql query in a string variable. The VBA code is below. The sql string basically counts the number of branches with less than 2000 customers from a table with Branch Id and customerTotal columns. However i want to run this query maybe 10 times with different fixed parameters. i.e. where customers is :
<2000
<3000
<4000
<5000 and so on....
So what is the best way of passing these parameters to the query in a loop without having to run the procedure 10 times.
Also I would like to be able to output the 10 count results in a single list, in the query output window, immediate window, table, whatever a long as all the results are in a list. Currently it just runs the one query and open the the query window.
Any pointers... ?
Private Sub cmdCountBranchTotals_Click()
Dim db As Database
Dim rs As Recordset
Dim qdf As QueryDef
Dim TableName As string
Set db = CurrentDb
Set qrydef = db.QueryDefs("qryCountTotals")
TableName = msgboxTableVersion
strsql = "SELECT Count([Branch ID]) AS CountOfBranch " & _
"FROM [" & TableName & "] " & _
"WHERE CustTotal < 2000; "
qrydef.SQL = strsql
DoCmd.OpenQuery "qryCountTotals", acViewNormal
End Sub
Thanks
This question has 2 parts really. I have a form with a command button which runs a sql query in a string variable. The VBA code is below. The sql string basically counts the number of branches with less than 2000 customers from a table with Branch Id and customerTotal columns. However i want to run this query maybe 10 times with different fixed parameters. i.e. where customers is :
<2000
<3000
<4000
<5000 and so on....
So what is the best way of passing these parameters to the query in a loop without having to run the procedure 10 times.
Also I would like to be able to output the 10 count results in a single list, in the query output window, immediate window, table, whatever a long as all the results are in a list. Currently it just runs the one query and open the the query window.
Any pointers... ?
Private Sub cmdCountBranchTotals_Click()
Dim db As Database
Dim rs As Recordset
Dim qdf As QueryDef
Dim TableName As string
Set db = CurrentDb
Set qrydef = db.QueryDefs("qryCountTotals")
TableName = msgboxTableVersion
strsql = "SELECT Count([Branch ID]) AS CountOfBranch " & _
"FROM [" & TableName & "] " & _
"WHERE CustTotal < 2000; "
qrydef.SQL = strsql
DoCmd.OpenQuery "qryCountTotals", acViewNormal
End Sub
Thanks