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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass control reference as parameter 1

Status
Not open for further replies.

demoman

Programmer
Oct 24, 2001
242
US
I am trying to pass the reference to an ADODC control to a function. The control is on a form and the function is in a class mod. I can reuse this function code if I can make the ADODC variable (there are three different ones on different forms). For example:

Public Function Load_Data_Values(objAdoSource as control) as Long

frmMain.txtHeader(0).text = objAdoSource.Recordset.Fields!Field1

So, I cannot figure out how the pass the value from the calling program. Appreciate any help
 
demoman,
I haven't tried sending and ADODC control as a parameter but I have tried ListBox and other controls without any problems, so I imagine your shouldn't have any problems with your ADODC control either. What kind of problem are you having? What error message do you get? For example, if the name of your control is, say, adoMyControl, you should have some code in your form that looks like this:
Code:
[COLOR=blue]Dim[/color] l [COLOR=blue]As Long[/color]
l = Load_Data_Values(adoMyControl)
Is the code above giving you problems?

On the other hand, if you'll be using the control just to get the name of some field, you can create a function that takes a string parameter instead, and send in the name of the field (from the ADODC control) to the function. Something like this:
Code:
[COLOR=green]' Let's call this function "GetValues"
' instead of "Get_Data_Values"[/color]
[COLOR=blue]Public Function[/color] GetValues([b]s As String[/b]) [COLOR=blue]As Long[/color]
   frmMain.txtHeader(0).Text = [b]s[/b]
   [COLOR=green]' Other stuff here[/color]
[COLOR=blue]End Function[/color]
[COLOR=green]
' To call this function from the form,
' you would do this:[/color]
[COLOR=blue]Dim[/color] l [COLOR=blue]As Long[/color]
l = GetValues(objAdo.Recordset.Fields!Field1)
Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
JC - thanks for the reply. He is more clarification:

I have a Sub like this:

Public Sub Load_Data_Values()

frmMain.txtHeader(0).text = AdoSource.Recordset.Fields!Field1
frmMain.txtHeader(1).text = AdoSource.Recordset.Fields!Field2
frmMain.txtHeader(2).text = AdoSource.Recordset.Fields!Field3
frmMain.txtHeader(3).text = AdoSource.Recordset.Fields!Field4
frmMain.txtHeader(4).text = AdoSource.Recordset.Fields!Field5

End Sub

The 'AdoSource' is an Adodc control. I want to reuse this Sub/Function (it will end up being a function later) for a couple different Adodc's, which are on different forms.

So, I was trying to pass the reference to the control as a parameter:

Public Sub Load_Data_Values(objAdoSource as control)

frmMain.txtHeader(0).text = objAdoSource.Recordset.Fields!Field1
frmMain.txtHeader(1).text = objAdoSource.Recordset.Fields!Field2
frmMain.txtHeader(2).text = objAdoSource.Recordset.Fields!Field3
frmMain.txtHeader(3).text = objAdoSource.Recordset.Fields!Field4
frmMain.txtHeader(4).text = objAdoSource.Recordset.Fields!Field5

End Sub

I call it like:

Private sub cmdTest_Click()

Load_Data_Values (Adodc1)

End sub

I receive an error on the call, "object does not support this property or method"

I tried substituting Public Sub Load_Data_Values(objAdoSource as Adodc), but that did not work either. There are other ways to accompish this, but I want to know why this does not work.

Thanks
 
demoman,
This is what's happening:

1 - You cannot use the methods of class on an object of a different class. In your case, you are using the methods of the Adodc class (Recordset, for instance) on an object of the Control class, and this is not allowed. Recordset is a method of the Adodc class and thus you must use it with an object of the Adodc class. That's why you get the error "object does not support this property or method". Let's look at your function again:
Code:
[COLOR=blue]Public Sub[/color] Load_Data_Values(objAdo [b][COLOR=blue]As[/color] Control[/b]) 
   [COLOR=green]' You cannot do this because objADO is a
   ' Control object, not an Adodc one[/color]
   x.Text = objAdo.[b]Recordset.Fields!Field1[/b]
   ...
[COLOR=blue]End Sub[/color]

2 - In your function, the parameter should be of type Adodc. Now, when you use the Adodc class, things should work properly, but you have to call the Refresh method of the control to populate the recordset before you use it. Thus, your code should look like this:
Code:
[COLOR=green]' Note that objADO is of type Adodc[/color]
[COLOR=blue]Public Sub[/color] Load_Data_Values(objADO [b][COLOR=blue]As[/color] [COLOR=darkred]Adodc[/color][/b])
   [COLOR=green]' Use Refresh to populate the recordset[/color]
   [b]objADO.Refresh[/b]
   [COLOR=green]' This (below) should work fine[/color]   
   x.Text = objADO.Recordset.Fields!Field1
   ...
[COLOR=blue]End Sub[/color]
Make sure that you use the name of the fields correctly. For example, for the line where it says objADO.Recordset.Fields!Field1, the recordset must have a field called Field1, otherwise the code won't work either. Thus, if your connection string is accessing a table or query that contains a field called "CustomerID", then the line above should be objADO.Recordset.Fields!CustomerID.

Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
demoman,
I forgot to mention that you don't have to call the Refresh method inside the Load_Data_Values function. You may (in fact, you should) call it in the calling form so that the control is ready to use before it is sent to the Load_Data_Values function!

Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
JC - thanks for the info. I am now functional and know where I need to do more reading!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top