Validation subform (1 Viewer)

tihmir

Registered User.
Local time
Today, 14:08
Joined
May 1, 2018
Messages
226
Hi all,
I have function name mdValidateData to validate data in my subform which is
on Navigation Control - Page Index 0
On Main form I have the following codes:

Code:
Private Sub Form_Load()
  'Add controls in order
  ctls.Add Me![fm_PSD]![cboInspection]
End Sub
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    If Not DataValid2(Me, ctls) Then
        Cancel = True
    End If
End Sub
I have closeButton (on Main form) with code:
Code:
Private Sub closeButton_Click()
    
    Dim rtn As Long
    
    If DataValid2(Me, ctls) Then
        DoCmd.Close acForm, Me.Name
    Else
        rtn = MsgBox(" Select OK to Close without saving, or CANCEL to complete form.", vbOKCancel, "Validate Data")
    If rtn = vbOK Then
        DoCmd.Close acForm, Me.Name
    End If
  End If
End Sub
I have Navigation form with 5 pages. The first page (with Index 0) where is my subform fm_PSD. I need to prevent clicking on other pages if cboInspection on fm_PSD (subform on PageIndex 0) is Null
What I need to do?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:08
Joined
Feb 28, 2001
Messages
23,214
First, it is considered of questionable value to add controls to a live form. You could get a proliferation of controls if not extremely careful. USUALLY what you do is predefine the control and then disable/hide it until you are ready for its use.

If you have pages and want to prevent them from responding, the OnClick event of that page selection control is probably the best place to go. The other possibility is that to select a choice from a combo (e.g. cboInspection) there is an on-click routine available for THAT type of control, too. The combination of events is that on Form's .OnLoad event, you would disable the controls you don't want to be selected, then in the cboInspection control's .OnClick event, you would have code to enable the things you wish to allow based on the selection.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:08
Joined
Feb 19, 2002
Messages
36,974
Validation should be controlled from the BeforeUpdate event of the form that holds the data. Therefore, you don't call the validation code from the close button and you certainly cannot call the validation of form 2 from form 1 (once you leave a form, Access automatically attempts to save the record), you call it from the BeforeUpdate event of the form. In the close button, you can include code to save the record if you want although Access would execute the form's BeforeUpdate event without you specifically saving as long as the form is dirty when the form saves. Think of the Form's BeforeUpdate event as the flapper on a funnel. EVERY saved record MUST pass through this event so this is the event you need to use to control whether or not a record should be saved. you can certainly put validation code in other events, but, you will always need code in multiple events and you might still let something slip through the cracks and bad data will get saved. If you want absolute control over saving and you only want to write code in a SINGLE event, then use the event that the Access development team created specifically for this purpose. In the BeforeUpdate event, you cancel the save using --- Cancel = True and that will prevent the bad data from being saved. If the user tries to leave the form again, the event runs again and brings the user back. He can't get out without cancelling the save himself or fixing the problem.

Now to the specific problem.
Navigation forms allow only ONE subform to be loaded at any one time so subforms cannot reference other subforms and the main form can only reference the subform that is actually loaded. Therefore, the Access Navigation Form might not be the best structure for what you are doing. You could simulate the effect by just creating a regular main form with a tab control and the tab control would have a separate subform on each page. But if I am reading your problem correctly, even that won't help.

As I read your question, the subforms are "equal" and your intention is to execute the first, then the second, etc. That only works if form 2 can refer to form1, and 3 can refer to 2. A better interface is to make it hierarchical. When the first part is done, then and only then allow the second part to be opened, etc.
 

Users who are viewing this thread

Top Bottom