Consulting

Results 1 to 12 of 12

Thread: Use Loop to modify the visibility of multiple fields in a form

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    Location
    [] are only needed if names have space or punctuation/special characters and sometimes if it is a reserved word.

    Syntax is not correct.

    If labels are associated with data control (textbox, combobox, etc) they will not be visible when data control is not visible.

    Why are you using a number iterating loop? All controls have the same number. What needs to change is the text prefix. If you want a number iteration then controls would need a generic name like: tbxData1, tbxData2, etc.

    Most likely controls that are blank are Null, not empty string. I never allow empty string in fields.

    Since visibility is dependent on value of Pill20 and I see only 3 other controls:

    For i = 1 to 3
    Me("tbxData" & i).Visible = Not IsNull(Me.Pill20)
    Next

    If you actually have 20 sets of these 4 fields/controls, then your db structure is bad.

    If you want to keep the names as they are, alternative is to use control's Tag property. Set it to some value like "Grp1". Then code would loop through ALL controls on form and look at the Tag value and only set visibility for those that are "Grp1".
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  2. #2
    Thank you for your quick and thorough reply, June.

    Quote Originally Posted by June
    Why (so many mistakes)
    Because i dont know shit! I dont have any education on the subject, its the second database i make in my life, and its something i dont need in my job, so i dont really have to learn it. Its something i do on my own, to pass my time and learn as i go.
    I m sure if anyone with a minor knowledge look at my database will see errors everywhere, but i m proud of it, thinking of the times something didnt work and after searching for 2 hours i found the solution.

    Quote Originally Posted by June7 View Post
    [] are only needed if names have space or punctuation/special characters and sometimes if it is a reserved word.

    If labels are associated with data control (textbox, combobox, etc) they will not be visible when data control is not visible
    Thanks for the lesson. I didnt know any of that and now i do.

    Quote Originally Posted by June
    Why are you using a number iterating loop?
    I though i cant have it 20 times longer that it has to, searched "access vba loop" on my friend google and come across "do while loop"...

    Quote Originally Posted by June
    For i = 1 to 3
    Me("tbxData" & i).Visible = Not IsNull(Me.Pill20)
    Next
    Thank you, thank you, thank you.

    Quote Originally Posted by June
    If you actually have 20 sets of these 4 fields/controls, then your db structure is bad.
    I hope you dont have to see my whole db... Its all bad!

    Quote Originally Posted by June
    If you want to keep the names as they are, alternative is to use control's Tag property. Set it to some value like "Grp1". Then code would loop through ALL controls on form and look at the Tag value and only set visibility for those that are "Grp1".
    Not something to add now, as i want its record searched independently, but something to search and learn.


    *Edit*
    This is my code that works now:
    Private Sub Form_Load()
    
    For i = 2 To 20
    If Me("Pill" & i).Value <> "" Then
    Me("Pill" & i).Visible = True
    Me("dose" & i).Visible = True
    Me("epi" & i).Visible = True
    Me("note" & i).Visible = True
        Else
    Me("Pill" & i).Visible = False
    Me("dose" & i).Visible = False
    Me("epi" & i).Visible = False
    Me("note" & i).Visible = False
    End If
    Next
    End Sub
    Last edited by jim_koug; 12-09-2022 at 03:44 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
  •