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

Dynamic Control Names - possible?? 1

Status
Not open for further replies.

RealKiwi

Technical User
Feb 26, 2004
41
0
0
NZ
I know I could use tags and controls collection to achieve this, but I'm also curious as to whether anyone has used dynamic control names in a loop.

I want to update several fieldnames with the only difference in their control names being a number on the end eg. "DateFx" where x is a number 1 through 13

Code:
Dim vLoop, vFieldName, calcDate

For vLoop = 1 To 13
    vFieldName = ("DateF" & vLoop)
    calcDate = Me.someFieldName + someExpression
    Me![vFieldName] = calcDate
Next

This code didnt work but maybe Im close and someone can point me in the right direction...

as always, thanks
 
Hi RealKiwi,

I use the following code to clear the previous selections made in all checkboxes on a form ... you should be able to use the same sort of theory.

Sub ClearSelections()
Dim frm As Form, ctl As Control, dbs As Database, rst As Recordset
Set frm = Forms!frmMainMenu

' Enumerate Controls collection of menu form, setting each checkbox
' value to false (not selected) with the exclusion of default items.

For Each ctl In frm.Controls
If ctl.ControlType = 106 Then ' I am a checkbox
If ctl.Enabled = True Then ' I am enabled for user selection
ctl.Value = 0 ' Make me unchecked
End If
End If
Next ctl

' refresh screen display
frm.Repaint

End Sub

Hope this helps [pipe]
 
Thanks,
That was exactly the type of code I was trying to avoid. Did you see the first sentence of my post? Never mind - onwards and upwards.

cheers
 
Hi!

Yes, this is possible, using a sligthtly different notation:

[tt]Dim vLoop as Long
Dim vFieldName as String
Dim calcDate as Date

For vLoop = 1 To 13
vFieldName = ("DateF" & vLoop)
calcDate = Me("someFieldName" & vLoop)
Me(vFieldName) = calcDate
Next[/tt]

The Me(varname) reference type allows for dynamic addressing of controls.

Roy-Vidar
 
Thanks Roy, as always right on the money.

cheers RK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top