passing a form to a module 
Author Message
 passing a form to a module

Hi there.

I have a simple question about VisualBasic 5.0. I have a form with a few
textboxes on it. What I want ot achieve is clearing the textboxes. This has
to be done a few times through out the program. This is why I would like to
place the code in a sub in module.

The only problem is that I can't seem te be able te figure ou how I can pass
the form name to the sub to clear the textboxes.

So far I've tried this, but it doesn't seem to work:

On the form:

 Dim f As Form
 Call sClearTextboxes(f)

In the module:

Public Sub sClearTextboxes (f as Form)
 f.txtTextbox1.Text = ""
 f.txtTextbox2.Text = ""
end sub

This keeps giving me Runt-time error '91': Object variable or With block
variable not set.

I hope that someone can help me with this.

Thanks,

Eric van Glabbeek



Tue, 12 Jun 2001 03:00:00 GMT  
 passing a form to a module
The problem is with the code:

Dim f As Form
Call sClearTextboxes(f)

You need to declare f as the actual form name, e.g.:
Dim f As Form1
Call sClearTextboxes(f)

If the only place you are calling sClearTextBoxes is from within the form
itself, then it would actually be better to place sClearTextboxes() inside
the form module.  Then, you wouldn't even need to pass a reference to the
form.

Hope this helps.



Tue, 12 Jun 2001 03:00:00 GMT  
 passing a form to a module
Hi!
If you want to clear all textboxes on a form then you could place the method
in a module.

Here's an example:

Put this code in an module:

Public Sub ClearAllTxt(frm as form)

    Dim Ctrl as variant

    For Each Ctlr in frm.Controls
        If Type Of Ctrl Is Textbox then
            Ctrl.text=""
        End If
    next

End Sub

This method can be used with any form that needs to clear all textboxes. You
can modify this method if you want to clear other type
of controls.

I hope this helps

Happy holidays!

Regards

/Mattias Dahlgren


Quote:
>Hi there.

>I have a simple question about VisualBasic 5.0. I have a form with a few
>textboxes on it. What I want ot achieve is clearing the textboxes. This has
>to be done a few times through out the program. This is why I would like to
>place the code in a sub in module.

>The only problem is that I can't seem te be able te figure ou how I can
pass
>the form name to the sub to clear the textboxes.

>So far I've tried this, but it doesn't seem to work:

>On the form:

> Dim f As Form
> Call sClearTextboxes(f)

>In the module:

>Public Sub sClearTextboxes (f as Form)
> f.txtTextbox1.Text = ""
> f.txtTextbox2.Text = ""
>end sub

>This keeps giving me Runt-time error '91': Object variable or With block
>variable not set.

>I hope that someone can help me with this.

>Thanks,

>Eric van Glabbeek



Tue, 12 Jun 2001 03:00:00 GMT  
 passing a form to a module
This subroutine should be placed in a standard module

Public Sub Clear_Textboxes(frmName As Form)
   Dim iIndex As Integer
   For iIndex = 0 To frmName.Controls.Count - 1
      If TypeOf frmName.Controls(iIndex) Is TextBox Then
         frmName.Controls(iIndex).Text = ""
      End If
   Next
End Sub

To call it use:

Clear_Textboxes Me

Hope this is what you need.
John

Quote:

>Hi there.

>I have a simple question about VisualBasic 5.0. I have a form with a few
>textboxes on it. What I want ot achieve is clearing the textboxes. This has
>to be done a few times through out the program. This is why I would like to
>place the code in a sub in module.

>The only problem is that I can't seem te be able te figure ou how I can
pass
>the form name to the sub to clear the textboxes.

>So far I've tried this, but it doesn't seem to work:

>On the form:

> Dim f As Form
> Call sClearTextboxes(f)

>In the module:

>Public Sub sClearTextboxes (f as Form)
> f.txtTextbox1.Text = ""
> f.txtTextbox2.Text = ""
>end sub

>This keeps giving me Runt-time error '91': Object variable or With block
>variable not set.

>I hope that someone can help me with this.

>Thanks,

>Eric van Glabbeek



Tue, 12 Jun 2001 03:00:00 GMT  
 passing a form to a module
Eric van Glabbeek wrote

Quote:
>On the form:
> Dim f As Form
> Call sClearTextboxes(f)
>In the module:
>Public Sub sClearTextboxes (f as Form)
> f.txtTextbox1.Text = ""
> f.txtTextbox2.Text = ""
>end sub

You should change your code on the form (named Form1). Both this

  Dim f As New Form1
  sClearTextboxes f

and this

  Dim f as Form
  Set f = Me
  sClearTextboxes f

work. I'm not sure about the reason, but i think the first sample creates an
instance from Form1, while the second creates an instance from the Form
class, and then assigns Me (i.e. Form1) to it.
bye bye and best wishes for incoming new year, :^)
Marco Marcon.



Thu, 14 Jun 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing input box value from Form Module to report module

2. Pass variable form module to a form

3. How do you pass from obj from one form to a module/forms

4. passing forms and controls so I can set focus back to a form from a module

5. passing forms and controls so I can set focus back to a form from a module

6. Passing Value from Module Function to Form sub problem

7. Module works, except when form passes arguements

8. Passing a Form name to a module

9. Passing variable from form to module??

10. Passing data between a user form and code module

11. passing parameters from a form to a module

12. Passing a form to a sub in a module

 

 
Powered by phpBB® Forum Software