For loop

  • Thread starter Thread starter toga
  • Start date Start date
T

toga

Hello, I am trying to learn about using visual basic for
accrss. I have created a form with 2 lables and 2 text
boxes on it (the names given to the lables and text boxes
is 1, 2, 3 and 4). On the click of a button I wanted to
set their properties ".Visible" to false.

This works fine if I used a seperate line of VB as below,
where the box [ ] would contain the corresponding number
e.g. [1] or [2] etc.:

Forms![EmployeesLogin]![1].Visible = False

However, I then tried to use a "For Loop" to loop through
and set the properties to false, without success e.g.

Dim F As String
Dim i As Integer
For i = 1 To 4
F = "Forms![EmployeesLogin]!["
F = F & i & "].Visible = False"
MsgBox F
Next

The code places the correct corresponding number in the
[ ] but doesn't execute. Any ideas please?

Many thanks
 
toga said:
Hello, I am trying to learn about using visual basic for
accrss. I have created a form with 2 lables and 2 text
boxes on it (the names given to the lables and text boxes
is 1, 2, 3 and 4). On the click of a button I wanted to
set their properties ".Visible" to false.

This works fine if I used a seperate line of VB as below,
where the box [ ] would contain the corresponding number
e.g. [1] or [2] etc.:

Forms![EmployeesLogin]![1].Visible = False

However, I then tried to use a "For Loop" to loop through
and set the properties to false, without success e.g.

Dim F As String
Dim i As Integer
For i = 1 To 4
F = "Forms![EmployeesLogin]!["
F = F & i & "].Visible = False"
MsgBox F
Next

The code places the correct corresponding number in the
[ ] but doesn't execute.


You can not construct VBA statements at run time.

The correct syntax for this would be:

Forms![EmployeesLogin].Controls(CStr(i)).Visible = False

There may very well be some confusion since you can index
into a collection either by its numeric position in the
collection, or by the item's index string. If you had named
the controls c1, c2, c3 and c4, it would be unambiguous:

Forms![EmployeesLogin].Controls("c" & i).Visible = False

In your example the button's code is in the same form as the
other controls, so the full form references can be avoided
by using Me. Also, the form object's default property is
Controls, so that is optional. Taking advantage of this
info:

Me("c" & i).Visible = False
 
-----Original Message-----
toga said:
Hello, I am trying to learn about using visual basic for
accrss. I have created a form with 2 lables and 2 text
boxes on it (the names given to the lables and text boxes
is 1, 2, 3 and 4). On the click of a button I wanted to
set their properties ".Visible" to false.

This works fine if I used a seperate line of VB as below,
where the box [ ] would contain the corresponding number
e.g. [1] or [2] etc.:

Forms![EmployeesLogin]![1].Visible = False

However, I then tried to use a "For Loop" to loop through
and set the properties to false, without success e.g.

Dim F As String
Dim i As Integer
For i = 1 To 4
F = "Forms![EmployeesLogin]!["
F = F & i & "].Visible = False"
MsgBox F
Next

The code places the correct corresponding number in the
[ ] but doesn't execute.


You can not construct VBA statements at run time.

The correct syntax for this would be:

Forms![EmployeesLogin].Controls(CStr(i)).Visible = False

There may very well be some confusion since you can index
into a collection either by its numeric position in the
collection, or by the item's index string. If you had named
the controls c1, c2, c3 and c4, it would be unambiguous:

Forms![EmployeesLogin].Controls("c" & i).Visible = False

In your example the button's code is in the same form as the
other controls, so the full form references can be avoided
by using Me. Also, the form object's default property is
Controls, so that is optional. Taking advantage of this
info:

Me("c" & i).Visible = False

--
Marsh
MVP [MS Access]
.
Marsh, I have now got the code working, many thanks Toga.
 
Back
Top