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

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,690
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
 

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,690
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!
 

Forum statistics

Threads
1,082,305
Messages
5,364,406
Members
400,801
Latest member
julievandermeulen

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top