Use same code in form and report

  • Thread starter Thread starter Chuck
  • Start date Start date
C

Chuck

I have a form and report which are almost identical in terms of content
(i.e., many of the controls have the same name and show the same data).

Currently, the same code exists in both the form and the report. This code
essentially sets the properties of some of the controls when the form or
report opens.

Example

Sub in Form:

Sub MyForm_Open()
Me!Control1.caption="Hi"
Me!Control2.caption="There"
End Sub

Sub in Report:

Sub MyReport_Open()
Me!Control1.caption="Hi"
Me!Control2.caption="There"
End Sub


Since the names of the controls are the same in both the form and report, I
would like to move the appropriate lines to a procedure in another module:

Sub in Form:

Sub MyForm_Open()
Call Set_Properties(Me)
End Sub

Sub in Report:

Sub MyReport_Open()
Call Set_Properties(Me)
End Sub

Sub in Module:

Sub Set_Properties(MyName as Form)
MyName!Control1.caption="Hi"
MyName!Control2.caption="There"
End sub



Now, the problem I am obviously having is that the code in the module only
applies to the form (since MyName is declared as Form). How can I have it
work for both the form and the report?

Thanks
 
Chuck said:
I have a form and report which are almost identical in terms of content
(i.e., many of the controls have the same name and show the same data).

Currently, the same code exists in both the form and the report. This code
essentially sets the properties of some of the controls when the form or
report opens.

Example

Sub in Form:

Sub MyForm_Open()
Me!Control1.caption="Hi"
Me!Control2.caption="There"
End Sub

Sub in Report:

Sub MyReport_Open()
Me!Control1.caption="Hi"
Me!Control2.caption="There"
End Sub


Since the names of the controls are the same in both the form and report, I
would like to move the appropriate lines to a procedure in another module:

Sub in Form:

Sub MyForm_Open()
Call Set_Properties(Me)
End Sub

Sub in Report:

Sub MyReport_Open()
Call Set_Properties(Me)
End Sub

Sub in Module:

Sub Set_Properties(MyName as Form)
MyName!Control1.caption="Hi"
MyName!Control2.caption="There"
End sub



Now, the problem I am obviously having is that the code in the module only
applies to the form (since MyName is declared as Form). How can I have it
work for both the form and the report?


Declare it as a generic object, Access will figure it out at
runtime.

Sub Set_Properties(obj As Object)
obj!Control1.caption="Hi"
obj!Control2.caption="There"
 
Thanks a bunch!

I would also like to take the time to thank everyone who has been providing
answers on the Access newsgroups. Your time and effort is much appreciated.
 
Back
Top