Form field conditionally required, based on selection in sub-form field (1 Viewer)

HGCanada

Registered User.
Local time
Today, 17:13
Joined
Dec 30, 2016
Messages
82
I have a database with a form, and a subform. There's a numeric "status" field on the subform, with 6 options. The subform is continuous, and each of the 6 status options may be logged, for each records. Each time one of the 6 stati are logged on the subform, there is a numeric field on the form, which should become required.

So, for example:
- If subform field "status" = 1 is logged, then form "Field1" cannot be null
- If subform field "status" = 2 is logged, then form "Field2" cannot be null
etc.

And vice versa:
- If main form "Field1" is completed, then subform "status" = 1 must be logged.
etc

Can anybody point me to some code for this? I've found several versions online, but nothing works. I think it's related to the fact that the 2 fields in the code are on different forms, and I get a "member not recognized" error message.

Thanks.
 

isladogs

MVP / VIP
Local time
Today, 22:13
Joined
Jan 14, 2017
Messages
18,212
So, for example:
- If subform field "status" = 1 is logged, then form "Field1" cannot be null
- If subform field "status" = 2 is logged, then form "Field2" cannot be null
etc.

And vice versa:
- If main form "Field1" is completed, then subform "status" = 1 must be logged.
etc

Taking the first part of this, do you mean
status =1 can't be logged unless field1 has already been completed

OR
after logging the status, the field must then be completed

I ask because potentially the first and second condition together will make it impossible for Access to proceed
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:13
Joined
May 7, 2009
Messages
19,230
you need to code the BeforeUpdate event of the "status" field
your Form and SubForm.

on the "Form":
Code:
Private Sub Status_BeforeUpdate(Cancel As Integer)
If Trim(Me.Status & "")<>"" Then 
	Me.Subform.Form!Status=1
	Me.Subform.Form.Dirty=False
End If
End Sub

on the "SubForm:"

Code:
Private Sub Status_BeforeUpdate(Cancel As Integer)
If Trim(Me.Status & "") <> "" Then
	If Me.Status = 1 Then
		' Parent form "field1" cannot be blank
		If Trim(Me.Parent!Field1 & "")="" Then
			MsgBox "Field1 on the main form must not be blank"
			Cancel = True
			Me.Status.Undo
		End If
	ElseIf Me.Status = 2
		' Parent form "field2" cannot be blank
		If Trim(Me.Parent!Field2 & "")="" Then
			MsgBox "Field2 on the main form must not be blank"
			Cancel = True
			Me.Status.Undo
		End If
	End If
End If
End Sub
 

HGCanada

Registered User.
Local time
Today, 17:13
Joined
Dec 30, 2016
Messages
82
Thank you both.

Ridders - it's more the second option - after logging the status on the subform, the field on the form must then be completed. Actually, it doesn't matter which order they are completed in, but just if one is completed, then the other must also be completed. If I need to force an order, that's fine. I can just train the database users to enter in that order.

arnelgp - thank you. I've tried applying the code, and keep getting "Compile error: Method or data member not found" on the subform "status" field.

Also, can you please clarify - in the code you posted, it looks like on the form, I'm supposed to put the event code on the "status" field. But on the form, there's only a "field1" field. Is this where I'm supposed to put the BeforeUpdate event on the form? The "status" field exists only on the subform.

Thanks so much.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:13
Joined
May 7, 2009
Messages
19,230
My mistake, it's field1.
 

Users who are viewing this thread

Top Bottom