By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,495 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,495 IT Pros & Developers. It's quick & easy.

Variable Pointers

P: n/a
Hi,
Is there a way to use something similar to C pointers in VBA? My reasoning
is as follows:
Imagine I have 10 items on a report, all called txtItem1,
txtItem2...txtItem10. I want to perform some operation to all of them in
VBA, and I'd like to use a for loop, and perform an operation on the
variable "txtItem" & i. In C perhaps you could operate on a pointer, and
direct the pointer to "txtItem" & i. Is there a way to do this in VBA?
Thanks.
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Yes, this can be done in VBA, but you have to use one of the alternate
syntax methods of referring to the control.

For i = 1 To 10
Me.Controls("txtItem" & i) = "whatever"
Next i

--
Wayne Morgan
MS Access MVP
"Raphi" <le**@DELETEME.optFAKEonline.net.REMOVE> wrote in message
news:Ff*****************@fe12.lga...
Hi,
Is there a way to use something similar to C pointers in VBA? My reasoning
is as follows:
Imagine I have 10 items on a report, all called txtItem1,
txtItem2...txtItem10. I want to perform some operation to all of them in
VBA, and I'd like to use a for loop, and perform an operation on the
variable "txtItem" & i. In C perhaps you could operate on a pointer, and
direct the pointer to "txtItem" & i. Is there a way to do this in VBA?
Thanks.

Nov 13 '05 #2

P: n/a
"Wayne Morgan" <co***************************@hotmail.com> wrote in
news:DA***************@newssvr25.news.prodigy.net:
Yes, this can be done in VBA, but you have to use one of the
alternate syntax methods of referring to the control.

For i = 1 To 10
Me.Controls("txtItem" & i) = "whatever"
Next i


In a report, this will be fine, but in a form, you'd be better off
creating a custom collection and populating that with the relevant
controls. You then don't need to have special naming conventions --
only your code that populates the custom collection needs to know
which controls to put in the collection (I often use the .Tag
property for keeping track of which controls belong together).

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #3

P: n/a
Raphi wrote:
Hi,
Is there a way to use something similar to C pointers in VBA? My reasoning
is as follows:
Imagine I have 10 items on a report, all called txtItem1,
txtItem2...txtItem10. I want to perform some operation to all of them in
VBA, and I'd like to use a for loop, and perform an operation on the
variable "txtItem" & i. In C perhaps you could operate on a pointer, and
direct the pointer to "txtItem" & i. Is there a way to do this in VBA?
Thanks.

The nearist thing I can think of is Type. Here's an example from Access
Help.

Type EmployeeRecord ' Create user-defined type.
ID As Integer ' Define elements of data type.
Name As String * 20
Address As String * 30
Phone As Long
HireDate As Date
End Type
Sub CreateRecord()
Dim MyRecord As EmployeeRecord ' Declare variable.

' Assignment to EmployeeRecord variable must occur in a procedure.
MyRecord.ID = 12003 ' Assign a value to an element.
End Sub

You can make EmployeeRecord an array of types.

Dim typEmp() As EmployeeRecord
Dim intEmpCnt As Integer
For intEmpCnt = 0 To 3
ReDim Preserve typEmps(intEmpCnt)
typEmp(intJobCnt).ID = intEmpCnt
Next
Nov 13 '05 #4

P: n/a
What is the operation to accomplish? I tend to think too procedurally,
this looks to me as if you could be thinking too procedurally here too.

Raphi wrote:
Hi,
Is there a way to use something similar to C pointers in VBA? My reasoning
is as follows:
Imagine I have 10 items on a report, all called txtItem1,
txtItem2...txtItem10. I want to perform some operation to all of them in
VBA, and I'd like to use a for loop, and perform an operation on the
variable "txtItem" & i. In C perhaps you could operate on a pointer, and
direct the pointer to "txtItem" & i. Is there a way to do this in VBA?
Thanks.


--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
For human replies, replace the queue with a tea

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.