473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Adding controls to a VBA Collection

I am trying to add specific form controls to a VBA collection, so that
I can pass the entire collection to another function, and access the
individual controls later. I have tried several different syntax
variations. In each case, the receiving function produces the error
'Object Required' when I try to access any property of a control in
the collection. If I reference a collection item in the debugger
using its ordinal position, I see the value of the control. So, on
the surface at least, it appears that I am only adding the control's
'value' to the collection, not the control itself; or maybe my syntax
for retrieving the control's properties is at fault. Any help would
be greatly appreciated.

Dim colControls As Collection
Dim ctl As Control

Set colControls = New Collection

'I've tried this
Set ctl = Me.txtDosage
colControls.Add (ctl)

'And this
colControls.Add Me.controls("txtLabel")

'And this
colControls.Add Me.txtLabel
'The code to receive the collection look like this
Public Function fcnLogAudit(lngRecordType As enuRecordType,
lngRecordKey As Long, _
colControls As Collection) As Boolean

Dim ctl As Control

'Error occurs on this line. If I look at ctl(1) I can see the
control's
'value, but I can't access any of it's properties
For Each ctl In colControls

If ctl.Text <ctl.OldValue Then...
Nov 29 '07 #1
3 6977

"Nunzio" <gg@ai-host.netwrote in message
news:c9**********************************@n20g2000 hsh.googlegroups.com...
>I am trying to add specific form controls to a VBA collection, so that
I can pass the entire collection to another function, and access the
individual controls later. I have tried several different syntax
variations. In each case, the receiving function produces the error
'Object Required' when I try to access any property of a control in
the collection. If I reference a collection item in the debugger
using its ordinal position, I see the value of the control. So, on
the surface at least, it appears that I am only adding the control's
'value' to the collection, not the control itself; or maybe my syntax
for retrieving the control's properties is at fault. Any help would
be greatly appreciated.

Dim colControls As Collection
Dim ctl As Control

Set colControls = New Collection

'I've tried this
Set ctl = Me.txtDosage
colControls.Add (ctl)

'And this
colControls.Add Me.controls("txtLabel")

'And this
colControls.Add Me.txtLabel
'The code to receive the collection look like this
Public Function fcnLogAudit(lngRecordType As enuRecordType,
lngRecordKey As Long, _
colControls As Collection) As Boolean

Dim ctl As Control

'Error occurs on this line. If I look at ctl(1) I can see the
control's
'value, but I can't access any of it's properties
For Each ctl In colControls

If ctl.Text <ctl.OldValue Then...

Just use an array. Define an array of controls, as follows:

Dim arrControls(10) As Control

Replace 10 with whatever number, or leave blank for a dynamic array.

Then, in your function, define the parameter using a variant type, as
follows:

Public Function fcnLogAudit(lngRecordType As enuRecordType,
lngRecordKey As Long, arrControls As Variant) As Boolean

Use the UBound() function to get the upper bound of the array if you use a
dynamic array. And you're done!

Neil
Nov 30 '07 #2
On Nov 29, 8:40 pm, "Neil" <nos...@nospam.netwrote:
"Nunzio" <g...@ai-host.netwrote in message

news:c9**********************************@n20g2000 hsh.googlegroups.com...
I am trying to add specific form controls to a VBA collection, so that
I can pass the entire collection to another function, and access the
individual controls later. I have tried several different syntax
variations. In each case, the receiving function produces the error
'Object Required' when I try to access any property of a control in
the collection. If I reference a collection item in the debugger
using its ordinal position, I see the value of the control. So, on
the surface at least, it appears that I am only adding the control's
'value' to the collection, not the control itself; or maybe my syntax
for retrieving the control's properties is at fault. Any help would
be greatly appreciated.
Dim colControls As Collection
Dim ctl As Control
Set colControls = New Collection
'I've tried this
Set ctl = Me.txtDosage
colControls.Add (ctl)
'And this
colControls.Add Me.controls("txtLabel")
'And this
colControls.Add Me.txtLabel
'The code to receive the collection look like this
Public Function fcnLogAudit(lngRecordType As enuRecordType,
lngRecordKey As Long, _
colControls As Collection) As Boolean
Dim ctl As Control
'Error occurs on this line. If I look at ctl(1) I can see the
control's
'value, but I can't access any of it's properties
For Each ctl In colControls
If ctl.Text <ctl.OldValue Then...

