Hello, I have been trying to get this code to work for a bit now and just can't figure it out so I could really use some help. A quick note as to what it is that I'm trying to do is basically count records in one table, with criteria, and then put that count into another table. So I have 2 tables one with the locations and the other with all the anomalies related to those locations. And I'm trying to use a query to get counts of anomalies, with certain criteria, for each of those locations.
The actual query that I use to do the count and update works just fine when I manually type in each location. But I have a few hundreds locations and so I'm trying to have code cycle through these locations and update the counts. So, here is the code that I'm trying to use ... and is failing miserably.
Hopefully you guys can show me the error of my ways here, and I appreciate and help. Thanks
The actual query that I use to do the count and update works just fine when I manually type in each location. But I have a few hundreds locations and so I'm trying to have code cycle through these locations and update the counts. So, here is the code that I'm trying to use ... and is failing miserably.
Code:
Public Function gridstatusanomalies() As Boolean
On Error Resume Next
' Requires reference to Microsoft Office 11.0 Object Library!!!!!!
Dim locals As Variant
Dim Var As Variant
'Then we tun off the warnings
DoCmd.SetWarnings False
'DAO.Database Related records
'DAO.Recordset Multi-valued field recordset
Dim db As DAO.database, rst As DAO.Recordset, sSQL As String
'Now the code sets the current database to the one we jsut worked on
Set db = CurrentDb()
'This next part selects the query to use as variables
sSQL = "SELECT Location FROM Status_Anomaly_Count"
Set rst = db.OpenRecordset(sSQL, dbOpenSnapshot)
'This part then associates the Location to the strcolumn variables form above
If Not rst.BOF And Not rst.EOF Then
Do Until rst.EOF
locals = rst!Location
For Each Var In locals
Dim strColumn1 As String
strColumn1 = Var
Dim ACount As String
'runs the update query with strcolumn1 variable
ACount = "UPDATE Status_Anomaly_Count SET Status_Anomaly_Count.Num_Greater_40mV = (DCount('Target_ID', 'Anomaly_Table', 'CH3_final>40 AND [location] = '" & strColumn1 & "')) WHERE (((Status_Anomaly_Count.Location)= '" & strColumn1 & "'))"
DoCmd.RunSQL ACount
Next
rst.MoveNext
Loop
End If
Set rst = Nothing
Set db = Nothing
'Turns warngins back on
DoCmd.SetWarnings True
'Finally the code displays a message box to assure you that it has updated fields
Dim iResponce As Integer
iResponce = MsgBox("Grid Status Anomalies Counted", vbOKOnly, "File Update")
End Function
Hopefully you guys can show me the error of my ways here, and I appreciate and help. Thanks