Solved

Use For Next loop to refer to data fields

Posted on 2016-07-23
5
31 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 500 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 142

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 57
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now