PDA

View Full Version : use code for multiple controls



Djblois
03-08-2007, 09:44 AM
I have a few forms that have some of the same controls and I would like to use the same code for them. I give the controls the same names on all the forms. Is there a way to assign a variable to a form so I can use the code in multiple places. Here is an example:

'Test if user left name blank
'Test if another sheet with the same name exists
If SheetExists(myInput) Then
myInput = PivotTableOptions.ReportName.Value
PivotTableOptions.Hide ' Sometimes this would be a different form
DoEvents
Error008.Show
End If

or do I always have to copy the code into each new sub and use the other forms name?

Bob Phillips
03-08-2007, 10:32 AM
I can't see what that code has to do with a form, but you could always pass the form object to the sub as an argument.



Sub Test()
Call Called(UserForm1)
End Sub

Sub Called(pForm As UserForm)
'do something to the form
End Sub

Djblois
03-08-2007, 10:42 AM
I don't want to do something to the form but I want to test the controls on the form:

If SheetExists(myInput) Then
myInput = PivotTableOptions.ReportName.Value
PivotTableOptions.Hide ' Sometimes this would be a different form
DoEvents
Error008.Show
End If

Here is a second time I use the same exact code for a different form:

If SheetExists(myInput) Then
myInput = InvoiceReports.ReportName.Value
InvoiceReports.Hide ' Sometimes this would be a different form
DoEvents
Error008.Show
End If

Djblois
03-08-2007, 01:22 PM
bump

Bob Phillips
03-08-2007, 03:43 PM
Controls are part of the form, so testing them is doing something to the form.

Djblois
03-09-2007, 11:38 AM
what is pForm is that a variable??? Also the first procedure is how I show the form? and the second procedure is how I would test the form?

Djblois
03-13-2007, 07:19 AM
bump

geekgirlau
03-13-2007, 06:41 PM
If the code is on the form,


If SheetExists(myInput) Then
myInput = Me.ReportName.Value
Me.Hide
DoEvents
Error008.Show
End If

Djblois
03-14-2007, 08:14 AM
that will help sometimes but then when they go to the error form they get an option to rename which would clear out myInput and then switch back to the previous form which can be pivtottableoptions or invoiceoptions or pfgoptions.

PivotTableOptions.ReportName.Value = ""
Unload Error00_00_02
PivotTableOptions.Show

or

InvoiceOptions.ReportName.Value = ""
Unload Error00_00_02
InvoiceOptions.Show

that is under the Rename report button