Solved

How to create a variable name from a string

Posted on 2011-03-15
3
376 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
ID: 35141579
"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
ID: 35141808
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
ID: 35141827
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

864 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

21 Experts available now in Live!

Get 1:1 Help Now