Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use For Next loop to refer to data fields

Posted on 2016-07-23
5
Medium Priority
?
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 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