Simple VBA structure and flow question

  • Thread starter Thread starter ChasW
  • Start date Start date
C

ChasW

Given the simplified code example below, what type of procedure or
function do I need to make so that both of these procedures can call
it and so when the called procedure is completed, flow returns to the
caller?

In other BASICs that I have used, this would be a GOSUB and a RETURN,
but I am not sure about VBA.

Private Sub Search_FORM_Click()

'call the common subroutine
'upon completion of the subroutine, program flow returns here

' Open subform query
DoCmd.OpenForm "someDocName", acNormal, acReadOnly

End Sub

Private Sub Search_DATASHEET_Click()

'call the common subroutine
'upon completion of the subroutine, program flow returns here

' Open subform query
DoCmd.OpenForm "someDocName", acFormDS, acReadOnly

End Sub

Thanks in advance,
Chas
 
Given the simplified code example below, what type of procedure or
function do I need to make so that both of these procedures can call
it and so when the called procedure is completed, flow returns to the
caller?

In other BASICs that I have used, this would be a GOSUB and a RETURN,
but I am not sure about VBA.

Private Sub Search_FORM_Click()

'call the common subroutine
'upon completion of the subroutine, program flow returns here

' Open subform query
DoCmd.OpenForm "someDocName", acNormal, acReadOnly

End Sub

Private Sub Search_DATASHEET_Click()

'call the common subroutine
'upon completion of the subroutine, program flow returns here

' Open subform query
DoCmd.OpenForm "someDocName", acFormDS, acReadOnly

End Sub

Thanks in advance,
Chas

Define you procedure as a Sub (if it won't return a value) or Function
(if it will return a value), either in the same module with the calling
routines, or in a standard module. Suppose your proc's name is
MyCommonRoutine. Then you could call it like this:

Private Sub Search_FORM_Click()

' call shared common routine
Call MyCommonRoutine

' Open subform query
DoCmd.OpenForm "someDocName", acNormal, acReadOnly

End Sub

Alternatively, you could just write the name of the procedure, which
will call it implicitly:

Private Sub Search_FORM_Click()


' call shared common routine
MyCommonRoutine

' Open subform query
DoCmd.OpenForm "someDocName", acNormal, acReadOnly

End Sub
 
Chas,

Create the common subroutine in the normal way between Sub and End Sub
lines. Then call it with the Call command:

Call YourCommonSub
' Flow returns here

If you plan on putting this in a global (from the Modules tab) instead of
the form module in which the other subroutines are defined, declare it as
public so that the others can see it:

Public Sub YourCommon Sub
....
End Sub

Hope that helps.
Sprinks
 
Define you procedure as a Sub (if it won't return a value) or Function
(if it will return a value), either in the same module with the calling
routines, or in a standard module. Suppose your proc's name is
MyCommonRoutine. Then you could call it like this:
...

Thank you very much.
Chas
 

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

Similar Threads


Back
Top