VBA code fails to run when compiled?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TomM484
    New Member
    • Jun 2013
    • 3

    VBA code fails to run when compiled?

    I have another brief question. So I am trying to protect one form on my database from being accessed by people who don't need to edit the data. I found VBA code that essentially password protects the data (it compares an input string to a hardcoded string). The code (posted below) is put into the 'On Load Event' of the sensitive form.

    It works fine when I am testing it in the database. However when I compile it to a .accde executable, it does not give the pop up window and challenge for a password. It simply allows access to the sensitive form.

    I know that Access usually compiles the code and then deletes the source code, but is that what is causing the problem? Let me know if any of you have an ideas.

    Thanks,
    Tom

    Code for thought:

    Code:
    Dim Password As String
     Password = InputBox("Enter Administrator Password")
     If Password = "mypasscode" Then
     ' Open Form
     DoCmd.OpenForm "AdminPage"
     DoCmd.GoToRecord , , acNewRec
     Else
     MsgBox ("You're not authorized to access this page.")
     DoCmd.Close
     End If
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Assuming your Code is in the Load() Event of the AdminPage Form, try:
    Code:
    Private Sub Form_Load()
    Dim Password As String
    
    Password = InputBox("Enter Administrator Password")
    
    If Password = "mypasscode" Then
      DoCmd.GoToRecord , , acNewRec
    Else
      MsgBox ("You're not authorized to access this page.")
        DoCmd.Close acForm, "AdminPage", acSaveNo
    End If
    End Sub

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #3
      Your problem is just with your logic. You are putting the code in the load event of the form you want to protect. Instead put it in the event that opens the form.

      That way the form never opens if the password is incorrect.

      Code:
      Private Sub OpenMyForm()
      Dim Password As String
      
      	Password = InputBox("Enter Administrator Password")
      
      	If Password = "mypasscode" Then
      		' Open Form
      		DoCmd.OpenForm "AdminPage", , , , acFormAdd
      	Else
      		MsgBox ("You're not authorized to access this page.")
      	End If
      
      End Sub
      You don't need a new command to go to new record as you can set that in the OpenForm. Also I removed the line to close the form. You don't need it as the form never opens if the password is not correct.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        I took it from the perspective that AdminPage is the Start Up Form and subsequent entry into the DB. I may have missed the boat on this one! (LOL). Should my assumption be correct, a Quit as opposed to a Close would be in order anyway.

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          You don't miss the boat very often so this one is allowed should it prove to be the case LOL!

          Comment

          Working...