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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run one sub procedure from another

Status
Not open for further replies.

helmpost

Technical User
Sep 13, 2005
10
GB
Folks,

I think this should be simple but ... sound of tearing hair...

Access 2002 under XP. How should I call one sub procedure from another on the same form? I have a form with a date field. On mousedown, I make visible an ocx calendar, select a date and make the date field equal to the selected value and then select some records based upon that date. Fine. Now I'd like to do the same thing based upon today's date when the form loads. It seems logical to simply (somehow) call the existing onmousedown sub procedure that already exists ... but I can't see how to do it .. run ... execute ... tried many ways and followed many web threads but no joy.

I can overcome the problem by simply copying the existing code into the on load event but believe that I shouldn't have to. Reusable code seems right here but how?

TIA for anyone able to respond

Regards, Brian

 
In VB/VBA, the way to call a subroutine is to simply use it's name
Code:
   MySub

If it has parameters, follow the name with the parameter values, seperated by commas
Code:
   MySub FirstParam, SecondParam, ThirdParam

You can also use the "Call" statement, but then you need to put the parameters within brackets
Code:
   Call MySub(FirstParam, SecondParam, ThirdParam)

In your case, I would move the code you currently have into a separate subroutine, and then call that subroutine from the two events where you want it to execute.
Code:
Private Sub MyNewSub()
   'Copy and paste code here
End Sub


 
Thanks for the response Joe. I'll take the new sub route but I'm still intrigued ...

my form is frmrace13 on which is the date field frmDate and within that the onmousedown event which generates this sub ...

Sub frmDate_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)

If I wanted to call this sub from, let's say the form load event, what would the syntax be ?

frmDate.MouseDown gives a compile syntax error as does Me.frmDate.MouseDown

Must be missing something ....

REgards, Brian
 
You could call frmDate_MouseDown as it is a procedure, but I would advise against it. Because it has parameters, you would need to provide values for them, and in this case you would basically be making the numbers up just to satisfy this requirement, so for example
Code:
    frm_DateMouseDown 0, 0, 0, 0
Those numbers may or may not be valid.

Even if you called a control event that doesn't require parameters, a button click for example, in my mind that is a bad coding practice, as it distorts the purpose of the procedure event.

If you have a "Save" button, for example, the procedure event (e.g. cmdSave_Click) should really be thought of as "the thing that happens when the button is clicked". Logically it should not be the procedure for "saving to the database", but that is what it becomes when you start calling cmdSave_Click from other procedures. Even when the button is the only thing that initiates saving, I still have it call a separate procedure to do the actual work
Code:
Private Sub cmdSave_Click()
    'Call procedure to save changes to the database
    SaveChanges
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top