Results 1 to 6 of 6
  1. #1
    kiwichick is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2020
    Posts
    31

    Loop through list of text values

    I have a report where I'm using code to hide a label if the corresponding field is empty. This is the code:



    Code:
    Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    
        Me.[myfield1].Visible = Not IsNull(Me.[myfield1])
        Me.[myfield2].Visible = Not IsNull(Me.[myfield2])
        Me.[myfield3].Visible = Not IsNull(Me.[myfield3])
        Me.[myfield4].Visible = Not IsNull(Me.[myfield4])
    
    End Sub
    I've added a line for each of the necessary fields. This works but what I'd like to be able to do is have a list I can add the fields to and then loop through the list. Something like this:


    Code:
    Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    
        Dim strFieldNames as String
        strFieldNames =
            myfield1
            myfield2
            myfield3
            myfield4
        
        For each item in strFieldNames
            Me.[item].Visible = Not IsNull(Me.[item])
    
    End Sub
    I'm just not sure what code I need. If someone could help me out, I'd be very greatful.

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'd store them in a table for ease of maintenance. You can open a recordset on that table and loop it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If you wanted to keep it in code, you could have a string like:

    "myfield1,myfield2,myfield3"

    and use the Split() function on it. It's more user-maintainable to have a table though.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In a report, it would not be a FIELD, but a CONTROL.

    I have not had to make complicated reports, so I have limited experience with reports.

    You might try having a table with 1 field (ControlName).
    If you are doing this for multiple reports, you could have 2 fields in the table (ControlName, ReportName).
    Open a record set, then loop though the record set setting the visible property of the control label in the report if the control value is empty/Null.


    Edit:
    Drat! Late to the party again.........

  5. #5
    kiwichick is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2020
    Posts
    31
    Thanks for all the suggestions about using a table instead. I don't know how to do that but will check it out

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Or use the tag property of the controls to mark each one you want to include then in a loop through all controls in the detail/ report check the tag value and process accordingly.
    Cheers,

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Macro Loop values in Form ang get values to query
    By mauryc1990 in forum Programming
    Replies: 13
    Last Post: 12-22-2017, 08:30 AM
  2. Loop through list of true values
    By micahbecca in forum Programming
    Replies: 3
    Last Post: 05-19-2017, 10:32 PM
  3. Replies: 4
    Last Post: 05-05-2017, 02:50 AM
  4. Replies: 4
    Last Post: 02-05-2014, 12:17 PM
  5. Replies: 5
    Last Post: 08-16-2012, 11:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums