Text boxes...

aweizd

Registered User.
Local time
Today, 02:19
Joined
Apr 8, 2005
Messages
12
Hi,

In a form I have 10 text boxes (TextBox1 - TextBox10), which are set to invisible.

With a loop I want to change the "Visible" property of the first X text boxes.

e.g.

X = 5

Dim BoxName As String
Dim i as Integer

i = 1

Do Until i = X
BoxName = "TextBox" & i
i = i + 1
Me.BoxName.Visible = True
Loop

I keep getting the error message that "BoxName" in the "Me.BoxName..." line is not a method or data member. So my question is: How do I tell Access that by BoxName I actually mean the value of the variable (TextBox1 - TextBoxX)? Or is my idea of doing this totally wrong? Can anyone help me?

Thanks in advance
Luke
 
Last edited:
Luke

Have you tried using the full name rather than me! eg Forms!frmFormName!txtTextBox1.visible

Does your text box names appear in the the left hand of the two list (object box ) boxes above the code window?
 
Copy this into your form's module and call it like this:

Code:
Call ToggleTextboxes(5, False)

Code:
Private Sub ToggleTextboxes(bytNumber, booVisible)
    Dim bytCount As Byte
    For bytCount = 1 To bytNumber
        Me("TextboxName" & bytCount).Visible = booVisible
    Next bytCount
End Sub
 
Last edited:
aweizd said:
Do Until i = X
BoxName = "TextBox" & i
i = i + 1
Me.BoxName.Visible = True
Loop

There's a few problems I see with this.

The first is that you can't call a string when you're referencing an object in this way. As McAbney points out you will have to reference the object using the function call.

Next, when using an increment loop, it'd probably be better to use a for loop just for readability reasons.

Code:
For i = 1 to 5
    BoxName = "TextBox" & i
    Me(BoxName).Visible = True
Next
 
If you plan to keep changing the textboxes to visible/invisible, do as SJ suggested and make a module/function that you can call. This will save you time in the future when writing the code again... and it will reduce your code, increasing it's readability and making it look less cluttered.

Good luck!
 

Users who are viewing this thread

Back
Top Bottom