Two subroutines controlling one form

J

jeh

Command buttons on my switchboard launch Event Procedures sub1 and
sub2. One button opens the form frmMyForm in data entry mode, the
other uses a query to select particular records that need updating and
then opens the same frmMyForm with the appropriate record(s)
available. So far so good.

frmMyForm has three command buttons whose logical functions are:
* Reopen the form (for another data entry or for another record to
edit, depending on whether the form was opened by sub1 or sub2)
* Save the entries, close the form and return to the Switchboard
* Save the entries and close the database.

The only way I can think of to get this to work when two subroutines
open the same form involves the following code (considerably
simplified from the original) for sub2, and similar but simpler code
for sub1.

Private Sub Sub2()

Dim strCrit As String
1 strCrit = InputBox("Please enter Data Identifier")
DoCmd.OpenForm "frmMyForm", , , strCrit, , acDialog

' Insert code to either
' Go to 1 (to start the routine again)
' Close frmMyForm and return to Switchboard, or
' Close the database

End Sub

My problem arises at completion of data entry/editing, when I'm ready
to close (or hide?) frmMyForm. Can, or should, I attempt to have each
of the three closing buttons return a different value to Sub2 (which I
can then use to switch control) or should I attempt to have each
button enter sub2 at a different entry point somewhere below the
DoCmd.OpenForm statement? In either case, how can one do this?

I know that one obvious answer is to use two versions of the form.
I've been doing this for a while but have had trouble with maintenance
issues when changes occur.

Apologies if this type of problem has been answered before - I've just
not found the right keywords to find an example

TIA John
 
K

Klatuu

If one button opens the form in data entry mode and the other doesn't, you
can tell which button opened the form by checking the value of the data entry
property

If Me.DataEntery = True Then
'Do the things you need if you are in data entry mode
Else
'Do the other Stuff
End If
 
J

jeh

If one button opens the form in data entry mode and the other doesn't, you
can tell which button opened the form by checking the value of the data entry
property

    If Me.DataEntery = True Then
        'Do the things you need if you are in data entry mode
    Else
        'Do the other Stuff
    End If
--
Dave Hargis, Microsoft Access MVP















- Show quoted text -

Thanks Klatu. Unfortunately that only partly fixes my problem.
Regardless of which sub opens frmMYForm, closing the form is initiated
by the user clicking on one of three buttons
1. Carry on with the subroutine (either sub1 or sub2, depending on
which one opened the form)
2. Go to the switchboard (to let the user go to their next task)
3. Close the database.

#3 is no problem. Everything can be done from the button. The Form
closes, the database closes and therefore the subroutine closes.
#1 & #2, however, must first return control to the subroutine. How
can I distinguish between them? Me.dataentry will be true for both if
the form was opened by sub1 and will be false for both if it was
entered from sub2. If I put code behind button 2 to go straight to
the switchboard control reverts to sub1 (or sub2) which tries to
complete its code - not wanted in this case.

Hope I've made myself clear.

cheers John
 
K

Klatuu

Control ins not going to be returned to the subroutines unless you open the
form in dialoge mode. If you open the form with Docmd.OpenForm in dialoge
mode, the code in the subroutine will sit and wait until the form closes
before continuing; otherwise, the code continues to execute. So in your
example, the application would open the form, then immediately close.

I guess I am also not clear on why you are doing things the way you are.
Why are you making it necessary for a user to have to close the form and
reopen the form for each record. Seems to me that would really slow a user
down.
 
J

jeh

The form is opened in dialog mode and is used to make entries, edit
data etc as appropriate. When finished, the user needs to have the
choice to either select another record, return to the Switchboard or
close the database. (The 3rd choice is to avoid the double entry
required if the user is forced to go to the switchboard before closing
the db). Usage of the db isn't intensive and the penalty involved in
closing/reopening is acceptable. The only problem I've not solved is
how to tell the subroutine which button on the form has closed the
form and returned control to the sub. (An alternative approach would
be, if possible, to have the "Go to Switchboard" button somehow
terminate the sub. I don't know of any way to do this.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top