Solved

How to create a variable name from a string

Posted on 2011-03-15
3
377 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

786 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