An event within an event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wildster
    New Member
    • Feb 2008
    • 16

    An event within an event

    Hi,
    I want to put an event within an event but it seems as though this can't be done and I need to figure a work around. Basically I am trying to say that if the user clicks on the 'cmdRenewTraini ng' button then it just saves the record without the prompt asking if they wish to save. If they dont press the 'cmdRenewTraini ng' button then I want the prompt to ask them if they want to save changes. The code looks to make sense but I think the reason Access wont allow it is because it's an event within an event, any ideas how I can get around this problem please? I'm guessing I need to remove lines 2-5 and bring them outside the Private Sub Form_BeforeUpda te(Cancel As Integer)??

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     If cmdRenewTraining_Click() Then
    DoCmd.Save
    End If
    Else
    
        If MsgBox("Changes have been made to this record." _
            & vbCrLf & vbCrLf & "Do you want to save these changes?" _
            , vbYesNo, "Changes Have Been Made") = vbYes Then
                DoCmd.Save
            Else
                DoCmd.RunCommand acCmdUndo
        End If
        End If
    End Sub
    Thanks
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by Wildster
    Hi,
    I want to put an event within an event but it seems as though this can't be done and I need to figure a work around. Basically I am trying to say that if the user clicks on the 'cmdRenewTraini ng' button then it just saves the record without the prompt asking if they wish to save. If they dont press the 'cmdRenewTraini ng' button then I want the prompt to ask them if they want to save changes. The code looks to make sense but I think the reason Access wont allow it is because it's an event within an event, any ideas how I can get around this problem please? I'm guessing I need to remove lines 2-5 and bring them outside the Private Sub Form_BeforeUpda te(Cancel As Integer)??

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     If cmdRenewTraining_Click() Then
    DoCmd.Save
    End If
    Else
    
        If MsgBox("Changes have been made to this record." _
            & vbCrLf & vbCrLf & "Do you want to save these changes?" _
            , vbYesNo, "Changes Have Been Made") = vbYes Then
                DoCmd.Save
            Else
                DoCmd.RunCommand acCmdUndo
        End If
        End If
    End Sub
    Thanks
    1. First of all, you cannot Save a Record in the BeforeUpdate() Event of a Form.
    2. You can experiment with the following logic, it should be close to what you are requesting:
      1. Declare a Private Variable in the Form's Code Module. The state of this Variable (True/False) will indicate whether or not the cmdRenewTrainin g button was clicked.
        [CODE=vb]Private blnRenewTrainin g As Boolean[/CODE]
      2. Place the following code in the Click() Event of cmdRenewTrainin g:
        [CODE=vb]
        Private Sub cmdRenewTrainin g___Click()
        blnRenewTrainin g = True 'Indicate the Button was clicked
        DoCmd.DoMenuIte m acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
        End Sub[/CODE]
      3. Copy and Paste the following code in the BeforeUpdate() Event of the Form. The code will first check and see if the cmdRenewTrainin g button was clicked and act accordingly:
        [CODE=vb]
        Private Sub Form_BeforeUpda te(Cancel As Integer)
        If blnRenewTrainin g = False Then 'Renew Training Button not clicked
        If MsgBox("Changes have been made to this record." _
        & vbCrLf & vbCrLf & "Do you want to save these changes?" _
        , vbYesNo, "Changes Have Been Made") = vbYes Then
        Else
        DoCmd.RunComman d acCmdUndo
        End If
        End If
        End Sub[/CODE]
      4. Reset our Variable in the AfterUpdate() Event of the Form:
        [CODE=vb]
        Private Sub Form_AfterUpdat e()
        blnRenewTrainin g = False
        End Sub[/CODE]
    3. I basically threw this together before bedtime, I'll leave it up to you to determine how well it works. It has obvious drawbacks such as if Required Fields are not filled in prior to repeated Saves, you will continue to get the annoying prompt, etc,

    Comment

    • Wildster
      New Member
      • Feb 2008
      • 16

      #3
      Originally posted by ADezii
      1. First of all, you cannot Save a Record in the BeforeUpdate() Event of a Form.
      2. You can experiment with the following logic, it should be close to what you are requesting:
        1. Declare a Private Variable in the Form's Code Module. The state of this Variable (True/False) will indicate whether or not the cmdRenewTrainin g button was clicked.
          [CODE=vb]Private blnRenewTrainin g As Boolean[/CODE]
        2. Place the following code in the Click() Event of cmdRenewTrainin g:
          [CODE=vb]
          Private Sub cmdRenewTrainin g___Click()
          blnRenewTrainin g = True 'Indicate the Button was clicked
          DoCmd.DoMenuIte m acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
          End Sub[/CODE]
        3. Copy and Paste the following code in the BeforeUpdate() Event of the Form. The code will first check and see if the cmdRenewTrainin g button was clicked and act accordingly:
          [CODE=vb]
          Private Sub Form_BeforeUpda te(Cancel As Integer)
          If blnRenewTrainin g = False Then 'Renew Training Button not clicked
          If MsgBox("Changes have been made to this record." _
          & vbCrLf & vbCrLf & "Do you want to save these changes?" _
          , vbYesNo, "Changes Have Been Made") = vbYes Then
          Else
          DoCmd.RunComman d acCmdUndo
          End If
          End If
          End Sub[/CODE]
        4. Reset our Variable in the AfterUpdate() Event of the Form:
          [CODE=vb]
          Private Sub Form_AfterUpdat e()
          blnRenewTrainin g = False
          End Sub[/CODE]
      3. I basically threw this together before bedtime, I'll leave it up to you to determine how well it works. It has obvious drawbacks such as if Required Fields are not filled in prior to repeated Saves, you will continue to get the annoying prompt, etc,


      Thanks very much, it's a good solution to the problem and makes sense. Having made a few tweaks here and there it now works perfectly.

      Thanks again

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by Wildster
        Thanks very much, it's a good solution to the problem and makes sense. Having made a few tweaks here and there it now works perfectly.

        Thanks again
        You are quite welcome.

        Comment

        Working...