Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use For Next loop to refer to data fields

Posted on 2016-07-23
5
Medium Priority
?
56 Views
Last Modified: 2016-07-23
Hi

Using Microsoft Access (latest version)

By example database fields

[Item 1]  --- Text field
[Item 2]  --- Text field
[Item 3]  ---  Text field

etc ... They are identical except by their numerical component in the name.

I would like to be able to do the following.

dim xxx as long
for xxx = 1 to 3
    "[Item"  & xxx & "]".visible = true
next xxx

Open in new window

Any help would be appreciated.
0
Comment
Question by:peispud
5 Comments
 
LVL 31

Accepted Solution

by:
Helen Feddema earned 2000 total points
ID: 41725761
I think you are talking about Textbox controls, not fields, since only controls have a Visible property.  Here is some code to cycle through controls on a form and make them either visible or invisible:

Public Sub SwitchVisibility(frm As Access.Form, blnValue As Boolean)
'Created by Helen Feddema 21-Dec-2010
'Last modified by Helen Feddema 21-Dec-2010

On Error Resume Next

   Dim ctl As Access.Control
   
      For Each ctl In frm.Controls
         Debug.Print ctl.Name
         If ctl.Tag = "Switch" Then
            ctl.Visible = blnValue
         End If
      Next ctl
         
End Sub

Open in new window


This code uses the Tag property to determine whether the control's visibility should be changed.  You can run it from a form's code (just remove the frm argument and replace frm with Me in the code).  You could use something else rather than the value of the Tag property, but more information would be needed about what you are trying to do here.
0
 

Author Comment

by:peispud
ID: 41725771
thank you!
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41725772
it would be possible:
dim xxx as long for xxx = 1 to 3
controls("Item" & xxx ).visible = true
next xxx
0
 

Author Closing Comment

by:peispud
ID: 41725773
Thank you
0
 
LVL 58
ID: 41725857
To add on a bit to Guy's comment, you can also do that with fields in a record set:

rst("myFieldsName" & xxx) = <some value>

 So you'd get myFieldName1, myFieldName2, myFieldName3, etc

This type of syntax is what Access converts to internally when working with objects. so it's a tad faster as well in that it doesn't have to do the conversion.

Jim.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question