PDA

View Full Version : help with correct IF statement syntax in vba



keek2a4
03-12-2008, 09:46 AM
Private Sub Command0_Click()
Dim db As Database
Dim insTbl As String

Set db = CurrentDb


CurrentDb.Execute "UPDATE tblStudent " & _
"SET tblStudent.deleted = 1;"

End Sub
I have the following code above that updates the field named "deleted" to 1 when the button is clicked. I need to include a "IF Statement" that checks the if the "flag" field in the school table is equal to 1, but I do not know the correct syntax for the IF statement. I am trying to achieve something like this:


Private Sub Command0_Click()
Dim db As Database
Dim insTbl As String

Set db = CurrentDb

If tblSchool.flag = 1 then ' how can I write the IF statement syntax in vba

CurrentDb.Execute "UPDATE tblStudent " & _
"SET tblStudent.deleted = 1;"
End IF

End Sub

Thanks

Trevor
03-12-2008, 09:40 PM
if tableschool.flag = "1" Then
[Condition]

End if
End Sub

keek2a4
03-13-2008, 03:23 AM
If tblschool.Flag = "1" Then
CurrentDb.Execute "UPDATE tblStudent " & _
"SET tblStudent.deleted = 1;"
End If

But when i execute the above code, I keep getting the error msg:
Run-time Error "424"
Object Required

Any Ideas?

DarkSprout
03-13-2008, 05:41 AM
The error may well be with the 'tblschool.Flag'! What is this field and where is it read from, is it a button on the form?

Ok, If the form is bound to the table tblschool and Flag a boolean Yes/No control. Then...

If [Flag] then
CurrentDb.Execute "UPDATE tblStudent " & _
"SET tblStudent.deleted = 1;"
End If
if Flag is a Integer then
If [Flag] = 1 then ...
If the form is Unbound then

If DLookup("[Flag]", "tblschool", "[AutoNumber] = " & anRefID) = 1 Then ...

keek2a4
03-13-2008, 07:24 AM
Hi, The form is Unboud. Thanks for your, reply I will try it out.
Could you tell me what this line actually does?


"[AutoNumber] = " & anRefID)

Thanks

Trevor
03-13-2008, 05:08 PM
Autonumber is the number access automaticly assigns to every record as the primary key , autonumber is not always the field name but is always the default property of that field (in my experience the default name for the field is [ID]
If you selected no to not having a primary key for that table then you will not have an autonumber proporty set for a field by default and , you will have to make a primary key by right clicking next to the field name in table design view and selecting primary key (not the primary key field is a value that must be unique to each record)