If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Force users to enter a value in a Text box
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 |
#2
|
|||
|
|||
Force users to enter a value in a Text box
move your txtTS_Note_LostFocus() script to txtTS_Note_Exit event:
Private Sub txtTS_Note_Exit(Cancel As Integer) 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 Cancel = True Exit Sub End If End Sub NJ wrote: 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 -- spread the WORD Message posted via http://www.accessmonster.com |
#3
|
|||
|
|||
Force users to enter a value in a Text box
On Wed, 21 Oct 2009 08:04:12 -0700, NJ wrote:
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 -- John W. Vinson [MVP] |
Thread Tools | |
Display Modes | |
|
|