A Microsoft Office (Excel, Word) forum. OfficeFrustration

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.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Force users to enter a value in a Text box



 
 
Thread Tools Display Modes
  #1  
Old October 21st, 2009, 04:04 PM posted to microsoft.public.access.forms
NJ
external usenet poster
 
Posts: 29
Default 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  
Old October 21st, 2009, 08:44 PM posted to microsoft.public.access.forms
NuBie via AccessMonster.com
external usenet poster
 
Posts: 67
Default 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  
Old October 22nd, 2009, 01:59 AM posted to microsoft.public.access.forms
John W. Vinson
external usenet poster
 
Posts: 18,261
Default 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 02:16 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.