How to stop a form from saving if txt fields are left blank??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slenish
    Contributor
    • Feb 2010
    • 283

    How to stop a form from saving if txt fields are left blank??

    What im trying to do is cut down on saving blank records. At this moment when I hit save record it just saves a whole blank record.

    What I want to do is make it so when you hit save if certain boxes do not have information typed in them a pop up box will come up telling you you are missing info.

    Also is there a way to adjust the tab button so you can tab threw all the fields but when you get to the last field it does not save the information? I want to make sure you have to hit a save button to save the information.

    Apperciate the help :)
  • slenish
    Contributor
    • Feb 2010
    • 283

    #2
    Ok after doing more searching I came across a possible answer but I am still having a problem.

    In the form BeforeUpDate field I put in this code:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.NewRecord Then
      If IsNull(Me.Combo264) Then
        Resp = MsgBox("This Record Cannot Be Saved Without Data in the Persons Name Field! Hit 'Yes' to enter this Data or 'No' To Dump This Record.", vbQuestion + vbYesNo + vbDefaultButton1, "Save Changes to Record ???")
        If Resp = vbYes Then
         Cancel = True
         ControlName.SetFocus
        Else
         Me.Undo
        End If
       End If
    End If
    End Sub
    It seems to work but when I press yes I want to update I get an error message due to the line (ControlName.Se tFocus)??

    Any ideas??

    Comment

    • TheSmileyCoder
      Recognized Expert Moderator Top Contributor
      • Dec 2009
      • 2322

      #3
      Please remember to use the [code][/code] tags provided, and read the Posting Guidelines

      Do you have a control on your form named ControlName? Is the control enabled? You haven't posted what ControlName is, neither have you posted what error message your receiving, which means we can only guess and waste your time and our time.....

      However, here is something to get you going:

      This is an example of some code I use. It will colour all the "bad/missing" fields, and give you one combined error message.
      In my Form_BeforeUpda te
      Code:
      Private Sub Form_BeforeUpdate(Cancel As Integer)
           If ValidateForm() Then
              Cancel = True
              Exit Sub
          End If
         'More code here, asking user if he wants to save and such, but omitted to prevent this post getting to long :)
      End Sub

      Code:
      Private Function ValidateForm() As Boolean
          Dim strMsg As String
          If IsNull(Me.tb_mem_Observation) Or Me.tb_mem_Observation = "" Then
              setErr Me.tb_mem_Observation, True
              strMsg = "The observation field cannot be empty" & vbNewLine
          Else
              Me.tb_mem_Observation.BorderColor = 8421504
          End If
          
          If IsNull(Me.cmb_Severity) Then
              setErr Me.cmb_Severity, True
              strMsg = strMsg & "The observation must be graded." & vbNewLine
          End If
              
          
          If Len(strMsg) > 0 Then
              ValidateForm = True
              MsgBox "You need to correct these item(s) before saving:" & vbNewLine & strMsg, vbOKOnly + vbExclamation, "Missing/incorrect data"
              
          End If
      
      End Function
      With a procedure stored in a module:
      Code:
      Public Sub setErr(ctrl As Control, Optional bPaint As Boolean = False)
          If bPaint Then
              If ctrl.BorderColor <> vbRed Then ctrl.BorderColor = vbRed
              If ctrl.BorderWidth <> 2 Then ctrl.BorderWidth = 2
              
          Else
              If ctrl.BorderColor <> 8421504 Then ctrl.BorderColor = 8421504
              If ctrl.BorderWidth <> 0 Then ctrl.BorderWidth = 0
      
          End If
      End Sub
      Finally in the Form_Current, I reset the error fields back to
      Code:
      Private Sub Form_Current()
          'Clear any error painting
          setErr Me.tb_mem_Observation, False
          setErr Me.cmb_Severity, False
      End Sub

      Comment

      • slenish
        Contributor
        • Feb 2010
        • 283

        #4
        Appercaite the answer theSmilyOne. sorry I did some of that wrong I did read the rules but did not quite understand how to post that code the right way. I actually got it to work the problem was the ControlSource line. I didnt have anything named ControlSource so i changed and it worked. The error I was getting was no record found.

        After all that work though the person i am building the data base for decided they didnt want to feature anyway so it was kind of all for nothing.

        I do apperciate the error finding code I am going to implement that in definatly will help down the road.

        Cheers mate :D

        Comment

        • TheSmileyCoder
          Recognized Expert Moderator Top Contributor
          • Dec 2009
          • 2322

          #5
          Glad to hear you figured it out. The worst enemy of a project is always the client :)

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32661

            #6
            Originally posted by slenish
            After all that work though the person i am building the data base for decided they didnt want to feature anyway so it was kind of all for nothing.
            I doubt it somehow. You've learned something, and we have a thread available for anyone else to find in a similar position. The only one who really loses out by the decision is your client ;)

            Comment

            Working...