Conflict in use of OpenArgs and Validation Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hulm1
    New Member
    • Mar 2008
    • 22

    Conflict in use of OpenArgs and Validation Code

    am checking for completed fields within a form in the Before Update event. However within the Form Unload event I am also using open args code to close the form and open the previous form. The problem is that when my MsgBox comes up and I check the OK button, the previous form opens!

    Can anyone please advise? Thanks in advance

    BEFORE UPDATE CODE
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    'Place an asterisk (*) in the Tag Property of the text
    'boxes you wish to validate.
    'Then in the BeforeUpdate Event of the form, copy/paste the following:
    
    
        Dim msg As String, Style As Integer, Title As String
        Dim nl As String, ctl As Control
    
    
        nl = vbNewLine & vbNewLine
    
        For Each ctl In Me.Controls
          If ctl.ControlType = acTextBox Then
            If ctl.Tag = "*" And Trim(ctl & "") = "" Then
             msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
                   "You can't save this record until this data is provided!" & nl & _
                   "Enter the data and try again . . . "
              Style = vbCritical + vbOKOnly
              Title = "Required Data..."
              MsgBox msg, Style, Title
              ctl.SetFocus
              Cancel = True
              Exit For
            End If
          End If
        Next
    
    End Sub
    FORM UNLOAD CODE
    Code:
    Private Sub Form_Unload(Cancel As Integer)
        On Error Resume Next
         Forms(Me.OpenArgs).Visible = True
       
        Forms!Project_Edit.cboCompanyID.Requery
    End Sub
    Last edited by NeoPa; Oct 5 '08, 07:16 PM. Reason: Please remember to use the [CODE] tags provided
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Originally posted by Hulm1
    ... The problem is that when my MsgBox comes up and I check the OK button, the previous form opens! ....
    What did you do before it? Closed the form?
    If so then work up your Form_Unload handler. It doesn't distinguish whether data validation passed or failed.

    Regards,
    Fish

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      Try removing the

      Exit For

      line from your code. The first time a validation fails and you click on the OK in your warning messagebox, I think this line is causing you to drop out of your validation routine, whether the fields are populated or not, and Access then moves on to your Form_Unload event.

      Linq ;0)>

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        I don't see why this would happen at all I'm afraid.

        I don't agree with Linq on this occasion (quite rare occurrence). The Exit For is exactly right it seems to me. Why it reverts to your other form doesn't seem to be explained by the code shown.

        The OpenArgs reference in the Form_Unload procedure may be a problem. My experience has always been that it (Me.OpenArgs) is only available to the called object (form or report) for the first line of code (in Form_Open). After this point it is reset and shows nothing. To get around this it is necessary to save it to a variable whose scope is such that it is available when needed. To be available in a separate procedure it should be an object level variable.

        Comment

        Working...