Results 1 to 2 of 2
  1. #1
    dr223 is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2010
    Posts
    20

    Validation of the comboboxes

    Hi,



    I have the following code which worked fine before I entered the highlighted if statements to validate.

    What i want is before any procedure takes place the first process is to check if all the combo boxes are populated/have value. Therefore, if if any is not a message box is prompted to ask the user to select a choice for that question.

    Incase any of the combo boxes is not filled then the sub terminates..

    any help please.. thank you

    Code:
    Private Sub Submit_Click()
    
    If (cmbcri1.Value) = 0 Then
      MsgBox "Please answer Question1"
       End If
      
      If (cmbcri2.Value) = 0 Then
       MsgBox "Please answer Question2"
         End If
      
       If (cmbcri3.Value) = 0 Then
       MsgBox "Please answer Question3"
        End If
       
        If (cmbcri4.Value) = 0 Then
       MsgBox "Please answer Question4"
       
        End If
      
      Exit Sub
          
    DoCmd.RunSQL "DELETE TblTemp.* FROM TblTemp"
    DoCmd.GoToRecord , , acNewRec
    DoCmd.RunSQL "INSERT INTO TblTemp(Que1W,Que2W, Que3W,Que4W) VALUES ('" & cmbcri1 & "','" & cmbcri2 & "','" & cmbcri3 & "','" & cmbcri4 & "');", dbSeeChanges + dbFailOnError
    DoCmd.OpenQuery "QryAppendTblDetails"
    'MsgBox ("Record saved")
    Dim rs As DAO.Recordset, strMessage
    Set rs = CurrentDb.OpenRecordset("QryWSMScores")
    If Not rs.EOF Then
     strMessage = Chr(13) & Chr(10)
     rs.MoveFirst
     Do Until rs.EOF
      strMessage = strMessage & Space(20) & rs.Fields(0) & Chr(13) & Chr(10)
      rs.MoveNext
     Loop
        MsgBox " Business Options Based on WSM Score " & strMessage
                                                    
    End If
    cmbcri1.Value = 0
    cmbcri2.Value = 0
    cmbcri3.Value = 0
    cmbcri4.Value = 0
     
    End Sub

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Actually you want to check to see if the combo box is null (nothing selected) not whether it is =0. If there is no value selected, then you might want your code to go back to that particular combo box directly, so the user does not have to figure out which combo box they missed. Then exit the procedure.

    Here is a code snippet

    Code:
    If IsNull(Me.cboCounselor) Then
        MsgBox "You must specify the person filling out this form"
        Me.cboCounselor.SetFocus
        Exit Sub
    End If
    If have also used the nz() function in place of the IsNull() function at times, that would look like this:

    If nz(me.cboCounselor,"") Then

    BTW the me. is a shorthand for the current form, and you do not have to specify .value, it is implied.

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

Similar Threads

  1. Cascade of 3 or more ComboBoxes
    By tomullus in forum Forms
    Replies: 4
    Last Post: 10-08-2011, 06:22 PM
  2. Comboboxes go where?
    By PaulCW in forum Database Design
    Replies: 12
    Last Post: 10-04-2011, 02:34 AM
  3. Code for two comboBoxes
    By t_dot in forum Forms
    Replies: 3
    Last Post: 08-24-2010, 10:20 AM
  4. Subform with Comboboxes
    By Angate in forum Forms
    Replies: 5
    Last Post: 04-23-2010, 08:10 PM
  5. How to Reset Comboboxes
    By bbarrene in forum Programming
    Replies: 5
    Last Post: 01-23-2010, 11:11 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