"Borrowed" name for Form's Text Box... how to?

Flecha Negra

Registered User.
Local time
Today, 17:20
Joined
Dec 4, 2013
Messages
10
Hi

Is it possible to give a name to a Form's Text Box with VBA code like this:

- I have a form with 31 Text Box. Each one as a name: TJan1, TJan2,...,TJan31
- I know the difference between two dates, for instance: 5
- I have a For Next to cycle from 1 to 5
- The Text Boxes are yellow rectangles, that are hidden, but I want to make them visible to show my year's planning

So... here is where I have THE problem... how to tell my Form that I need, for instance, TJan4 to be visible? I have tried something that I learned years ago in Clipper' language... the &(varName) but... no success.

Here's my code:

Dim i, ndias As Integer
Dim arrDias() As String
Dim arraySize As Integer
Dim nomeCampo As String

ndias = 5
arraySize = ndias
ReDim arrDias(1 To arraySize)

For i = LBound(arrDias) To UBound(arrDias)
nomeCampo = "TJan" & CStr(i)
me.&[nomeCampo].visible=true 'doesn't work and...

' I don't want to make this:

' TJan01.Visible = True
' TJan02.Visible = True
' TJan03.Visible = True
' TJan04.Visible = True
' TJan05.Visible = True

Next i

Is this possible? Can I "borrow" the content of my var "nomeCampo" to:

Me.<TextBox_Name>.Visible = True

I appreciate any help. Thanks.
 
It would make your code easier to read if you put it in Code tags. Use the # button in the advanced editing window to do this

Your code

Code:
Dim i, ndias As Integer
Dim arrDias() As String
Dim arraySize As Integer
Dim nomeCampo As String

ndias = 5
arraySize = ndias
ReDim arrDias(1 To arraySize)

For i = LBound(arrDias) To UBound(arrDias)
nomeCampo = "TJan" & CStr(i)
me.&[nomeCampo].visible=true 'doesn't work and...

' I don't want to make this:

' TJan01.Visible = True
' TJan02.Visible = True
' TJan03.Visible = True
' TJan04.Visible = True
' TJan05.Visible = True

Next i

Is this possible? Can I "borrow" the content of my var "nomeCampo" to:

Me.<TextBox_Name>.Visible = True
I suspect your problem may be that CStr(1) is returning 1,2... and not 01,02 ....

Check exactly what you are setting nomeCampo to. Use a temporary line of code like

Code:
     Msgbox(nomeCampo)
to see what is going on.
 
Close. Try ...
Code:
For i = LBound(arrDias) To UBound(arrDias)

  Me.Controls("TJan" & Format(i, "00")).Visible = True

Next i

Using "00" ensures that Format() returns a string with a leading zero if appropriate.
 
Thanks to you Rabbie for your suggestion but I had already done that. Besides the code was wrong in one character: it's not "TJan01" but "TJan1" so the approach to solve the problem it's another.

Nascombe gave me the solution. I've just modify the format from "00" to "0" and it works.

Many thanks to both of you. This problem is solved. I'll put another question in another thread.
 
Happy to hear your problem is solved:)
 

Users who are viewing this thread

Back
Top Bottom