Solved

How to create a variable name from a string

Posted on 2011-03-15
3
375 Views
Last Modified: 2012-08-14
I need to create a variable or control name from a string.

For example, I am given a number which is 1, 2, 3, 4 or 5.  

If the number is 1, I need to reference the form control named cboWS1.
If the number is 4, I need to reference the from control named cboWS4.
etc.

I could use the Select Case statement for this simple example, but my application utilizes a dozen variables that need to have the selected number included in their names...
cboWS4
cboAG4
cboLT4
etc.

It's simple enough to concatenate the "1" to "cboWS" to create "cboWS1", but how do I use this string to create a statement such as...       Variable1 = cboWS1

Something to the effect...      Variable1 = CName("cboWS1")
0
Comment
Question by:David_W_R
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
"If the number is 1, I need to reference the form control named cboWS1."

You can pass the name to the controls collection to get a reference to the control:
Private Sub Command1_Click()
    Dim number As Integer
    number = 1
    
    Dim ctlName As String
    ctlName = "cboWS" & number
    
    Dim cb As ComboBox
    Set cb = Me.Controls(ctlName)
    ' ... do something with "cb" ....
    MsgBox ctlName & " --> " & cb.Value
End Sub

Open in new window

0
 

Author Closing Comment

by:David_W_R
Comment Utility
This does the job.  Thanks for the insight.  Got my mind out of the idle state.  Was it any challenge for yours?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
I haven't used MS Access in literally about a decade.  Had to open up Access 2010 and play with it to make sure I was right.  ;)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now