Solved

do not allow subform entry until fiels in parent are filled

Posted on 2012-12-28
7
647 Views
Last Modified: 2013-01-09
Hi.  I am trying to make controls in my subform or the subform as a whole, inactive until specific controls on my main form are filled. I've tried a number of things and am having no luck.  I figured the easiest would be to use the gotfocus property of a control on my subform and if parent control is null then setfocus back to the parent control and isue a message to the user.  The logic works but as soon as I open my parent form the message I'm sending my user pops up and it doesn't matter which subform control I wrap this code around either.  I'm confused as to why my subform controls all seem active when I open the parent form.  Is there a way to keep the subform from being active until the user manually activates it?  What's the best way to keep users from entering data into a subform until they have properly filled the parent form? I am including code from my subform.
Private Sub ExpDate_GotFocus()
If IsNull(Me.Parent!TripFinish) Then
    MsgBox "Fill in the Trip Detail above before entering Expense data.", , "ERROR"
    Me.Parent!TripPurpose.SetFocus
End If
End Sub

Open in new window


parent form = frmTripDetail
parent form control = TripFinish
Subform = subfrmExpDetail
subform control = ExpDate
Key field = TripID

thanks
0
Comment
Question by:valmatic
7 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38728296
To spare you the never ending debate, ...

You can validate the fields in the Before Update event of the main form.

Trying to do this "On-the-fly", while in the field is always problematic.

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If isnull(me.YourField) then
        msgbox "You must fill in YourField."
        me.YourField.stefocus
    End if
    Cancel=True
End Sub

But lets see what other experts may post...

;-)


jeff
0
 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 125 total points
ID: 38728300
Slight code corrections:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.YourField) Then
        MsgBox "You must fill in YourField."
        Me.YourField.SetFocus
        Cancel = True
    End If
End Sub
0
 
LVL 30

Assisted Solution

by:hnasr
hnasr earned 125 total points
ID: 38728628
"To spare you the never ending debate, ..."
Agree with boag2000!

Which field will trigger end of entry?
Which fields allow nulls?

One idea, to check for newrecord, and disable the subform.
At the point you are satisfied with main form entry, enable subform.

Private Sub Form_Current()
    If NewRecord Then
        subForm.Enabled = False
    Else
        subForm.Enabled = True
   End If

In a button enable subform in data entry.

    subForm.Enabled = True

This can be set as boag2000 suggested in before update event of the main form.
And as boag2000 mentioned this can go on for a while analysing the problem.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38728695
hnasr,

As always, thank you for that great additional info...
;-)

Jeff
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 125 total points
ID: 38728955
Another idea is to develop a routine that checks each of the mainform's controls and enables/disables the subform based on the results of that. You would then just call that routine in one of the control Events (AfterUpdate, Exit, LostFocus, etc). Something like:

Function CheckRecord as Boolean

Dim bOK As Boolean
bOK = True '/ assume everything it okay

bOK= Nz(Me.Textbox1,"") = "" 
'/ If bOK is already false, we don't reset it - we only
'/ continue the checks if the previous controls return True
If bOK Then
  bOK = Nz(Me.Textbox2, "") = ""
End If

If bOK Then
  bOK = Nz(Me.Combo1.Column(0), "") = ""
End If

Me.YourSubformControl.Enabled = bOK

End Function

Now just call CheckRecord for each control that is used to determine if the mainform record is valid. Again, use the AfterUpdate, Exit, LostFocus, etc etc to determine that.
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 125 total points
ID: 38728992
I agree with LSM.  Whenever I use a subform this way, I almost always disable the subform in the main forms Current event if Me.NewRecord = True.

I then have a Save button on the main form, which runs through my required fields and makes sure they are all filled in.  If not, it sets the focus to the missing control(s) sequentially and after all controls are filled in, the Save button will save the record and enable the subform.
0
 
LVL 7

Author Closing Comment

by:valmatic
ID: 38759225
Thanks for all of the responses on this one guys.  I tried variations of each of your posts and could not get it to work exactly like I wanted it so I'm changing tactics.  I'm going to make the controls on my subform non-maintainable so data is preview only.  User can call an entry form through a button on the main form, which will ultimately populate my subform.  User can add as many entries as he wants without actually touching my subform and hopefully minimize mistakes and bogus entries.  I think that's the theory LSM and Fyed wer trying to get across..  NO idea how to split this one so am just doing an even split for everyone.  As always, I appreciate the great input.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question