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

Can't get collection to work? What am I doing wrong? 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I have a global var 'oSerials' declared as a collection.

I use the following code to populate it
Code:
    Set oSerials = New Collection
    
    Do While Not rs.EOF
        oSerials.Add rs.Fields("serial")
        rs.MoveNext
    Loop

so far so good, however, when I later try to access the collection I get
object invalid or no longer set

I'm accessing it like this...
Code:
Dim oSerial As Object
    Dim bOK As Boolean

    bOK = False

    For Each oSerial In oSerials
        If oSerial.Value = getSerial Then
            bOK = True
        End If
    Next

what am I missing and why isn't there a 'contains' method of the collection class?

Thanks,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 

Code:
Dim oSerial As Object
declares an ENTIRELY DIFFERENT oSerial that your GLOBAL, which should have been declared
Code:
PUBLIC oSerial As Object
at the top of a MODULE and not in a procedure.


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
hi Skip,

No the global is declared in the 'Global Code' as
Code:
Global oSerials As Collection

What i have found is the sub being called to populate the collection works, and if I use
Code:
msgbox NameType(oSerials)
in the sub that populates the colleciton I get
Collection

However, the function that later tries to access the collection , if I use the same msgbox i get

So the collection seems to be getting garbage collected , yet it is a Global for the entire application?

Why is the collection being lost?

Cheers,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I'd replace this:
oSerials.Add rs.Fields("serial")
with this:
oSerials.Add rs.Fields("serial").Value

And this:
If oSerial.Value = getSerial Then
with this:
If oSerial = getSerial Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
agh , i think you could be right PHV, just realised that if I am passing the record set object with
Code:
rs.fields("col")
NOT
Code:
rs.fields("col").value

and as i clear the recordset after use with
Code:
set rs = nothing,
i'm trashing the collection!

That was driving me crazy, yet is now so obvious!

Much appreciated PHV!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
If I understand correctly you missed Skips point. VB will allow you to name local and public variables with the same name.

public oSerial as collection '(dimension a public object)

now assume in some other procedure you instantiate the oSerial object and add items

however if now you do this as you show
public sub someSub()
dim oSerial as collection 'dimension a public object with same name
msgbox OSerial(0).property
'This now assumes you are talking about the local oSerial and it is not set
end sub

you need to remove the dim statement

simpler example

public x as integer

public sub test()
x = 100
msgbox x 'will get 100
call sub1() 'msgbox returns zero
end sub

Public sub1()
dim x
msgbox x
end sub
 
I'm accessing it like this...
Dim oSerial As Object
Dim bOK As Boolean

bOK = False

For Each oSerial In oSerials
If oSerial.Value = getSerial Then
bOK = True
End If
Next

so bottom line you have created a local oSerial and it is not ever set. You method thinks you refer to the local oSerial not the public/global.
 
there is no global oSerial, it's oSerials , it's OK, PHV got it spot on ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top