Access: Modify VBA code from an On Open to On Click

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
I have code that I successfully run for one of my forms "On Open". I am trying to modify this for another form where the code is run when I click a button on the form

Code:
Private Sub Form_Open(Cancel As Integer)
    On Error GoTo Error_Handler
 
    If Nz(DLookup("UserName", "tbl_Users", _
                  "[Object_Name]='" & Me.Name & "' AND [UserName]='" & CurrentUser() & "'"), "") = "" Then
        Cancel = True
        MsgBox "You are not authorized to add a new supplier"
    End If
 
Error_Handler_Exit:
    On Error Resume Next
    Exit Sub
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: Form_Open" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

I want to run the same code (this checks a table to see if the user has permissions)
when I click Button command270. If it finds a match I want it to change a fields setting

Digit_1.Enabled = False
Digit_1.Locked = True
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Got it to work

Code:
Private Sub Command270_Click()
'Check to see if user has permission, if so unlock the fields
On Error GoTo Command270_Click_Err

 
    If Nz(DLookup("UserName", "tbl_Users", _
                  "[Object_Name]='" & Me.Name & "' AND [UserName]='" & CurrentUser() & "'"), "") = "" Then
        MsgBox "You are not authorized to Edit/Modify or add commodities"
        
    Else
    
        Digit_1.Enabled = True
        Digit_1.Locked = False

        Digit_2.Enabled = True
        Digit_2.Locked = False

        Digit_3.Enabled = True
        Digit_3.Locked = False

        Digit_4.Enabled = True
        Digit_4.Locked = False

        IHS.Enabled = True
        IHS.Locked = True

        Definition.Enabled = True
        Definition.Locked = False

        Description.Enabled = True
        Description.Locked = False
        
        Examples.Enabled = True
        Examples.Locked = False
        
    End If


Command270_Click_Exit:
    Exit Sub

Command270_Click_Err:
    MsgBox Error$
    Resume Command270_Click_Exit
 

End Sub

Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top