Results 1 to 9 of 9
  1. #1
    thebionicredneck2003 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2013
    Posts
    22

    Data Validation issue on a continuous form

    Hi everyone,

    I would like to find out if someone has encountered this problem before or if someone knows how to solve the problem I have.

    I have a continuous subform that displays lots of questions. This specific form has 10 questions on it. Each question has one answer and the users are required to select one answer for each question. I am trying to do some data validation before the data gets saved to my table to ensure that users have answered all the questions. I found some very useful code online and tweaked it to suit my needs. The validation works perfectly on subforms with only one question, as I have some other tabs that use it.

    On the specific form that has the issue, the validation works as expected when no question has been answered, but when a user answers only one question and leaves the remaining nine questions blank, the validation does not work. I guess it is as a result of the continuous form repeating the different questions and ms access thinking that the data entered for one question alone is complete when it actually isn't.

    I would be happy to hear from anyone who has any ideas or who has overcome this problem before. I have included the code I used below

    Code:
    Private Sub Ctl4_frm_Staff_Exit(Cancel As Integer)
    Dim ctrl As Control, EmptyStr$
       For Each ctrl In Form_subFrm_staffQuestion.Controls
        If InStr(1, ctrl.Tag, "Required") > 0 Then
           If IsNull(ctrl.Value) Or ctrl.Value = vbNullString Or Len(ctrl.Value) = 0 Then
             ctrl.SetFocus
             EmptyStr = EmptyStr & ctrl.Name & ";"
             ctrl.BorderColor = vbRed
          End If
        End If
      Next
      If EmptyStr <> vbNullString Then
        EmptyStr = Left(EmptyStr, Len(EmptyStr) - 1)
        MsgBox "Mandatory fields incomplete: " & vbCrLf & EmptyStr & vbCrLf & "Please complete them before saving!", vbCritical, "Staff Form"
        Cancel = True
      End If
    End Sub
    Many thanks

  2. #2
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    This worked for me.

    Code:
    Private Sub Ctl4_frm_Staff_Exit(Cancel As Integer)
    Dim ctrl As Control
    Dim EmptyStr As String
       For Each ctrl In Forms!Form1
            'Debug.Print ctrl.Name
            If IsNull(ctrl) Then
                ctrl.SetFocus
                'Debug.Print ctrl.Name
                MsgBox "This control is required", vbOKOnly
            End If
      Next
      If EmptyStr <> vbNullString Then
        EmptyStr = Left(EmptyStr, Len(EmptyStr) - 1)
        MsgBox "Mandatory fields incomplete: " & vbCrLf & EmptyStr & vbCrLf & "Please complete them before saving!", vbCritical, "Staff Form"
        'Cancel = True
      End If
    
    End Sub
    Change the form name in the code to your form name.

    Dale

  3. #3
    thebionicredneck2003 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2013
    Posts
    22
    Thanks for the reply. I will try it out and will let you know how it goes

  4. #4
    thebionicredneck2003 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2013
    Posts
    22
    Many thanks for your solution as it works in part. My subform has two questions on one row

    i.e Question 1 - Answer Comment
    Question 2 - Answer Comment
    Question 3 - Answer Comment

    and so on.

    The code works fine if there is only a question and an answer, but because there is also a comment control, the validation does not work really well as the focus always goes to the comment column after a value for an answer is selected. If the cursor is on answer 2, then the validation works if you try to save or exit.

    I put this bit to force the cursor to move to the next answer

    DoCmd.GoToRecord acActiveDataObject, , acNext

    It works, but when it reaches the last record answer in the subform, it throws up an error message.

    Just wondering if you have any more useful pointers to help me out.


    Many thanks

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Your explanation is somewhat confusing, but if I understand your intent and you're validating more that one Control, and doing it by looping through all Controls, you need to exit the sub each time you find a Control that needs populating, so that the code stops looping. So whereas you had

    Code:
    If IsNull(ctrl) Then
        ctrl.SetFocus
        'Debug.Print ctrl.Name
        MsgBox "This control is required", vbOKOnly
     End If


    you need

    Code:
    If IsNull(ctrl) Then
        ctrl.SetFocus
        'Debug.Print ctrl.Name
        MsgBox "This control is required", vbOKOnly
        Exit Sub
     End If


    This temporarily stops the looping, allowing you to fill in the empty Control. Then when you try leaving the Ctl4_frm_Staff, again, it restarts the loop, stopping at the next empty Control, and repeats until all empty Controls have been handled.

    Linq ;0)>

  6. #6
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    Thanks Linq. I missed that.

    Dale

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    It's easy to do! Don't need it, of course, if you're only validating one Control, but with more than one Control, if you don't exit the sub, it just keeps running through all of them! Because of that, I usually include the command in all of my validation code; doesn't hurt anything and it's one less thing for me to have to think about!

    Linq ;0)>

  8. #8
    thebionicredneck2003 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2013
    Posts
    22
    Thank you both for your patience and help so far. I apologise for not being to articulate my problem well enough, as I basically inherited this from someone else and I have only recently started using MS Access in this capacity and I am just coming to terms with it.

    I have attached two images. Hopefully this will help to articulate the problem better. I will also try the solutions you have posted today and will let you know how it goes.


    Many thanks once again for your contributions and your patience.
    Attached Thumbnails Attached Thumbnails Section A.png   Section C.png  

  9. #9
    thebionicredneck2003 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2013
    Posts
    22
    Just to say that with some help I found a way to fix my problem using DAO.Recordset and a loop. Thanks to everyone that has contributed in some way to helping me resolve my problem

    Regards

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 02-17-2013, 07:11 AM
  2. Data entry validation rule in Form
    By accessn00bie in forum Access
    Replies: 3
    Last Post: 12-07-2012, 01:11 PM
  3. Replies: 4
    Last Post: 05-24-2011, 08:19 AM
  4. Replies: 2
    Last Post: 03-29-2011, 01:26 PM
  5. Replies: 6
    Last Post: 07-21-2010, 05:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums