Passing Form Name 
Author Message
 Passing Form Name

I have a subroutine that is used on three different forms. I have the
subroutine copied into each of the forms but it is the same exact routine
except that the form name is different. That seems terribly inefficient.

Is there a way to create a subroutine that is passed the form's name? The
example code below is something I've tried (also to further explain what I
am trying to do) but it doesn't work. Any suggestions?

- Merf

spam

EXAMPLE CODE (In Module1.BAS):
Public Sub DoSomething(mFormName As Object)
    Dim mWithStmt As Object

    Set mWithStmt = mFormName

    With mWithStmt
        .Caption = "Test"       <-- Change the form's caption
        .chkCheckBox.Value = 1  <-- Change the value of a check box on the
form
    End With

End Sub



Mon, 20 Nov 2000 03:00:00 GMT  
 Passing Form Name

Answered my own question.

I need to "Call" the subroutine below and I wasn't. The proper syntax is
Call DoSomething(frmOne). Don't use the reassignment line (the set
function).

- Merf

Quote:

>I have a subroutine that is used on three different forms. I have the
>subroutine copied into each of the forms but it is the same exact routine
>except that the form name is different. That seems terribly inefficient.

>Is there a way to create a subroutine that is passed the form's name? The
>example code below is something I've tried (also to further explain what I
>am trying to do) but it doesn't work. Any suggestions?

>- Merf

>spam

>EXAMPLE CODE (In Module1.BAS):
>Public Sub DoSomething(mFormName As Object)
>    Dim mWithStmt As Object

>    Set mWithStmt = mFormName

>    With mWithStmt
>        .Caption = "Test"       <-- Change the form's caption
>        .chkCheckBox.Value = 1  <-- Change the value of a check box on the
>form
>    End With

>End Sub



Mon, 20 Nov 2000 03:00:00 GMT  
 Passing Form Name

I don't know that you answered your own question.  You should never
need to use CALL.

Call DoSomething(x)

is the same as:

DoSomething x

Create a subroutine in the BAS module which is passed a form object
then access that object directly.  For example

Public Sub CenterMe(Form1 as Form)

        With Form1
                .Move (Screen.Width / 2) - (.Width / 2), _
                (Screen.Height / 2) - (.Height / 2)
        End With

End Sub

-RHood

On Thu, 04 Jun 1998 14:07:14 GMT, "Merrill \"Merf\" Fratkin"

Quote:

>I have a subroutine that is used on three different forms. I have the
>subroutine copied into each of the forms but it is the same exact routine
>except that the form name is different. That seems terribly inefficient.

>Is there a way to create a subroutine that is passed the form's name? The
>example code below is something I've tried (also to further explain what I
>am trying to do) but it doesn't work. Any suggestions?

>- Merf

>spam

>EXAMPLE CODE (In Module1.BAS):
>Public Sub DoSomething(mFormName As Object)
>    Dim mWithStmt As Object

>    Set mWithStmt = mFormName

>    With mWithStmt
>        .Caption = "Test"       <-- Change the form's caption
>        .chkCheckBox.Value = 1  <-- Change the value of a check box on the
>form
>    End With

>End Sub



Tue, 21 Nov 2000 03:00:00 GMT  
 Passing Form Name

Where you think you're passing a form name you are actually passing a Form.
Your code would make more sense if rewritten

Public Sub DoSomething(ByRef frmThisForm As Form)

    With frmThisForm
        .Caption = "Test"       <-- Change the form's caption
        .chkCheckBox.Value = 1  <-- Change the value of a check box on the
form
    End With

End Sub

The confusion between
    Call DoSomething(frmOne)
    DoSomething frmOne
and
    DoSomething (frmOne)

Is that the first two will pass By Reference and the last one will pass By
Value.
While this may not affect some routines it will others. Note the space in
the last example between the procedure name and the bracket. In the first
example (with Call keyword) there is no space and the brackets form part of
the procedure call. In the last example the brackets are not part of the
procedure call and server to "evaluate" the expression they contain. That is
they give a VALUE

Regards,
Simon Jones
PC Pro Magazine



Tue, 21 Nov 2000 03:00:00 GMT  
 Passing Form Name

Quote:

> I don't know that you answered your own question.  You should never
> need to use CALL.

> Call DoSomething(x)

> is the same as:

> DoSomething x

Sorry to barge in. I was wondering about your statement about CALL. I've
been using it, I guess out of habit. But besides personal preference,
does it make any speed or other difference whether you use it or not?

Thank you
Robert



Tue, 21 Nov 2000 03:00:00 GMT  
 Passing Form Name

No, the compiler treats it exactly the same, its just a matter of
preference.  Personally I leave it out, but I see many people still
use it.

Quote:


>> I don't know that you answered your own question.  You should never
>> need to use CALL.

>> Call DoSomething(x)

>> is the same as:

>> DoSomething x

>Sorry to barge in. I was wondering about your statement about CALL. I've
>been using it, I guess out of habit. But besides personal preference,
>does it make any speed or other difference whether you use it or not?

>Thank you
>Robert



Mon, 27 Nov 2000 03:00:00 GMT  
 Passing Form Name

Thank you

Quote:

> No, the compiler treats it exactly the same, its just a matter of
> preference.  Personally I leave it out, but I see many people still
> use it.


> >Sorry to barge in. I was wondering about your statement about CALL. I've
> >been using it, I guess out of habit. But besides personal preference,
> >does it make any speed or other difference whether you use it or not?



Mon, 27 Nov 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Passing Form Names as Variables

2. Passing Form Name

3. Passing form names

4. => Passing form name to called proceedure

5. Passing form names

6. passing form names to subs

7. Passing form Names

8. How to pass form name as variable to a public module?

9. Visual Basic 4 error 438 passing form name to sub.

10. Forms Collection: Using it to Open Forms by Passing the Form Name as a String Variable

11. How do I open a form by passing the Name of the form as a string

12. Newbie, correct syntax passing arguments as form names

 

 
Powered by phpBB® Forum Software