Having trouble with my code.

You can see the values of the list in the code.

Here's my code:

If cboGender.Text <> "Male" Or "Female" Then MsgBox "Please select from the list provided.", vbExclamation, "Error": cboGender.SetFocus: cboGender.Text = "Male": Exit Sub
       If cboGender.Text = vbNullString Then MsgBox "Please select from the list provided.", vbExclamation, "Error": cboGender.SetFocus: cboGender.Text = "Male": Exit Sub
       If cboStat.Text <> "Single" Or "Married" Or "Widow" Or "Seperated" Then MsgBox "Please select from the list provided.", vbExclamation, "Error": cboStat.SetFocus: cboStat.Text = "Single": Exit Sub
       If cboStat.Text = vbNullString Then MsgBox "Please select from the list provided.", vbExclamation, "Error": cboStat.SetFocus: cboStat.Text = "Single": Exit Sub

The problem is that, even if i enter another value on my combobox except from the list provided, the error doesn't occur. It should only accept values from the list provided and doesn't accept any other values including null values.

Set your combobox "locked" property to true. That means that when ever a user tries to add more text, it is locked. It will only accept the list text as correct.

My bad, sorry ABE.

Use the following -

'Under the combo change event...

Private Sub Combo1_Change()

Combo1.Text = "Please select from the list provided."
End Sub

'This will cancel any text a user might want to try and enter...

HHhmmm, it works sir but wont allow me to close the form if i try to cancel the Add operation.

Got a fixed though, have to create a DB Table which contains the Combobox list items and refer to it.

Public Sub Check()

Dim dbX As ADODB.Connection
Set dbX = New ADODB.Connection

dbX.CursorLocation = adUseClient
dbX.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\Database.mdb"

    Dim rsX As ADODB.Recordset
    Set rsX = New ADODB.Recordset
    'If rsX.State = adStateOpen Then rsX.Close
 
    rsX.Open "Select * from Gender", dbX, adOpenStatic, adLockOptimistic
    If cboGender.Text <> rsX.Fields("Gender") Then MsgBox "Select from the list.", vbExclamation, "Error": cboGender.SetFocus: Exit Sub
    
dbX.Close

End Sub

Just call for Check :idea:

Comments
Well done!!!
This question has already been answered. Start a new discussion instead.