Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing UserControl as param into Sub

Status
Not open for further replies.

BTon15

Programmer
Nov 21, 2001
29
0
0
GB
I'm trying to write a generic Sub to build up a string of all the data I've typed into my text boxes, combos, etc since I last did a Save.

Most of my controls are held within user controls, so I'd like to call my new Sub like this -

sChangeString = GetChangeString (Me)

I've created my Sub within a module like this -

Public Sub GetChangeString(ByRef myCtl As Variant)

Inside my Sub I wanted to loop through the controls collection within myCtl to build up a text string of their values. However, when I try to access any of my constituent controls I get Run time error 438 - Object doesn't support this property or method. I can't even access the name of the controls, e.g. myCtl.Controls(1).Name

Strangely though, I can see any properties of my User Control that have a Public Get by doing myCtl.myID where myID is a public property.

I'd be very grateful if anyone could tell me where I'm going wrong, or suggest a better way to do this.

Thanks in advance.


 

Actually, what you are trying to do is pass a form as an argument. So what you would need to do is something like...
[tt]
Public Function GetChangeString(ByRef myForm As Form) As String
Dim C As Control, S As String
For Each C In MyForm
If TypeOf C Is TextBox Then
If UCase(C.Name) = UCase("text1") Then
S = S & C.Text
ElseIf UCase(C.Name) = UCase("text2") Then
...
GetChangeString = S
[/tt]

Also you are calling a sub which does not explicitly return a value unlike a function as noted above.

BTW BTon15, have you read FAQ222-2244 yet?

Good Luck

 
Thanks for your reply. However, what I'm trying to do is pass a User Control as the parameter, not a form.

I have a form with several tabs, each one containing a User Control which holds text boxes, combos, etc. I want to do a Save only for the tab that I'm currently on, not the whole form. This means I want to check the data only for the User Control for my current tab, not the entire form.

My code was in a function, not a Sub. I just typed up my question slightly wrong! Hope that didn't mislead you.
 

Then...
[tt]
Public Function GetChangeString(ByRef myCtl As UserControName) As String
[/tt]

BTW BTon15, have you read FAQ222-2244 yet?

Good Luck

 
The problem is that my User Controls are all different, so I can't pass in my parameter with type UserControlName.

I'm trying to write a generic module that will work for all my User Controls, so was trying to pass the parameter with type Control, or Variant, or Object or something similar, but can't get any of them to work the way I want.

Thanks again for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top