Do Loop

K

Katrina

I purposely number my text boxes and button in a
sequential manner so that I could call them out more
easily. There are many times in the code where I have to
do the same thing to text1 through text12.

I would like to be able to count numbers and say something
to the effect of:

number = 1
For number = 1 to 12 do
me."text"&number.visible = true
next number

But I keep getting errors when i try to do this. I don't
care if I use the do loop, I just want to avoid having to
have 12 lines of code for each instance... Any suggestions?
 
G

Guest

Should number be a string?
-----Original Message-----
For number = 1 To 12
Me.Controls("text" & number).Visible = True
Next number


--
Ken Snell
<MS ACCESS MVP>

suggestions?


.
 
K

Ken Snell

Not necessary to cast it as such; ACCESS will do that for you. But you can
do it yourself if you wish:

Me.Controls("text" & CStr$(number)).Visible = True
 
T

Tim Ferguson

Me.Controls("text" & CStr$(number)).Visible = True

me.controls(format$(number, """text""00")).visible

but only if the OP has had the foresight to number the controls text01,
text02, text03 and so on.

Tim F
 
K

Katrina

Access tells me that it can not find the field controls
refered to in the code. I have visual basic 6, is this
maybe why it doesn't work?
 
K

Ken Snell

I'm confused. Are you doing this in ACCESS, using ACCESS forms, or are you
doing this in Visual Basic, using VB forms?
 
K

Katrina

I have access forms. I am trying to use VB to manipulate
the forms. For instance if one of the controls from
text01 to text12 is "n/a" then I want the visible setting
set to false. I would like this to happen each time the
form is opened.
 
K

Ken Snell

By "n/a", do you mean the value in the textbox? Or that the textbox does not
exist?

Assuming you mean the value is "n/a", try this code in the form's OnLoad
event:

Private Sub Form_Load()
Dim intNumber As Integer
On Error Resume Next
For intNumber = 1 To 12
Me.Controls("text" & Format(intNumber, "00")).Visible = _
(Me.Controls("text" & Format(intNumber, "00")).Value <> _
"n/a")
Err.Clear
Next intNumber
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top