Solved

Use For Next loop to refer to data fields

Posted on 2016-07-23
5
41 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add records to a form to a table 11 41
Can not open the Access Help ? 3 55
aggregate query? 20 52
MS Access Delete All Excel sheets 1 10
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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 …
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…

803 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