Results 1 to 2 of 2
  1. #1
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152

    Validation Button

    Hello.



    I have the following code which runs upon the click of a button.
    It looks at the repsonses made within the form and checks for errors.
    If errors are found then it highlights them red and focuses on them.

    The code does work, but it just looks a bit messy and long winded, especially given there are probably another 100+ IF checks to be added in.

    Also I am having trouble with the following part not working if anything other than none is selected in the combobox.

    Code:
    If cboEntranceDoorsFlats.Value <> "None" And Len(Me.txtEntranceDoorsFlatsQuant & "") = 0 Then
        Me.txtEntranceDoorsFlatsQuant.BackColor = vbRed
        Me.txtEntranceDoorsFlatsQuant.SetFocus
    But heres an example of the full code as it is now.
    Code:
    Private Sub btnCompleteSurvey_Click()
    If IsNull(cboSurveyor.Value) Then
        Me.cboSurveyor.BackColor = vbRed
        Me.cboSurveyor.SetFocus
    If IsNull(txtSurveyDate.Value) Then
        Me.txtSurveyDate.BackColor = vbRed
        Me.txtSurveyDate.SetFocus
    If IsNull(cboNumberofBedrooms.Value) Then
        Me.cboNumberofBedrooms.BackColor = vbRed
        Me.cboNumberofBedrooms.SetFocus
    If IsNull(cboNumberofBedSpaces.Value) Then
        Me.cboNumberofBedSpaces.BackColor = vbRed
        Me.cboNumberofBedSpaces.SetFocus
    If IsNull(cboNumberofHabitableRooms.Value) Then
        Me.cboNumberofHabitableRooms.BackColor = vbRed
        Me.cboNumberofHabitableRooms.SetFocus
    If IsNull(cboNumberofHeatedRooms.Value) Then
        Me.cboNumberofHeatedRooms.BackColor = vbRed
        Me.cboNumberofHeatedRooms.SetFocus
    If Len(Me.txtGroundfloorarea & "") = 0 Then
        Me.txtGroundfloorarea.BackColor = vbRed
        Me.txtGroundfloorarea.SetFocus
    If Len(Me.txtRoomheight & "") = 0 Then
        Me.txtRoomheight.BackColor = vbRed
        Me.txtRoomheight.SetFocus
    If IsNull(cboEntranceDoorsFlats.Value) Then
        Me.cboEntranceDoorsFlats.BackColor = vbRed
        Me.cboEntranceDoorsFlats.SetFocus
    If cboEntranceDoorsFlats.Value <> "None" And Len(Me.txtEntranceDoorsFlatsQuant & "") = 0 Then
        Me.txtEntranceDoorsFlatsQuant.BackColor = vbRed
        Me.txtEntranceDoorsFlatsQuant.SetFocus
    If cboEntranceDoorsFlats.Value <> "None" And Len(Me.txtEntranceDoorsFlatsRenewYear & "") = 0 Then
        Me.txtEntranceDoorsFlatsRenewYear.BackColor = vbRed
        Me.txtEntranceDoorsFlatsRenewYear.SetFocus
          
    MsgBox ("Survey contains Missing or Incorrect Data! Please complete all fields in RED and click Complete Survey!")
    Me.txtSurveyStatus = "Incomplete"
    Me.txtSurveyStatus.ForeColor = vbRed
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    If Me.cboSurveyor.Value <> "" Then
    If Me.txtSurveyDate.Value <> "" Then
    If Me.cboNumberofBedrooms.Value <> "" Then
    If Me.cboNumberofBedSpaces.Value <> "" Then
    If Me.cboNumberofHabitableRooms.Value <> "" Then
    If Me.cboNumberofHeatedRooms.Value <> "" Then
    If Me.txtGroundfloorarea.Value <> "" Then
    If Me.txtRoomheight.Value <> "" Then
    If Me.cboEntranceDoorsFlats.Value <> "" Then
    If Me.txtEntranceDoorsFlatsQuant.Value <> "" Then
        MsgBox "Survey Complete Successfully"
        Me.txtSurveyStatus = "Complete"
        Me.txtSurveyStatus.ForeColor = vbGreen
        Me.txtSurveyStatus.SetFocus
    MsgBox ("Survey contains Missing or Incorrect Data! Please complete all fields in RED and click Complete Survey!")
    Me.txtSurveyStatus = "Incomplete"
    Me.txtSurveyStatus.ForeColor = vbRed
        
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End Sub
    Help much appreciated.

  2. #2
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    I have now changed the code to and everything works great on it!
    However it only highlights one error at a time! Can this be changed other than reverting back to the code above?

    The second issue now is the first msgbox which states to fill in all red highlighted errors does not appear.

    Does anyone have any ideas?

    Code:
    Private Sub btnCompleteSurvey_Click()
    If IsNull(cboSurveyor.Value) Then
        Me.cboSurveyor.BackColor = vbRed
        Me.cboSurveyor.SetFocus
    ElseIf IsNull(txtSurveyDate.Value) Then
        Me.txtSurveyDate.BackColor = vbRed
        Me.txtSurveyDate.SetFocus
    ElseIf IsNull(cboNumberofBedrooms.Value) Then
        Me.cboNumberofBedrooms.BackColor = vbRed
        Me.cboNumberofBedrooms.SetFocus
    ElseIf IsNull(cboNumberofBedSpaces.Value) Then
        Me.cboNumberofBedSpaces.BackColor = vbRed
        Me.cboNumberofBedSpaces.SetFocus
    ElseIf IsNull(cboNumberofHabitableRooms.Value) Then
        Me.cboNumberofHabitableRooms.BackColor = vbRed
        Me.cboNumberofHabitableRooms.SetFocus
    ElseIf IsNull(cboNumberofHeatedRooms.Value) Then
        Me.cboNumberofHeatedRooms.BackColor = vbRed
        Me.cboNumberofHeatedRooms.SetFocus
    ElseIf Len(Me.txtGroundfloorarea & "") = 0 Then
        Me.txtGroundfloorarea.BackColor = vbRed
        Me.txtGroundfloorarea.SetFocus
    ElseIf Len(Me.txtRoomheight & "") = 0 Then
        Me.txtRoomheight.BackColor = vbRed
        Me.txtRoomheight.SetFocus
    ElseIf IsNull(cboEntranceDoorsFlats.Value) Then
        Me.cboEntranceDoorsFlats.BackColor = vbRed
        Me.cboEntranceDoorsFlats.SetFocus
    ElseIf cboEntranceDoorsFlats.Value <> "None" And Len(Me.txtEntranceDoorsFlatsQuant & "") = 0 Then
        Me.txtEntranceDoorsFlatsQuant.BackColor = vbRed
        Me.txtEntranceDoorsFlatsQuant.SetFocus
    ElseIf cboEntranceDoorsFlats.Value <> "None" And Len(Me.txtEntranceDoorsFlatsRenewYear & "") = 0 Then
        Me.txtEntranceDoorsFlatsRenewYear.BackColor = vbRed
        Me.txtEntranceDoorsFlatsRenewYear.SetFocus
     
    MsgBox ("Survey contains Missing or Incorrect Data! Please complete all fields in RED and click Complete Survey!")
    Me.txtSurveyStatus = "Incomplete"
    Me.txtSurveyStatus.ForeColor = vbRed
    Else
        MsgBox "Survey Complete Successfully"
        Me.txtSurveyStatus = "Complete"
        Me.txtSurveyStatus.ForeColor = vbGreen
        Me.txtSurveyStatus.SetFocus
     
    End If
    End Sub

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

Similar Threads

  1. not null validation
    By kkukreja in forum Forms
    Replies: 7
    Last Post: 01-15-2011, 03:34 PM
  2. Validation Rule
    By Christopher in forum Forms
    Replies: 15
    Last Post: 04-20-2010, 06:05 AM
  3. password validation
    By RycherX in forum Forms
    Replies: 1
    Last Post: 02-20-2010, 03:55 PM
  4. Replies: 6
    Last Post: 02-09-2010, 07:53 AM
  5. Data Validation - Please help
    By larry S in forum Access
    Replies: 0
    Last Post: 11-16-2008, 10:24 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