Force the users to enter value in a Text box based on a Combo Box

N

NJ

Hi,

I have a form which the combo box control that has (0 & 1) and text box
assocaited for the combo box; my question is that; if users select 0 value
from the combo box; then they should be forced to enter some value in the
Text box; currently I have the code at two places as below: but for some
reason; I am not able to force the cursor not to go to the next
control....and should pop-up with the message until some value is entered in
the Text box ....

Private Sub cboTD_AfterUpdate()
If cboTD = "0" And (txtTS_Note = "" Or IsNull(txtTS_Note)) Then
txtTS_Note.SetFocus
txtTS_Note.BackColor = vbRed
Else
If (cboTD = "1" Or cboTD = "N/A") And (txtTS_Note = "" Or
IsNull(txtTS_Note)) Then
txtTS_Note.BackColor = vbWhite
End If
End If
End Sub



Private Sub txtTS_Note_LostFocus()
If cboTD = "0" And (txtTS_Note = "" Or IsNull(txtTS_Note)) Then
txtTS_Note.BackColor = vbRed
MsgBox ("Please enter a value for the TS Notes field")
Me.txtTS_Note.SetFocus
Exit Sub
End If
End Sub
 
B

Beetle

You only need to put the code in one place - the Before
Update event of your form. For example;

Private Sub Form_BeforeUpdate (Cancel As Integer)

If cboTD = "0" And Nz(txtTS_Note, "") = "" Then
Cancel = True
MsgBox "Please enter a value in the TS Notes Field"
txtTS_Note.SetFocus
txtTS_Note.BackColor = vbRed
Else
txtTS_Note.BackColor = vbWhite
End If

End Sub


Validation code like this should always be done in the Before
Update event of the form itself, so you can cancel the update
and force the user to remain on the current record until all
fields are correctly filled in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top