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.

You need to look at the format of your IF statements. They are malformed.

Hhhmm, setting it to True would disable selecting from the list also..

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:

commented: Well done!!! +6

Well done.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.