Hi, With Access97, I update f1 based on more than 3 conditions, e.g if f2=a and f3=b, f1=xxx else if f2=a or f3>b, f1 = yyy, etc) it'll be too complicated if I use iif() statement in update query. So, I used VBA code instead. The table with f1 in it has 70,000 records. Here is my code:
Function updt()
Dim db As Database, rst As Recordset
Set db = CurrentDb()
Set rst = db.OpenRecordset("Non-MM visits test"
Dim tbl As String, f1 As String, sql As String, sql2 As String, sql3 As String
tbl = "[non-MM visits test]"
f1 = "[visit factor]" '(single field)
rst.MoveFirst
Do Until rst.EOF
sql = "Update " & tbl & " set " & tbl & "." & f1 & "=1"
sql2 = "Update " & tbl & " set " & tbl & "." & f1 & "=" & tbl & ".[mccalc perc]"
sql3 = "Update " & tbl & " set " & tbl & "." & f1 & "=" & tbl & ".[mcal calc paid]/100"
If rst("mcal paid"
= 0 Then
If rst("Mcare Paid"
= 0 Then
DoCmd.RunSQL (sql)
Else
DoCmd.RunSQL (sql2)
End If
ElseIf rst("mcal paid"
> 0 And rst("mcare paid"
= 0 Then
If rst("mcal paid"
= 34 Or 436 Or 550 Or 654 Then
DoCmd.RunSQL (sql)
Else
DoCmd.RunSQL (sql3)
End If
End If
rst.MoveNext
Loop
rst.Close
End Function
I only want the update query to run once, depends on the if statement, for each record it read. But it seemed to run several times from the status bar. What's wrong? Thanks a bunch.
Function updt()
Dim db As Database, rst As Recordset
Set db = CurrentDb()
Set rst = db.OpenRecordset("Non-MM visits test"
Dim tbl As String, f1 As String, sql As String, sql2 As String, sql3 As String
tbl = "[non-MM visits test]"
f1 = "[visit factor]" '(single field)
rst.MoveFirst
Do Until rst.EOF
sql = "Update " & tbl & " set " & tbl & "." & f1 & "=1"
sql2 = "Update " & tbl & " set " & tbl & "." & f1 & "=" & tbl & ".[mccalc perc]"
sql3 = "Update " & tbl & " set " & tbl & "." & f1 & "=" & tbl & ".[mcal calc paid]/100"
If rst("mcal paid"
If rst("Mcare Paid"
DoCmd.RunSQL (sql)
Else
DoCmd.RunSQL (sql2)
End If
ElseIf rst("mcal paid"
If rst("mcal paid"
DoCmd.RunSQL (sql)
Else
DoCmd.RunSQL (sql3)
End If
End If
rst.MoveNext
Loop
rst.Close
End Function
I only want the update query to run once, depends on the if statement, for each record it read. But it seemed to run several times from the status bar. What's wrong? Thanks a bunch.