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!

USING A CLASS MODULE AS A DATA SOURCE

Status
Not open for further replies.

TNN

Programmer
Sep 13, 2000
417
US
If anyone out there can help it is appreciated.
I have created a class module as a data source. In the initialize event I use ADO to open my recordsets. I have code for the "Private Sub Class_GetDataMember(DataMember as String, Data As Object" as follows:
select case datamember
case 1stRs
set data = 1stRs
case 2ndRs
set data = 2ndRs
case 3rdRs
set data = 3rdRs
end select
How do I pass the DataMember value to the Sub? When I try to pass DataMember from another procedure in a form module I get error "Sub or Function not Defined". Making the Sub Public makes no difference, get same error.
I understand that a class module by default is private. If so, how in the world am I going to pass a value to it?? How else would you make the select case work?? I must be missing something.
Thanks to anyone in advance.
TNN, Tom
TNPAYROLL@AOL.COM

[sig]<p>TOM<br><a href=mailto:WWW.TNPAYROLL@AOL.COM>WWW.TNPAYROLL@AOL.COM</a><br>[/sig]
 


If I am correct by reading your inquery you are trying to pass data (vars,strings etc) to a Sub??

If so try this:

When you call the sub call it as follows:

Call SubA(Var1,Var2,Str1,Str2)

In the module refer to the sub:

Public SubA(Var1 as integer,Var2 as Byte, Str1 as String,Str2 as String)

.
.
.
End Sub

Now if you need to pass an Arrey just:

Public SubA(Var1() as Integer,Var2 as Byte,Str1() as String,Str2 as String)

Add the '()' to the Data members which are arreys.

John Stephens [sig][/sig]
 
Thank You John, but I have already tried according to the code your giving me but it's no go.

As I understand it you cannot pass an argument to a procedure that is in a class module. A class module by default is Private. If this is true than how do I get an argument to the Class_GetDataMember sub that is automatically created when I set the class module property to become a datasource.

TNN, Tom
TNPAYROLL@AOL.COM [sig]<p>TOM<br><a href=mailto:WWW.TNPAYROLL@AOL.COM>WWW.TNPAYROLL@AOL.COM</a><br>[/sig]
 
Your class is a Data Source, When a Data Consumer's DataSource property is set (The Data Consumer has to be bound to your Data Source Class), the above Event fires in your Class, you should not be passing anything to this event.


