Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Jul 2013
    Posts
    46

    Unanswered: Form Still Opens With Invalid Password

    I have this code in the On Open event:

    Private Sub Form_Open(Cancel As Integer)
    Dim PassWord As String
    PassWord = InputBox("Enter Password")
    If PassWord = "Houston" Then
    'Open Form
    DoCmd.OpenForm "Clerk"
    Else
    MsgBox ("You are not authorized.")
    'Close Form
    DoCmd.Close , "Clerk"
    End If
    End Sub

    The form still opens with the wrong password. What am I missing?

    Thanks,
    Bill

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Set cancel = true to cancel the form open event
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Jul 2013
    Posts
    46

    Form Still Opens With Invalid Password

    Where do I put that?

  4. #4
    Join Date
    Jul 2013
    Posts
    46
    I found where to put that statement:

    Private Sub Form_Open(Cancel As Integer)
    Dim PassWord As String
    PassWord = InputBox("Enter Password")
    If PassWord = "Houston" Then
    'Open Form
    DoCmd.OpenForm "Clerk"
    Else
    MsgBox ("You are not authorized.")
    Cancel = True
    End If
    End Sub

    Works great! Thanks very much.

    Could I add another password so either one will open the form?

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Code:
    If PassWord = "Houston" or password = "myotherpassword" Then
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Jul 2013
    Posts
    46
    Private Sub Form_Open(Cancel As Integer)
    Dim PassWord As String
    PassWord = InputBox("Enter Password")
    If PassWord = "Houston" Or PassWord = "Austin" Then
    'Open Form
    DoCmd.OpenForm "Clerk"
    Else
    MsgBox ("You are not authorized.")
    Cancel = True
    End If
    End Sub

    I get error on the line in bold now.

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    and the error message is?
    does the form Clerk exist?

    when pasting code please use the [ c o d e ] & [ / c o d e]
    without the spaces around your code
    also please indent your code, it makes it easier to read
    Code:
    If PassWord = "Houston" Or PassWord = "Austin" Then
        'Open Form
        DoCmd.OpenForm "Clerk"
    Else
        MsgBox ("You are not authorized.")
        Cancel = True
    End If
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I still think this is an iffy way of implementing security, assuming that your 'security' is actually meant to be protecting something. hardcoding passwords into a db is not a smart call especially if the db is visible as an MDB or ACCDB.

    changing a password your way requires code changes.
    I'd rather be riding on the Tiger 800 or the Norton

  9. #9
    Join Date
    Jul 2013
    Posts
    46
    I have a main switchboard sectioned with Users and Administrators. The Users area have buttons to open forms without protected passwords. The Administrators area have buttons with different passwords to open certain forms depending on the passwords.

    I tried the code but shows error:

    Private Sub Form_Open(Cancel As Integer)
    Dim PassWord As String
    PassWord = InputBox("Enter Password")
    If PassWord = "Houston" Or PassWord = "Austin" Then
    'Open Form
    DoCmd.OpenForm "Clerk"
    Else
    MsgBox ("You are not authorized.")
    Cancel = True
    End If
    End Sub


    *************

    And yes the form 'Clerk" does exist.

  10. #10
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    and the error message is?
    I'd rather be riding on the Tiger 800 or the Norton

  11. #11
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    you arent;' trying to open form clerk from within form clerk are you?
    I'd rather be riding on the Tiger 800 or the Norton

  12. #12
    Join Date
    Jul 2013
    Posts
    46

    Form Still Opens With Invalid Password

    Yes this code is in the On Open event.

  13. #13
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    and the error message is?

    if you are trying to open the form from within the form you may well have problems
    if this code is running within the form clerk then you don't need to open it again. arguably you could set cancel to false in place of the open form command
    or alternatively
    Code:
    If PassWord <> "Houston" Or PassWord = "Austin" Then
        'do nothing
        cancel = false 'optional to make it explicit what you are doing. the default value is false
    Else
        MsgBox ("You are not authorized.")
        Cancel = True
    End If
    I'd rather be riding on the Tiger 800 or the Norton

  14. #14
    Join Date
    Jul 2013
    Posts
    46
    Private Sub Form_Open(Cancel As Integer)
    Dim PassWord As String
    PassWord = InputBox("Enter Password")
    If PassWord <> "Houston" Or PassWord = "Austin" Then
    'do nothing
    Cancel = True 'optional to make it explicit what you are doing. the default value is false
    MsgBox ("You are not authorized.")
    Else
    Cancel = False
    End If
    End Sub


    I had to switch the places of True and False because it was doing the opposite. Now the word 'Houston' works but entering 'Austin' doesn't.

    I really appreciate the help you are giving me. I am trying to learn.

  15. #15
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    do you really mean
    Code:
    if PassWord <> "Houston" Or PassWord = "Austin" Then
    shouldn't that read
    Code:
    If PassWord = "Houston" Or PassWord = "Austin" Then
        'do nothing
        Cancel = false 'optional to make it explicit what you are doing. the default value is false
    Else
        MsgBox ("You are not authorized.")
        Cancel = true
    End If
    End Sub

    I think the <> crept in becuase i was trying to eliminate the cancle = false
    so it would probably be expressed as

    Code:
    if PassWord <> "Houston" Or PassWord = "Austin" Then
    it could be simplified to read:-
    Code:
    If not( PassWord = "Houston" Or PassWord = "Austin" )Then
        MsgBox ("You are not authorized.")
        Cancel = true
    End If
    End Sub
    if you are serious about developing in Access then get to grips with debugging in Access. do a google on that
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •