Make form control value required with VBA (1 Viewer)

Zydeceltico

Registered User.
Local time
Yesterday, 19:33
Joined
Dec 5, 2017
Messages
843
Hi All -

I have a form with some controls. The nature of this form is such that it is always opened twice. Once to record an initial Start time (=Now) and later to record the Stop time.

I have another control called ScrapProduced that takes a number.

I don't want the user to leave that field blank. I made it required at the table level but there is no scrap produced when the user visits the form the first time.
Only on the second visit can the user know how much scrap was produced and be able to enter a number in that control.

Is there a way to enable/disable the Required property of a field contingent on a value being placed in a different control on the form? In this case, it would be when the user enters a Stop time.

Thanks as always,

Tim
 

Micron

AWF VIP
Local time
Yesterday, 19:33
Joined
Oct 20, 2018
Messages
3,478
AfterUpdate of stop time - make scrap field visible. When to trap no scrap entry depends.
Form unload event, check if null or "", cancel form unload if it is, present message?
Button click that saves record?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:33
Joined
Oct 29, 2018
Messages
21,467
Hi Tim. If it's possible to enter partial records, then the field is not really required, or at least it shouldn't be. You can always force an entry in said field in the BeforeUpdate of the form that goes something like, if Stop time is not empty, then Scrap Produced must not be empty also.
 

Zydeceltico

Registered User.
Local time
Yesterday, 19:33
Joined
Dec 5, 2017
Messages
843
Hi Tim. If it's possible to enter partial records, then the field is not really required, or at least it shouldn't be.

That....and the rest of your response......makes sense.

Thx!
 

Zydeceltico

Registered User.
Local time
Yesterday, 19:33
Joined
Dec 5, 2017
Messages
843
Hi Tim. If it's possible to enter partial records, then the field is not really required, or at least it shouldn't be. You can always force an entry in said field in the BeforeUpdate of the form that goes something like, if Stop time is not empty, then Scrap Produced must not be empty also.

What is the difference between using the Nz function versus testing for Null?

What is the functional difference between:

If Me.txtBox Is Not Null Then...... (this appears to compile btw)

or

If Me.txtBox Not IsNull Then......

or

If Nz(Me.txtBox,"") &""<>"" Then
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:33
Joined
Feb 28, 2001
Messages
27,172
The function difference between a test for Null and using NZ is that NZ allows you to "kill two birds with one stone."

Ask yourself this question: If you test for something using If xxx Is Null, what is the next thing you would do?

If you next would do something complex involving message boxes or GoTo statements or some other sequence of logic, then your use of Is Null was probably the right choice.

BUT if your next action would merely be to substitute a zero or a blank (or any other simple value) in place of the null, NZ() does exactly that in one quick call.

Watch out for "IsNull" (one word) because that is a function. Whereas "Is Null" (two words) is a special-syntax test for something being null.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:33
Joined
Oct 29, 2018
Messages
21,467
Hi Tim. What I would say is you'll need to know there are two kinds of "empty" data (for Text data anyway). Null means "unknown." Data is either missing or intentionally left blank. For example, if you have a Middle Name field and a person has a blank MI column, does it mean he/she doesn't have one or the data entry person forgot to put it in? The other type is called a "Zero Length String" or ZLS for short. A ZLS is not null but it doesn't have a length either (or perhaps more accurately, it has a length of zero). So, if you are checking a Text field if it's empty, you'll have to be aware it may be Null or it may be ZLS. The IsNull(), Nz(), and Is Null checks handle Nulls. They don't work with ZLS. ZLS can be checked using an empty string ("") or the Len() function.
 

Zydeceltico

Registered User.
Local time
Yesterday, 19:33
Joined
Dec 5, 2017
Messages
843
Thank you all. Signing off for the night. I've been trying variations of Is Null on the BeforeUpdate and Unload events - - -with some degree of success and also some unexpected errors. I'll post more tomorrow after sleeping on it. :)

Thank You - as always!

Tim
 

Micron

AWF VIP
Local time
Yesterday, 19:33
Joined
Oct 20, 2018
Messages
3,478
What is the difference between using the Nz function versus testing for Null?
Nz is a conversion function. You could use it to convert Null and do some sort of test on the result (or not) but I wouldn't call it a test for Null in of itself - at least not the way I think you mean. Sure, it's a kind of test in that if Null, a conversion takes place but by itself reports nothing.

The basic difference between Is Null and IsNull is that the former is query syntax, the latter is vba. Both are a test for Null but I'm pretty sure you can't interchange them.
If Me.txtBox Is Not Null Then...... (this appears to compile btw)
There should be something wrong with your statement.
If Is Null(Forms!frmAvgData.ID) Then MsgBox "is Null" is red text for me. So is
If Is Null Forms!frmAvgData.ID Then MsgBox "is Null"

If IsNull(Forms!frmAvgData.ID) Then MsgBox "is Null" is not red. Red, of course, meaning it won't compile.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 00:33
Joined
Jan 14, 2017
Messages
18,216
Hi Tim
Micron and the DBG have discussed the different methods of handling nulls. If you are interested, I ran some tests comparing the efficiency of three methods: Nz, Len and Trim. See http://www.mendipdatasystems.co.uk/speed-comparison-tests/4594424200.
For info, Nz was marginally the best approach is my tests.

Back in post #1, you mentioned that the form is opened twice. Once to populate the start time and again for a stop time. Why do you close the form? If not needed in the interim, why not leave it open but hidden? Then you can return to the same record on the form at any time.
Or why not have the timing done on the same form as the other code to simplify matters further?
 

Users who are viewing this thread

Top Bottom