Private Sub Class_GetDataMember(DataMember as String, Data As Object

I pasted some stuff below from MSDN that may anwser this question and your previous one.

Collin

The example uses a class module as a data source. When code to set the DataSource and DataMember properties of two Binding objects executes, the class module's Initialize event occurs; two ADO recordsets are created in that event, and the names of the recordsets are added to the DataMembers collection. The GetDataMember event and its arguments are used to return data to the data consumer.

To try the example, on the Project menu, click References, and set a reference to Microsoft Data Binding Collection and Microsoft ActiveX Data Objects. On the Project menu, click Add Class Module. Change the name of the class to MyDataClass, and set the DataSourceBehavior property to vbDataSource. Then draw two TextBox controls on a form. Paste the code into the Form object's code module.

Option Explicit
' Declare the object variables, one for a Class module named MyDataClass,
' and two more for each BindingCollection object one for each
' recordset).

Private clsData As New MyDataClass ' Class module
Private bndColProducts As New BindingCollection ' Bindings Collection
Private bndColSuppliers As New BindingCollection ' Bindings Collection

Private Sub Form_Load()
' Set DataSource and DataMember properties for each Bindings
' collection object.
With bndColProducts
.DataMember = &quot;Products&quot;
Set .DataSource = clsData
.Add Text1, &quot;Text&quot;, &quot;ProductName&quot; ' Bind to a TextBox.
End With

With bndColSuppliers
.DataMember = &quot;Suppliers&quot;
Set .DataSource = clsData
.Add Text2, &quot;Text&quot;, &quot;CompanyName&quot; ' Bind to a TextBox.
End With

' Change the Caption of Command1
Command1.Caption = &quot;MoveNext&quot;

End Sub

Private Sub Command1_Click()
clsData.MoveNext
End Sub

Paste the code below into the MyDataClass module. The DataSourceBehavior property must be set to vbDataSource in order to see the GetDataMember event. Run the project.

Option Explicit
' Declare object variables for ADO Recordset and Connection objects.
Private WithEvents rsProducts As ADODB.Recordset
Private WithEvents rsSuppliers As ADODB.Recordset
Private cnNwind As ADODB.Connection

Private Sub Class_Initialize()
' Add strings to the DataMembers collection.
With DataMembers
.Add &quot;Products&quot;
.Add &quot;Suppliers&quot;
End With

' Set Recordset objects.
Set rsProducts = New ADODB.Recordset
Set rsSuppliers = New ADODB.Recordset
Set cnNwind = New ADODB.Connection

' Set the Connection object parameters.
With cnNwind
' The Nwind.mdb that comes with Visual Basic must be installed on
' the computer or the code will fail. Otherwise alter the path to
' find the file on the computer.
.Provider = &quot;Microsoft.Jet.OLEDB.3.51&quot;
.Open &quot;C:\Program Files\DevStudio\VB\Nwind.mdb&quot;
End With

' Open the recordset objects.
rsSuppliers.Open &quot;SELECT * FROM Suppliers&quot;, cnNwind, _
adOpenStatic, adLockOptimistic
rsProducts.Open &quot;SELECT * FROM Products&quot;, cnNwind, _
adOpenStatic, adLockOptimistic

End Sub

' The GetDataMember occurs when the DataSource property of a data
' consumer is set. In this case, the Bindings collection object is
' the consumer.
Private Sub Class_GetDataMember(DataMember As String, Data As Object)
Select Case DataMember
Case &quot;Products&quot;
Set Data = rsProducts
Case &quot;Suppliers&quot;
Set Data = rsSuppliers
Case &quot;&quot;
' Provide a default record source when no Data Member is specified.
Set Data = rsProducts
End Select

End Sub

Public Function MoveNext()
If rsProducts.EOF Then
rsProducts.MoveFirst
Else
rsProducts.MoveNext
End If
End Function

Private Sub rsProducts_MoveComplete(ByVal adReason As _
ADODB.EventReasonEnum, ByVal pError As ADODB.Error, adStatus As _
ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
' Keep the two recordsets in sync. The first textbox displays
' the supplier of the product. If the SupplierID for both
' recordsets are equivalent, no change needed. Otherwise,
' move to first record and test for SupplierID. This example
' is for demonstration only as the method is not the most
' efficient.

If rsSuppliers(&quot;SupplierID&quot;).Value = _
pRecordset(&quot;SupplierID&quot;).Value Then Exit Sub

rsSuppliers.MoveFirst
Do While Not rsSuppliers.EOF
If rsSuppliers(&quot;SupplierID&quot;).Value = _
pRecordset(&quot;SupplierID&quot;).Value Then
Exit Sub
Else
rsSuppliers.MoveNext
End If
Loop
End Sub

[sig][/sig]
 
Hello Collin,
Your a peach. I love ya man!!!

Your info got me over the hump. I was not understanding how the GetDataMember event was working and I was failing to assign the DataMember to the BindingCollection.

I had searched MSDN but never found the section you showed me.
Thank You Again,
TNN, Tom
TNPAYROLL@AOL.COM [sig]<p>TOM<br><a href=mailto:WWW.TNPAYROLL@AOL.COM>WWW.TNPAYROLL@AOL.COM</a><br>[/sig]
 
if you want to get a cleaner code, declare a public variable, i.e sDataMember,
to hold the string value( i.e. &quot;supplier&quot;, &quot;product&quot;) and pass it through the property methods. This way, you don't have to add code to class_initialize whenever you
declare a new data member.

Hence, in the getDataMember, do this,


select case sDataMember
case &quot;Products&quot;

case &quot;Supplier&quot;

case else

end select.

good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top