Lock Field after update (1 Viewer)

kobiashi

Registered User.
Local time
Today, 11:59
Joined
May 11, 2018
Messages
258
hi

i want to be able to lock a field on a form after the user updates it, i have an if statement but i cant seem to get it to work, can someone take a look at it and tell me if theres something missing from it

Code:
Private Sub Form_Current()
    If IsNull(Me.cboVehicleID) Then
        cboVehicleID.Locked = False
    Else
        cboVehicleID.Locked = True
        Me.cboVehicleID.BackColor = RGB(206, 206, 206)
    End If
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:59
Joined
Sep 21, 2011
Messages
14,051
Shouldn't you be using the Me. prefix everywhere?
 

kobiashi

Registered User.
Local time
Today, 11:59
Joined
May 11, 2018
Messages
258
i tried that, but didnt have any effect
 

isladogs

MVP / VIP
Local time
Today, 11:59
Joined
Jan 14, 2017
Messages
18,186
Perhaps you have a zero length string
Use

Code:
If Nz(Me.cboVehicleID,"")="" Then

If for any reason that also fails, try setting the control as locked by default then in form current unlock if the field is null or a ZLS
 

CJ_London

Super Moderator
Staff member
Local time
Today, 11:59
Joined
Feb 19, 2013
Messages
16,555
you can also simplify your code


cboVehicleID.Locked =nz(Me.cboVehicleID,"")<>""

I'm assuming cboVehicleID is text and not a number

i want to be able to lock a field on a form after the user updates it
You are using the form current event - so the code won't fire until you return to the record - not sure if that is what you mean or you mean the user only has one chance to choose the right ID, in which case you need to use the cboVehicleID control after update event as well
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:59
Joined
May 7, 2009
Messages
19,175
use the control's OnGotFocus Event instead of the Form's Current event:

Private Sub cboVehicleID_GotFocus()
Me.cboVehicleID.Locked = (Trim(Me.cboVehicleID & "")="")
End Sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:59
Joined
Feb 28, 2001
Messages
27,001
Or the LostFocus event. But whichever one you choose, use the same event for each control subject to this restriction. I.e. don't use GotFocus sometimes and LostFocus sometimes.

The issue also might be not that you use GotFocus or LostFocus instead of Current, but rather IN SUPPLEMENT to Current. This would apply if it is possible for the field to be occupied when you navigate to a different record because was already updated. If that can happen, then both Current and one of the Focus events will be needed to keep it "clean."
 

kobiashi

Registered User.
Local time
Today, 11:59
Joined
May 11, 2018
Messages
258
thanks everybody for the help in sorting this, the only one i could get to work was

Code:
Private Sub cboVehicleID_GotFocus()
    Me.cboVehicleID.Locked = Nz(Me.cboVehicleID, "") <> ""
End Sub
 

Users who are viewing this thread

Top Bottom