Duplicate code consolidation

R

RLN

I inherited an application that has lots of duplicate code in it.
The code below is for two separate buttons on a form, each one opens a
different form. Is there a way to have one module that could pass in the
form? Do you pass in the form as a string, an object or as a form? I tried
several an received errors. I think I'm on the right track, just have the
wrong syntax.

Thanks.

Code:
Private Sub cmdW1Data_Click()
On Error GoTo Err1
DoCmd.OpenForm "frmW1DataOut", acFormDS 'acFormDS = Datasheet mode
Exit1:
Exit Sub
Err1:
UTIL_ErrorMessage Err.Number, _
Err.Description, _
"There was a problem viewing the output data
for W1.", _
"cmdW1Data_Click()"
Resume Exit1
End Sub

Private Sub cmdW2Data_Click()
On Error GoTo Err1
DoCmd.OpenForm "frmW2DataOut", acFormDS
Exit1:
Exit Sub
Err1:
UTIL_ErrorMessage Err.Number, _
Err.Description, _
"There was a problem viewing the output data
for W2.", _
"cmdW2Data_Click()"
Resume Exit1
End Sub
 
M

Mackster66

I'm not sure what you mean about duplicate code. You have two buttons. Each
button has its own code that opens different forms. Are you trying to
consolidate both forms into one dependent upon a condition on the main form?
 
R

RLN

Yes, I just want to have one module that can open any form. I don't want to
have to duplicate the whole module (including error routines, etc) for each
form I need to open.

As a spinoff to this thread, I'd like to be able to do this thing for other
objects as well. (have a routine that will open any query, then another that
will open reports, etc.)
 
M

Mackster66

I'm really not sure what you are talking about.

The code specifies what should happen when an event occurs, such as when you
click a button. You need to have code for each event if you want something
to happen when that event occurs. The code should specify what should happen
in the event of an error. It may look like duplication, but it really isn't.

In the case of the code you posted, you have two buttons which are for two
different actions. Each button has code to open a specific form with error
handling. Unless both buttons open the same form, I don't see any
duplication.

You could assign one event with a bunch of If statements to determine which
event should fire, but that is really not the best practice. Everything will
run much more smoothly if you leave each event with its own Sub.
 
M

Mackster66

One other thought comes to mind....

If you want to open several reports with one button, simply add each one in
succession into the code. i.e.:

Private Sub cmdW1Data_Click()
On Error GoTo Err1
DoCmd.OpenReport "report1", acViewNormal
DoCmd.OpenReport "report2", acViewNormal
DoCmd.OpenReport "report3", acViewNormal
Exit1:
Exit Sub
Err1:
MsgBox Err.Description
Resume Exit1
End Sub

This will open and print report1, report2, and report3 with one click. You
can also do this with forms, but it's not advisable. It can be very
confusing to the user (and the system) to have too many forms open at once.
 
R

RLN

What I'm trying to do is something like this:

Private Sub cmdButton1(frm1 as form)
'this module is inside a form
OpenMyForm(frm1)
End Sub

Private Sub cmdButton2(frm2 as form)
'this module is inside a form also...
OpenMyForm(frm2)
End Sub
- - - - -
Public Sub OpenMyForm(prmForm as Form)
'this module is inside a module & can be called from anywhere
Here is where I am stuck in writing code for the form in the current db...
...need to test to see if the parameter "prmForm" exists; if not throw
error & exit
...open the form if user does not have it opened already
End Sub
 
M

Mackster66

Now I understand what you are trying to do. In my opinion, it is all too
easy to copy and paste error handling for events. Since there is already
code for opening forms, reports, etc. I cannot see why you would work so hard
to create a module to accomplish the same thing.

If you're dead set on doing it, though, I'm afraid you will have to ask
someone else. Modules require coding that is beyond my ability. I apologize
for wasting your time.
 
R

Rick A.B.

What I'm trying to do is something like this:

Private Sub cmdButton1(frm1 as form)
    'this module is inside a form
     OpenMyForm(frm1)
End Sub

Private Sub cmdButton2(frm2 as form)
    'this module is inside a form also...
     OpenMyForm(frm2)
End Sub
- - - - -
Public Sub OpenMyForm(prmForm as Form)
    'this module is inside a module & can be called from anywhere
    Here is where I am stuck in writing code for the form in the current db...
    ...need to test to see if the parameter "prmForm" exists; if not throw
error & exit
    ...open the form if user does not have it opened already
End Sub


Create a public function something like

Public Function OpenForms(stFormName as string) as Integer
DoCmd.OpenForm stFormName
End Function

Then in the OnClick of your button pass the form name to the function
like
Call OpenForms(prmForm)

if you want to open the form to a specific record you'll have to
modify the function to accept your link criteria and include it when
you call the function

Hope that helps.

Rick
 
R

RLN

Modules require coding that is beyond my ability. <<
no problem....
....no need to apologize. :)
You took the time to write and for that I appreciate it.
 

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