Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / April 2005



Tip: Looking for answers? Try searching our database.

Help with Adding data to a class collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MichaelJohnson168@hotmail.com - 28 Apr 2005 21:31 GMT
Using an Excel Spreadsheet.

I am trying to add an orders information via a class object into a
collection but I am getting the error message below.
-----------------------------------------------
Object variable or With block variable not set
-----------------------------------------------

The error is occuring in the collection module in its Add function.
Snippent of error location is below.

On Error GoTo AddError
   *****OrdersP.Add aorder, aorder.IDOrderP***** Error occurs here
   Add = True
   Exit Function

More Code is Below.

Worksheet Variables
--------------------------------
Dim stList As Class2
Dim aorder As Class1
--------------------------------
End

Command Button Event Procedure
--------------------------------
Private Sub Cmd2_Click()

Set aorder = New Class1
Set stList = New Class2

aorder.IDOrderP = 10
aorder.IDOrderItemP = "hhjk"
aorder.ItemQuantityP = 1
aorder.ItemP = "fdfd"
aorder.ItemDetailsP = "fdj"
aorder.FoodTypeP = "fdjk"
stList.Add aorder

Set aorder = Nothing

End Sub
--------------------------------
End

Orders Collection
--------------------------------
Option Explicit

Private OrdersP As Collection
--------------------------------
Public Property Get Count() As Integer
Count = OrdersP.Count
End Property
--------------------------------

Public Property Get Orders() As Collection
Set Orders = OrdersP
End Property
--------------------------------

Public Function Add(aorder As Class1) As Boolean
   On Error GoTo AddError
   OrdersP.Add aorder, aorder.IDOrderP
   Add = True
   Exit Function

AddError:
MsgBox Error$, vbInformation, "Class2.add"
Add = False

End Function
--------------------------------
Stoil Marinov - 28 Apr 2005 21:51 GMT
Hi Michael,

Before using the collection object, you need to create it:

Set OrdersP = New Collection

And only then you can use the OrdersP object properties and methods.

So, if you modify your Add Function to first make sure that the collection object exist:

Public Function Add(aorder As Class1) As Boolean
   If OrdersP is Nothing Then
      'Object does not exist.
      'Create it now.

        Set OrdersP = New Collection
   end if
   
   'Rest of your code follows
   
end function

Also, in your Count property, I would put this check to handle tha case when the property is called before the collection has been created.

Regards,

Stoil

> Using an Excel Spreadsheet.
>
[quoted text clipped - 70 lines]
> End Function
> --------------------------------
MichaelJohnson168@hotmail.com - 28 Apr 2005 23:44 GMT
Hello and thanks Stoil

I plugged in the code that you gave and it seems to have worked to a
certain degree. However it is now generating the Error below.
-------------
Type Mismatch
-------------

It is still occuring at the line stated previously

On Error GoTo AddError

Error source
---------------------------------------------
*****OrdersP.Add aorder, aorder.IDOrderP*****
---------------------------------------------
   Add = True
   Exit Function

> Hi Michael,
>
[quoted text clipped - 19 lines]
>
> Also, in your Count property, I would put this check to handle tha case when the property is called before the collection has been
created.

> Regards,
>
[quoted text clipped - 74 lines]
> > End Function
> > --------------------------------
Steve Gerrard - 29 Apr 2005 02:44 GMT
> Hello and thanks Stoil
>
[quoted text clipped - 14 lines]
>    Add = True
>    Exit Function

The collection Add method doesn't like being given numbers as keys, It wants
strings. So
   OrdersP.Add aorder, CStr(aorder.IDOrderP)
will get it done. If you want to retrieve it, be sure to use
   OrdersP.Item(CStr(10))
since using .Item(10) would try to find the 10th item.

Also, the easiest place to create the private collection in a custom collection
class is the Initialize event:
Private Sub Class_Initialize()
   Set OrderP = New Collection
End Sub
This will be fired when the instance of your class is created.

MichaelJohnson168@hotmail.com - 29 Apr 2005 02:30 GMT
Disregard my previous response. In my "non"-collection class
("class1"), I did not set any "Get" or "Let" statements. That was
probably why I got the "Type Mismatch" error.

> Hi Michael,
>
[quoted text clipped - 19 lines]
>
> Also, in your Count property, I would put this check to handle tha case when the property is called before the collection has been
created.

> Regards,
>
[quoted text clipped - 74 lines]
> > End Function
> > --------------------------------
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.