Solved

How to create a variable name from a string

Posted on 2011-03-15
3
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

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 86

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange - Deleted Resource Info appears in Azure Portal 6 98
String manipulation in Visual Basic 7 90
Recommendation vb6 to vb.net or others 14 241
Why use this lambda? 12 97
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 …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…

759 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