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  

Require Field if another field is not blank



 
 
Thread Tools Display Modes
  #1  
Old November 5th, 2009, 04:03 PM posted to microsoft.public.access.forms
OsmoseTom
external usenet poster
 
Posts: 5
Default Require Field if another field is not blank

I am having some troble with my code within a form. What I am trying to do
is diplay a message box and require entry for a field when another field has
an amount greater than zero.

Here is my code that is not working.


Private Sub Sun_Hours_1_Exit(Cancel As Integer)

Dim strMsg As String
If (Me.[Sun PR OUS 1] = "" And Me.[Sunday Hours 1] 0) Then
Beep
MsgBox "If hours are entered for sunday a corresponding PR OUS job number is
required.", vbOKOnly, "Entry Required"
Cancel = True
End If

End Sub

Can someone please help me?

  #2  
Old November 5th, 2009, 04:42 PM posted to microsoft.public.access.forms
Tom Lake[_2_]
external usenet poster
 
Posts: 96
Default Require Field if another field is not blank


"OsmoseTom" wrote in message
...
I am having some troble with my code within a form. What I am trying to do
is diplay a message box and require entry for a field when another field has
an amount greater than zero.

Here is my code that is not working.


Private Sub Sun_Hours_1_Exit(Cancel As Integer)

Dim strMsg As String
If (Me.[Sun PR OUS 1] = "" And Me.[Sunday Hours 1] 0) Then
Beep
MsgBox "If hours are entered for sunday a corresponding PR OUS job number is
required.", vbOKOnly, "Entry Required"
Cancel = True
End If

End Sub

Can someone please help me?


Try this:

If (IsNull(Me.[Sun PR OUS 1]) And Me.[Sunday Hours 1] 0) Then

Tom Lake

  #3  
Old November 5th, 2009, 05:23 PM posted to microsoft.public.access.forms
Linq Adams via AccessMonster.com
external usenet poster
 
Posts: 1,474
Default Require Field if another field is not blank

Along with Tom's suggestion, if you're going to leave this in the

Sun_Hours_1_Exit

event, you'll have to remove the

Cancel = True

line! If you don't, you will not be able to move to the

Sun PR OUS 1

textbox to enter the needed data! Focus will remain in the

Sunday Hours 1

textbox as long as it is greater the 0 and

Sun PR OUS 1

is empty!

In point of fact, this type of Validation code should really be in the
Form_BeforeUpdate event. That way your user will only be warned if they try
to save the record with the conditions

IsNull(Me.[Sun PR OUS 1]) And Me.[Sunday Hours 1] 0

being true. Your users may simply wnat to enter the

[Sun PR OUS 1]

data after entering Sundy's hours.

In this case, with the code being in the Form_BeforeUpdate event, it would
then be appropriate to have the

Cancel = True

line, to cancel the Save until the correction is made.

--
Message posted via http://www.accessmonster.com

 




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 03: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.