Just use an array. Define an array of controls, as follows:

Dim arrControls(10) As Control

Replace 10 with whatever number, or leave blank for a dynamic array.

Then, in your function, define the parameter using a variant type, as
follows:

Public Function fcnLogAudit(lngRecordType As enuRecordType,
lngRecordKey As Long, arrControls As Variant) As Boolean

Use the UBound() function to get the upper bound of the array if you use a
dynamic array. And you're done!

Neil
Thanks Neil. That hadn't occurred to me.

I've been trying different approaches, and I've found that by using a
scripting dictionary object instead of a collection, the technique
that I've been using now works. I like your idea better though.

-Dean
Nov 30 '07 #3

"Nunzio" <gg@ai-host.netwrote in message
news:f1**********************************@s19g2000 prg.googlegroups.com...
On Nov 29, 8:40 pm, "Neil" <nos...@nospam.netwrote:
>"Nunzio" <g...@ai-host.netwrote in message

news:c9**********************************@n20g200 0hsh.googlegroups.com...
>I am trying to add specific form controls to a VBA collection, so that
I can pass the entire collection to another function, and access the
individual controls later. I have tried several different syntax
variations. In each case, the receiving function produces the error
'Object Required' when I try to access any property of a control in
the collection. If I reference a collection item in the debugger
using its ordinal position, I see the value of the control. So, on
the surface at least, it appears that I am only adding the control's
'value' to the collection, not the control itself; or maybe my syntax
for retrieving the control's properties is at fault. Any help would
be greatly appreciated.
Dim colControls As Collection
Dim ctl As Control
Set colControls = New Collection
'I've tried this
Set ctl = Me.txtDosage
colControls.Add (ctl)
'And this
colControls.Add Me.controls("txtLabel")
'And this
colControls.Add Me.txtLabel
'The code to receive the collection look like this
Public Function fcnLogAudit(lngRecordType As enuRecordType,
lngRecordKey As Long, _
colControls As Collection) As Boolean
Dim ctl As Control
'Error occurs on this line. If I look at ctl(1) I can see the
control's
'value, but I can't access any of it's properties
For Each ctl In colControls
If ctl.Text <ctl.OldValue Then...

Just use an array. Define an array of controls, as follows:

Dim arrControls(10) As Control

Replace 10 with whatever number, or leave blank for a dynamic array.

Then, in your function, define the parameter using a variant type, as
follows:

Public Function fcnLogAudit(lngRecordType As enuRecordType,
lngRecordKey As Long, arrControls As Variant) As Boolean

Use the UBound() function to get the upper bound of the array if you use
a
dynamic array. And you're done!

Neil

Thanks Neil. That hadn't occurred to me.

I've been trying different approaches, and I've found that by using a
scripting dictionary object instead of a collection, the technique
that I've been using now works. I like your idea better though.

-Dean
Great! Glad it worked for you.

Neil
Nov 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
5
by: Jeremy Ames | last post by:
I have run into a major road block with my page development projects. I am trying to add a literal control to my asp.net page and I got an error stating that the control could not be added because...
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
6
by: Juan Pedro Gonzalez | last post by:
I wanted to add a Combobox to a toolbar... Kind of the look you get on VisualStudio's toolbar. I've been able to find some VB 6 samples, but the placeholder option is no longer available for...
1
by: cashdeskmac | last post by:
I have created a Datagrid in code, added it to the Controls collection and set it's DataSource. All works fine. I try the same thing with a button (create an instance, add it to the controls...
9
by: Kadett | last post by:
Hi all, I have following problem: I'm creating a ListView (Details) control at run-time and filling it with some records (let's say 10 000). This operation seems to be quite fast, but when I call...
4
by: John Dalberg | last post by:
I am looking at a problem which is preventing my code to get a reference to any asp control inside a div section which has a runat=server attribute. This can be reproduced in a simple test:...
5
by: zswanson | last post by:
Hello all, I'm trying to change the propeties of 4 controls all at once using a collections object. Is this correct? I have to add them to a custom collection first, because I don't want all the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.