If textbox on sub form is null then... (1 Viewer)

WebFaktor

Registered User.
Local time
Today, 20:18
Joined
May 12, 2001
Messages
81
I have a data entry form called FORM_A

It contains a data entry subform called FORM_B and is recognized by FORM_A as SUBFORM_B

SUBFORM_B, or FORM_B contains a textbox called TEXTBOX_A

Problem: My command button, CmdSave, to save the data entry made in both FORM_A and FORM_B, or SUBFORM_B is located on FORM_A. What I'm trying to achieve is preventing the saving of data from occuring if TEXTBOX_A on SUBFORM_B, or FORM_B is Null.

More specifically, I can't figure out the code necessary to have this command button , CmdSave, located on my main form, FORM_A check a textbox, TEXTBOX_A, located on its subform FORM_B, or SUBFORM_B to determine if it's Null.

I keep getting error messages such as "Can't find FORM_B, or SUBFORM_B."

I've done a good search through the archives here and I'm unable to discover how to code a situation like this???


Thank you, in advance.
 
J

Jerry Stoner

Guest
Modify this to suit your needs

If IsNull(Me.SUBFORM_B.Form.Controls(TEXTBOX_A)) Then
MsgBox "YourMessageHere"
Else
YourSaveCodeHere
End If
 

WebFaktor

Registered User.
Local time
Today, 20:18
Joined
May 12, 2001
Messages
81
Jerry,

Thanks for sharing the wealth!

I've implemented your direction as such - modifying my actual form and field names to fit and I get the error message of "Variable not defined" with regard to - MaterialName?

If IsNull(Me.[Ctlg_E_MaterialsRef sfrm].Form.Controls(MaterialName)) Then
Exit Sub
MsgBox "Enter Training Material Name..."

Else
 
J

Jerry Stoner

Guest
Try this:
If IsNull([Ctlg_E_MaterialsRef sfrm].Form!(MaterialName)) Then
MsgBox "Enter Training Material Name..."
Else
SaveCodeHere
End Sub
 

WebFaktor

Registered User.
Local time
Today, 20:18
Joined
May 12, 2001
Messages
81
Still getting error???

I tryed new code... and am still getting error message "Variable not defined" with regard to - MaterialName?
 

WebFaktor

Registered User.
Local time
Today, 20:18
Joined
May 12, 2001
Messages
81
Discovered correct code...

This code here did what I needed.


Private Sub Command6_Click()
If IsNull([MaterialSfrm].[Form]![MaterialName]) Then
MsgBox "Enter Training Material Name, or select cancel button..."
Exit Sub

Else
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:18
Joined
Feb 19, 2002
Messages
43,371
You may be getting the error message but the code is most definitely NOT doing what you want. Access is automaitcally saving the record from Form_A when the cursor is moved to Form_B. This is by design and is NECESSARY to enforce referential integrity. The one-side record MUST exist before any related many-side records may be added. Then when you click the Save button on Form_A, that causes the subform to automatically save the subform record BEFORE your button's click event is executed So, even though you get an error message, BOTH records have already been saved. You have done nothing to prevent the automatic saving implemented by Access.

If MaterialName is required, modify the table design to insure that a record CANNOT be saved unless this field has a value. If you want to produce your own error message, then put the following code in the subform's BeforeUpdate event:

If IsNull(Me.MaterialName) Then
MsgBox "Training Material Name is required", vbOKOnly
Me.MaterialName.SetFocus
Cancel = True
End If

The above code will prevent Access from attempting to save the record by cancelling the update event.
 

Users who are viewing this thread

Top Bottom