Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flowing through subroutines without "Call" to them

Status
Not open for further replies.

briansmithdanisco

IS-IT--Management
Jun 13, 2007
19
US
I have a main form with 2 sub-forms that have been added to two of the tab controls on the main form. All three have VB added in the OnOpen event. One of the sub-forms, called MiscResource, has a few triggered on-exit events for a few of the fields when data is entered.

The problem I have is when I open the main form and logic passes to each of the OnOpen modules for the main form and the 2 sub-forms, the flow of control continues to execute the other events in the MiscResource subform without the events taking place. An example is I have an On-Exit for a field that looks up a material description after the material number is entered. The On-Exit executes a function that does the query and fills in the display. After the OnOpen finishes, the program continues to go through the On-Exit logic, even though the form has not been displayed yet and nothing has been keyed in to trigger the On-Exit. After it finishes the On-Exit module, it continues to an AfterUpdate module I have in the same subform.

One thing I did find is that if I comment out the call to the material description function, the logic does not jump to the AfterUpdate module. Not sure why?

Why are the other modules being executed when the form is opened and not just the OnOpen part? I guess the way I think it should work is the OnOpen events finish, the form displays, and the logic stops until data is entered and the other triggered events take place.

If anyone can explain why everything seems to be executing when I think only the OnOpen event should run, it would be a big help.

Here is the OnOpen for the subform MiscResource:
Private Sub Form_Open(Cancel As Integer)
' If the close flag is Y then set the type to 2 (Snapshot - read only), else set to 0 (Dynaset)
If [Forms]![frmLiquidBatchSheet].Form![Closed] = -1 Then
Me.RecordsetType = 2
Else
Me.RecordsetType = 0
End If
End Sub

Here is the material lookup that keeps being executed after the OnOpen in the same subform MiscResource:
Private Sub MaterialNumber_Exit(Cancel As Integer)
' If the record is closed then skip all updates and changes.
If [Forms]![frmLiquidBatchSheet].Form![Closed] = -1 Then
Exit Sub
End If

' Lookup material description.
If Not Me.MaterialNumber Then
Me.MaterialName = LookupMaterialName(Me.MaterialNumber.Value)
End If
End Sub


Thanks.
 
check help et al. there is a specific set and order of events fired by opening a form/report object. at least in part, the subobjects are loaded and code in some of the form events are triggered simply by opening the object. an exercise suggested my some (earlier?) versions of help is to use sample objects and include some debug statements to illustrate the events (and their execution order) n various situations. you may be suprised at the sequences. there are often multiple calles to the same procedure - without reference to any code/events which you engendered in your code / procedures/events, but close examination of these audit trails along with the help topics usually illustrates the ratrionale sufficiently for at least proximate understanding - and occassionally points out how/why something you did causes a part of the sqauence.




MichaelRed


 
How are ya briansmithdanisco . . .

A form with subforms opens from the innermost subform out to the mainform (subforms can be at least 3 levels deep), and as each subform is opened it receives the focus. Now . . . when a form is opened, focus is directed to the first control in the Tab Order. Herein lies your problem! One of the controls in question is 1st in the tab order. Here's whats happening:
[ol][li][blue]MiscResource[/blue] opens and focus goes to the 1st control in the tab order. This is probably the [blue] material description[/blue] control you speak of.[/li]
[li][blue]MiscResource[/blue] finishes its opening phases and wether the alternate subform or mainform opens next, focus is transferred to that form (robbing focus from [blue] material description[/blue]) and triggering the [blue]OnExit[/blue] event![/li][/ol]
To circumvent this, add a hidden textbox to the form and set it 1st in the tab order or set focus to a control where the exit & lost focus events are not used . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Thanks TheAceMan1. I will try what you suggested. I walked through the code several times and I really could not figure out why it was flowing the way it did. I figured out the sequence of the subforms being opened, but I had no real explanation as to the order after the OnOpen.

My fix was to check the Screen.ActiveControl on the code that was causing me the problems. If it was not set to the field that was being modified, I exited the sub. That way the code passed through the module without executing and causing me problems. It works, but I will try what you suggested. Your suggestion seems cleaner.

Thanks.
 
briansmithdanisco . . .

I should've said: add a hidden textbox to the [blue]subform[/blue] and set it 1st in the tab order or set focus to a control where the exit & lost focus events are not used . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top