Consulting

Results 1 to 12 of 12

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

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

    Hello, all. Completely new to vba here and coding in general. What i learned is from forums and youtube videos.

    I' making an access database for a friend of mine, to store informations about her patients. I have a form (ViewPatient) to view single patient information, meaning general informations, pill informations and exam informations. As there are people who take only 1 pill and others who take 15, i want to make the empty pill fields not visible on load.
    I made it happen with simple IF and visibility true or false on load, but since there are to many fields (pill1...pill20 / dose1...dose20 / epi1...epi20 / note1 ...note20 / plus the labels for all that), i wonder if there is a way to make it work with a loop, to not fill that coding page. And to learn something new basically.


    This is the code that works:
    If pill20.Value <> "" Then
       Label_pill20.Visible = True
       Pill20.Visible = True 
       Label_dose20.Visible = True
       dose20.Visible = True
       Label_epi20.Visible = True
       epi20.Visible = True
       Label_note20.Visible = True
       note20.Visible = True 
       Else
       Label_Pill20.Visible = False
       Pill20.Visible = False
       Label_dose20.Visible = False
       dose20.Visible = False
       Label_epi20.Visible = False
       epi20.Visible = False 
       Label_note20.Visible = False
       note20.Visible = False
    End If
    And this is what i come up for a loop, that doesn't work ( i get an error it cant find the field '"Pill" & i' ):
    Dim i As Long
    i = 2
    Do While i < 21
    If Forms![ViewPatient].Form!["Pill" & i].Value <> "" Then
       Forms![ViewPatient].Form!["Label_Pill" & i].Visible = True
       Else
       Forms![ViewPatient].Form!["Label_Pill" & i].Visible = False
    End If
    i = i + 1
    Loop
    If what i m thinking can be done, i m pretty sure i lost it with the [ ] and the " " and the ' 's. Those things are confusing as f...

    Thanks in advance.
    Last edited by Aussiebear; 12-08-2022 at 08:09 PM. Reason: Added code tags to supplied code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •