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

string substitution in VBA executable statements

Status
Not open for further replies.

CraigMan

Programmer
Apr 27, 2004
33
US
I've got 24 textboxes and 24 checkboxes on a form. An earlier routine sets the textbox visible property to True only if it contains text. This routine sets the checkbox visible property based on the textbox to which it relates.

This is the code I'm currently using:

If Me.[ANSWER A].Visible = True Then
Me.CheckA.Visible = True
Else
Me.CheckA.Visible = False
End If

If Me.[ANSWER B].Visible = True Then
Me.CheckB.Visible = True
Else
Me.CheckB.Visible = False
End If

If Me.[ANSWER C].Visible = True Then
Me.CheckC.Visible = True
Else
Me.CheckC.Visible = False
End If

et cetera! [thru X]

Obviously, I would like to write a for loop (for items A thru X). I'm used to writing code in scripting languages like unix shells, awk, sed, etc. In those languages I was able to use command line substitution. So I would love to be able to do this (syntax notwithstanding):

For Each Item In "A", "B", "C", ... , "X"

If Me.[ANSWER $Item].Visible = True Then
Me.Check$Item.Visible = True
Else
Me.Check$Item.Visible = False
End If

Next Item

Shouldn't this be easy to do? I'm not as familiar with the VB/VBA functions as I am with the functions of other languages. I thought maybe I could use EVAL?

Any ideas?


 
Something like this ?
For i = 65 to 90
Me.Controls("Check" & Chr(i)).Visible = Me.Controls("ANSWER " & Chr(i)).Visible
Next i

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I have no doubt that will work. I forgot about using the parenthetical syntax. In fact, the sytax using parentheses and double quotes was no doubt "invented" for precisely what I intended to do!
 
Here's the code I ended up with:

Dim Letter(1 To 24) As String, i As Integer

'fill character array
For i = 65 To 88 'Chr(65) To Chr(88) = A To X
Letter(i - 64) = Chr(i)
Next i

Dim varLetter As Variant

For Each varLetter In Letter
If Me("ANSWER " & varLetter).Visible = True Then
Me("Check" & varLetter).Visible = True
Else
Me("Check" & varLetter).Visible = False
End If
Next varLetter


Although your version is more elegant!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top