Add variable to a Collection ByRef (1 Viewer)

joeKra

Registered User.
Local time
Today, 18:14
Joined
Jan 24, 2012
Messages
208
Hi Guys

is it possible to add a variable to a collection ByRef as i wanted to be able to set the value via collection and be cloned to the actual veriable.

thanks in advance

my Code:
Code:
    m_colItem.Add ShullContactID, "ShullContactID"
    m_colItem.Add LocationID, "LocationID"
    m_colItem.Add Title, "Title"
    m_colItem.Add Full_Name, "Full_Name"
    m_colItem.Add Phone, "Phone"
    m_colItem.Add Phone2, "Phone2"
    m_colItem.Add Email, "Email"
    m_colItem.Add DateCreated, "DateCreated"
    m_colItem.Add User, "User"
 

Cronk

Registered User.
Local time
Tomorrow, 08:14
Joined
Jul 4, 2013
Messages
2,774
I think you are confusion user defined type variables with classes.

A collection consists of members of the same object, although each object can have different properties.
 

MarkK

bit cruncher
Local time
Today, 15:14
Joined
Mar 17, 2004
Messages
8,197
No, it only works that way with objects. Object variables are only really pointers to class instances, so you can have multiple object variables that all "point" to the same instance. In a form, for example, you could do . . .
Code:
dim frm1 as form
dim frm2 as form

set frm1 = me
set frm2 = me

frm2.caption = "testing 123"
debug.print frm1.caption
debug.print me.caption

In which case Me, frm1, and frm2 are all just pointers to the same instance of the class.

But it doesn't work that way with simple data types. Any new variable is a new discrete memory location, not a pointer, and so you can't synchronize simple variables in the way you want to.

Although with a collection of simple variables you can still reference them by name, which is an advantage is some ways over an array, which only allows a numeric index.
 

stopher

AWF VIP
Local time
Today, 23:14
Joined
Feb 1, 2006
Messages
2,395
Like Cronk and Mark said you can't do want you want with simple variables. But it is quite common to create a class and then store multiple instances of the class object in a collection which you can then reference via the collection.
Create a class and name it clsContact. Then add this to the class:

Code:
Public ShullContactID As Integer        
Public LocationID As String
Public Title As String
etc...


So now in your main module you can do this:
Code:
Dim contact As clsContact
'create a contact
Set contact = New clsContact
contact.ShullContactID = 1      
contact.LocationID = "a LocationID"
But the above only gives you one contact and maybe you want many and to be able to reference each. So we can use a collection (or an array or dictionary)
Code:
Dim m_Contacts As collection

Dim contact As clsContact

Set m_Contacts = New collection

'create a contact
Set contact = New clsContact
contact.ShullContactID = 1
contact.LocationID = "a LocationID"
contact.Title = "a Title"
'etc

'add the first contact to the collection
m_Contacts.Add contact, "contact1"

'create another contact
Set contact = New clsContact
contact.ShullContactID = 2
contact.LocationID = "another LocationID2"
contact.Title = "another Title"
'etc

'add the second contact to the collection
m_Contacts.Add contact, "contact2"

'so now we can refernce the first contact like this:
Debug.Print m_Contacts("contact1").LocationID

'or like this
Debug.Print m_Contacts(1).LocationID

'And we can change the values of a contact e.g. like this
m_Contacts(2).LocationID = "a different location ID"

'Collections are designed to be looped, so we can do this:
For Each contact In m_Contacts
    Debug.Print contact.Title
Next contact

In the above I created a new contact one at a time but typically you would do this in the loop. Make sure the Set Contact = New clsContact is in the loop so you get a new object instance each time.

hth
 

joeKra

Registered User.
Local time
Today, 18:14
Joined
Jan 24, 2012
Messages
208
Hi Guys

Thanks for taking the time and reply... everybody's effort is appreciated !
Basically, the answer to my question is "NO". meaning - I can't pass a variable byref. But I would like to comment that this statement isn't true - "you can't pass a variable by ref" - because in case of passing a variable in a function you can indeed pass by ref - setting the value and get back the new value set by called sub/function. but fact being that to a collection it's impossible to pass data type variables by ref - regretfully.

Now, I would like to explain where I came across so maybe someone has a better idea. Am using a class module where I have all fields as properties and I've added a collection where I'm adding all the property' name /values so when I open the form it loops the collection and sets the unbound controls to the precise values.
EDIT: as we all know class modules does not have a built-in collection property

What am trying to do is, when I'm saving the data i should be able to save the data to the collection rather than writing a line for each property,but the problem is that there's no way how to pass back the data from the collection to the properties and the properties added to the collection are not added byref so it has no connection, let's say, me.locationID it will show the value set at first but not what set within the collection because it's not tied by pointer. Is there any way to get around this or this is just another "welcome to access" ?

Thanks in advance !
 

MarkK

bit cruncher
Local time
Today, 15:14
Joined
Mar 17, 2004
Messages
8,197
I don't see what you are trying to do. Why save data from an unbound form to a collection in a class? In both cases your data is lost if you don't at some point save it to a table, so it seems like a sideways move.

You could add the unbound controls on your form to a collection in your class. Then your class would support a "byref" relationship to the data, because form edits would be immediately present in your collection. But if you do that, what problem have you really solved? You still lose all your data when you close your form.

Hope this helps,
 

Cronk

Registered User.
Local time
Tomorrow, 08:14
Joined
Jul 4, 2013
Messages
2,774
joeKra: "as we all know class modules does not have a built-in collection property"

Wrong. A class object can have a collection defined as a property.

Say I have a system to handle a training situation where I have a number of courses. I could set up a class for particular session for instances of a particular course eg Introduction to Access session commencing Term 1 2015.

I could set up a class for each student.

I could have a collection of students attending a particular course and make this collection a property of my class session.

Note, I'm not necessarily advocating this for all such similar situations but I might just use such an approach in particular circumstances.

And as Mark points out, any persisting data has to be stored in tables. However that's easily covered by adding Load and Save method to classes.
 

joeKra

Registered User.
Local time
Today, 18:14
Joined
Jan 24, 2012
Messages
208
joeKra: "as we all know class modules does not have a built-in collection property"

Wrong. A class object can have a collection defined as a property.

sorry for not being expressive enough.... i meant to say that the class module it-self does not have a collection which holds all the properties like a form does (me.controls(item)) or a table (me.fields(fld))...

I don't see what you are trying to do

In Short
To get a collection of all the properties defined in class module By Ref (like a form \ table etc... have)

You still lose all your data when you close your form

well, i never thought of not explicitly saving the data to a table, i mean, i am not :D i just didn't mention it as it was not part of my problem... of course after setting the properties via collection (if possible) i will then save the data to the table, i were just looking for a way to make things generic (looping is much easier than setting every form their precise properties, which can sometimes lead to a lot of lines (think, 25 fields or so)) but unfortunately it doesn't look like i will get any positive reply on that.

I know it will not help me one way or the other. but i am just curios why we can pass a Date-type variable to a sub\function byRef and to add a Date-type Var to a collection byRef is such a big deal.

Anyhow. i don't want to spend anyone's time for this question anymore as it's almost clear that, "Yes, we are limited" and will live with that.

Thanks for the assistance.

Joseph
 
Last edited:

Users who are viewing this thread

Top Bottom