Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2010
    Posts
    96
    Provided Answers: 4

    Unhappy Unanswered: Cancel Module and Close Form

    Hi all

    I've been trying to add a cancel button to a form (frmPassword) that currently only has a submit button on it, effectively meaning that the user can't currently cancel the operation.

    A button on Form1 opens Form2 that has this event on it's OnOpen property (effectively opening frmPassword to get the password)

    Code:
    Private Sub Form_Open(Cancel As Integer)
      
       Dim Hold As Variant
       Dim tmpKey As Long
       Dim I As Integer
       Dim rs As DAO.Recordset
       Dim db As DAO.Database
    
       On Error GoTo Error_Handler
       ' Prompt the user for the Password.
       DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
                Hold = MyPassword
    
    ' Open the table that contains the password.
       Set db = CurrentDb
       Set rs = db.OpenRecordset("tblPassword", dbOpenTable)
       rs.Index = "PrimaryKey"
       rs.Seek "=", Me.Name
    
       If rs.NoMatch Then
          MsgBox "Password information not found. Try Again"
          Cancel = -1
       Else
    
          If Not (rs![KeyCode] = KeyCode(CStr(Hold))) Then
             MsgBox "Password not recognised. " & _
                "Please try again.", vbOKOnly, "Incorrect Password"
             Cancel = -1
          End If
       End If
       rs.Close
       db.Close
       Exit Sub
    
    Error_Handler:
       MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
       Exit Sub
    End Sub
    The code behind the Cancel button is;

    Code:
    Private Sub btnCancel_Click()
    On Error GoTo Err_btnCancel_Click
    
    
        If Me.Dirty Then Me.Dirty = False
        DoCmd.Close
    
    Exit_btnCancel_Click:
        Exit Sub
    
    Err_btnCancel_Click:
        MsgBox Err.Description
        Resume Exit_btnCancel_Click
        
    End Sub
    Now, if the user clicks the Cancel button the message "Password not recognised." appears, indicating that once the form frmPassword closes, the calling module continues on, which is not what I want. I need to exit out of that module as well.

    How do I get the Cancel module to actually close the form frmPassword AND cancel actions of the calling module and return control to Form1?

    Any assistance would be greatly appreciated.

  2. #2
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    602
    Provided Answers: 31
    you dont seek a record, you just open directly to it in the query
    set rst = currentdb.openrecordset("select * from table where [passord]=" & me.pass & "'")

    you dont open a form then ask for the password,
    you ask for the password, THEN open the form (if correct)

  3. #3
    Join Date
    Sep 2010
    Posts
    96
    Provided Answers: 4
    Hello Ranman

    Thanks for your reply, however I don't quite follow you. Are you suggesting that opening the frmPassword should be done from the command button of Form1?

    As it stand now the whole process works fine, it's just that I can't cancel it once it has started.

Posting Permissions

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