Force users to enter a value in a Text 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 a 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
 
J

John W. Vinson

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 a 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

To really ensure validity you should put the code in the Form (not any
control's) BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel as Integer)

If Me!cboTD = 0 Then ' use "0" if cboTD's control source is a Text field
If Len(Me!txtTS_Note) = 0 ' fastest way to check for empty control
MsgBox "Please fill in a note", vbOKOnly
Cancel = True
Me!txtTS_Note.SetFocus
End If
End If
End Sub
 

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