Disable or Enable Primary Key , depending upon ADD or EDIT

Hello Experts,
i am using the same form for Editing and Addition of new record (say Purchase Entry).
I dont want to allow user to edit PO_No (Purchase Order No) if the same is used in Sales Table (tblSale)

On Load event of the form, i am using below code :

Private Sub Form_Load()

'If Me.AllowEdits = True Then
'If Me.DataEntry = False Then
If Me.NewRecord = False Then ' to check if its in Edit Mode

    Dim poid As String
    Dim stlinkCriteria As String
    MsgBox "Form is in edit mode"
    poid = Me.PO_No.Value

    stlinkCriteria = "[PO_NO]=" & "'" & poid & "'"

If DCount("PO_NO", "tblsale", stlinkCriteria) > 0 Then
    MsgBox "Dcount of total sales records :" & DCount("PO_NO", "tblsale", stlinkCriteria)
    Me.txtPO_no.Enabled = False
    MsgBox "This PO No can not be edited as this is used in sale Master", vbOKOnly, "ERROR"
Else
    Me.txtPO_no.Enabled = True
    MsgBox stlinkCriteria, vbOKOnly
End If
End If

End Sub


Till now its working fine, but when the form is used for new record, textbox  PO_NO is automatically disabled. :(

Could you please help ?
Thanks in advance.
Kamlesh Jain
Kamlesh JainManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:
You are only calling the function once (OnLoad), so move the code to the OnCurrent event.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kamlesh JainManagerAuthor Commented:
hello. Thanks for your prompt response.


My problem is that i am not gettng the field "PO No " enabled while i am in NEw Record Mode / Data Entry Mode.

This field needs to be enabled / disabled depending upon the selection of record by Dcount and i am getting the same done and its working fine.

Kindly help.
Gustav BrockCIOCommented:
You have:

If Me.NewRecord = False Then 
    ' Stuff ...
Else
    Me.txtPO_no.Enabled = True
End If

Open in new window

So, if the code is run, the control will be enabled. And, if put in the OnCurrent event, it will.
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

PatHartmanCommented:
The Current event is the correct event but you actually need to toggle the setting otherwise once you add a record, the PO will stay enabled until you close and then reopen the form.

If Me.NewRecord = False Then 
    Me.txtPO_No.Enabled = False
Else
    Me.txtPO_no.Enabled = True
End If

Open in new window

Kamlesh JainManagerAuthor Commented:
Thanks.. i just modified a little bit and got the desired results :
If Me.NewRecord = True Then ' to check if its in Edit Mode

    Me.PO_No.Enabled = True

Else
    Dim poid As String
    Dim stlinkCriteria As String
    'MsgBox "Form is in edit mode"
    poid = Me.PO_No.Value

    stlinkCriteria = "[PO_NO]=" & "'" & poid & "'"

If DCount("PO_NO", "tblsale", stlinkCriteria) > 0 Then
    MsgBox " You have already booked " & DCount("PO_NO", "tblsale", stlinkCriteria) & " Sale against this PO & PO Number can not be edited ...", vbCritical, "NOTE"
    Me.PO_No.Enabled = False
Else
    Me.PO_No.Enabled = True
    'MsgBox "PO no shall be enabled", vbOKOnly
End If
End If
Kamlesh JainManagerAuthor Commented:
Thanks. Closing the question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.