I have a HR DB and I would like to ensure that users CANNOT edit the BilletNumber of an existing BilletTbl record, but CAN type in a new BilletNumber for a new record.
I am not able to use VBA to determine whether I am in a new record.
I tried using this in the OnLoad event for the form: If Me.Form.NewRecord Then Me.BilletNumber.Locked = False Else Me.BilletNumber.Locked = True End If
For new records, I open the form with this code: DoCmd.OpenForm "Frm_BilletDetail" DoCmd.GoToRecord acDataForm, "Frm_BilletDetail", acNewRec
In the debugger, Me.Form.NewRecord is always 0 even on a new record, and the value of Me.BilletNumber is always the value of the first record even though it is blank on the form for a new record. I am assuming that I may be using the wrong event or not setting a property before opening the form.
Adam Schwanz 56 days ago
I would do this in the on current event, It covers more if the record got moved with the form still open the event could re-fire.
Try this, I know I've used this before and it works.
If NewRecord = True Then
'stuff
ElseIf NewRecord = False Then
'other stuff
End If
Adam Schwanz 56 days ago
Also you don't need the Me if your trying to interact with the form the code is on, BilletNumber.Locked will work fine
Brian Crawford 56 days ago
I found the issue. It was with the order of operations. The new record button first executes the DoCmd.OpenForm command, which triggers the OnLoad code of the form with the record pointer on the first record (i.e. NOT a new record), which is where the If statement runs with NewRecord being (always) False. Then the second line in the button DoCmd.GoToRecord executes AFTER the field is already locked (from the form's OnLoad code).
The solution was to remove the If statement from the OnLoad and lock the field by default
BilletNumber.Locked = True.
Then move the unlock BilletNumber field as the third line in the New Record button code.
Adam Schwanz 56 days ago
You could also have just put it in the on current event instead, just FYI
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.