Saving form with required fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ledmark
    New Member
    • Oct 2007
    • 19

    Saving form with required fields

    Hello - I have made forms which have many required fields so when a person is filling out the forms and they get interrupted they can't save it and go back to it - they have to start over.

    Is there a way to save the form with written code so that they can't print it unless all required fields are entered?

    Thanks for any help.

    Laura
  • sierra7
    Recognized Expert Contributor
    • Sep 2007
    • 446

    #2
    Hi Laura
    You could write a function called CheckComplete that validated each of your mandatory fields and if any failed would give an error message to inform the user and prevent printing.
    Code:
     
    Private Function checkComplete() As Boolean
    checkComplete = True
    If IsNull(Me.txtField1) Or Len(Me.txtField1) < 2 Then
    	checkComplete = False
    	MsgBox "Field " & Me.txtField1.Name & " is incomplete", vbCritical, "Missing Data"
    	Exit Function
    End If
     
    If IsNull(Me.txtField2) Or Len(Me.txtField2) < 20 Then
    	checkComplete = False
    	MsgBox "Field " & Me.txtField2.Name & " is incomplete", vbCritical, "Missing Data"
    	Exit Function
    End If
     
    End Function
    The above code first test that the field is not empty and then tests that the user has not entered a prefunctory value to 'cheat' the system. You can set the length to whatever suits you. You must have both checks because an empty field is Null not zero, so doesn't fail the Len() test.

    You would call this function from your print button
    Code:
     If checkComplete = True Then 
    	 DoCmd.OpenReport "ReportName".....
    Else
    	 Exit Sub
    End If

    S7

    Comment

    • Ledmark
      New Member
      • Oct 2007
      • 19

      #3
      Thank you so much for your reply and help. The following is the code I have for printing the for right now:

      Private Sub Print_Recruitme nt_Report_Click ()
      On Error GoTo Err_Print_Recru itment_Report_C lick

      Dim stDocName As String

      'MsgBox "value=" + RequestNumberTe xtField

      stDocName = "Recruitmen t 52"
      DoCmd.OpenRepor t stDocName, acNormal

      Exit_Print_Recr uitment_Report_ Click:
      Exit Sub

      Err_Print_Recru itment_Report_C lick:
      MsgBox Err.Description
      Resume Exit_Print_Recr uitment_Report_ Click

      End Sub

      Would I put this statement If checkComplete = True Then in front of my code?
      I don't know quite how to put them together?

      Thanks for any more help.
      Laura

      Originally posted by sierra7
      Hi Laura
      You could write a function called CheckComplete that validated each of your mandatory fields and if any failed would give an error message to inform the user and prevent printing.
      Code:
       
      Private Function checkComplete() As Boolean
      checkComplete = True
      If IsNull(Me.txtField1) Or Len(Me.txtField1) < 2 Then
      	checkComplete = False
      	MsgBox "Field " & Me.txtField1.Name & " is incomplete", vbCritical, "Missing Data"
      	Exit Function
      End If
       
      If IsNull(Me.txtField2) Or Len(Me.txtField2) < 20 Then
      	checkComplete = False
      	MsgBox "Field " & Me.txtField2.Name & " is incomplete", vbCritical, "Missing Data"
      	Exit Function
      End If
       
      End Function
      The above code first test that the field is not empty and then tests that the user has not entered a prefunctory value to 'cheat' the system. You can set the length to whatever suits you. You must have both checks because an empty field is Null not zero, so doesn't fail the Len() test.

      You would call this function from your print button
      Code:
       If checkComplete = True Then 
      	 DoCmd.OpenReport "ReportName".....
      Else
      	 Exit Sub
      End If

      S7

      Comment

      • Denburt
        Recognized Expert Top Contributor
        • Mar 2007
        • 1356

        #4
        I would think that the following should work.

        Code:
        Private Sub Print_Recruitment_Report_Click()
        On Error GoTo Err_Print_Recruitment_Report_Click
        
        Dim stDocName As String
              If checkComplete = True Then
        'MsgBox "value=" + RequestNumberTextField
        
        stDocName = "Recruitment 52"
        DoCmd.OpenReport stDocName, acNormal
        
              Else
                   Exit Sub
              End If
        
        Exit_Print_Recruitment_Report_Click:
        Exit Sub
        
        Err_Print_Recruitment_Report_Click:
        MsgBox Err.Description
        Resume Exit_Print_Recruitment_Report_Click
        
        End Sub

        Comment

        • Ledmark
          New Member
          • Oct 2007
          • 19

          #5
          Thank you so much for all your help - it is really appreciated.


          Originally posted by Denburt
          I would think that the following should work.

          Code:
          Private Sub Print_Recruitment_Report_Click()
          On Error GoTo Err_Print_Recruitment_Report_Click
          
          Dim stDocName As String
                If checkComplete = True Then
          'MsgBox "value=" + RequestNumberTextField
          
          stDocName = "Recruitment 52"
          DoCmd.OpenReport stDocName, acNormal
          
                Else
                     Exit Sub
                End If
          
          Exit_Print_Recruitment_Report_Click:
          Exit Sub
          
          Err_Print_Recruitment_Report_Click:
          MsgBox Err.Description
          Resume Exit_Print_Recruitment_Report_Click
          
          End Sub

          Comment

          • Denburt
            Recognized Expert Top Contributor
            • Mar 2007
            • 1356

            #6
            Is everything working as you would like now?

            If so that is awesome glad we could help nice work sierra7.

            Comment

            • sierra7
              Recognized Expert Contributor
              • Sep 2007
              • 446

              #7
              Hi
              Well let's hope it works OK.

              What I forgot to mention was that the properties of the fields in the Table should be changed to be none mandatory.

              S7

              Comment

              • Ledmark
                New Member
                • Oct 2007
                • 19

                #8
                Hello again - I haven't gotten a chance to try it out yet but am wondering about the checks - do I need to write both checks for each required field? I should find someone to sit down and do it with me so I can understand what's happening. Do I put the name of the required field in where the code says Me.txtField1 or Me.txtField1.Na me?

                I will get this figured out and I'll let you know how it works.

                Thanks so much again!

                Laura


                Originally posted by sierra7
                Hi
                Well let's hope it works OK.

                What I forgot to mention was that the properties of the fields in the Table should be changed to be none mandatory.

                S7

                Comment

                • sierra7
                  Recognized Expert Contributor
                  • Sep 2007
                  • 446

                  #9
                  Hi again
                  YES, you will have to write code for each of the fields you wish to make mandatory.

                  YES, [txtField1] and [txtField2] represented the name of your text boxes and will have to be changed to your actual names.

                  NO, you don't have to do both checks if you are only checking for an entry; IsNull() will suffice.

                  However, users may type 'tba' (meaning 'to be advised') instead of sourcing valid data. It depends on your application and what you are trying to do.

                  It's quite valid to sit down with users and understand how they do the job you are trying to model in your application.

                  S7

                  Comment

                  Working...