Results 1 to 13 of 13
  1. #1
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82

    Dynamically hide or show controls on a form based on user selections

    I have a table that has 130 fields (attributes to each specific item) the user wants to be able to select the fields that they need to see at any specific time. to enable this I have a form linked to a table that contains all the attributes and control names (not visible to user) and a y/n field. I have a viewer form that has all the controls on it (displayed as a grid view). when the user opens the viewer form, I want to loop through the list of items they want to see and the list of controls and only show controls they want to see.

    trying to do something like this on the form load event (assume all controls start as not visible and rst is a recordset of the control names they do want to see)

    dim ctl as control

    for each ctl in me.controls



    rst.movefirst
    do until rst.eof
    if clt.name = rst.name then ctl.visable = true


    rst.movenext
    loop


    I cant get to the .visible property for the controls

    any help is GREATLY appreciated

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Is Visible spelled correctly in the actual code (because it isn't here)?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82
    Quote Originally Posted by pbaldy View Post
    Is Visible spelled correctly in the actual code (because it isn't here)?
    yes have to post here from a different computer than my work computer.

    when I type ctl. visible is not an option in the popup list???

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That is sometimes the case. Have you tested the code to see if it works?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    By the way, it doesn't sound like your table is normalized. Typically there would be a related table with a record for each attribute.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82
    don't disagree but this is a export from a larger system and the data comes in as a single large table.

    I have it working if I open the form in form view (control is hidden) but it doesn't hide the field if it is opened in datasheet view. because of the amount of data, I need it in datasheet view. thoughts?

  7. #7
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82
    never mind .columnhidden got it working Thanks!

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Glad you got it sorted. I don't use datasheet much, would probably have suggested continuous.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82
    have it working BUT if the first control is not selected as one to view, i get an error (cant hide control that has focus) when it tries to hide it. have set all controls to TabStop = no how do I check if the control has focus and if it does move focus to the next displayed control

    just want to automate just like clicking TAB when the form is displayed (goes to the next displayed control in the Tab Index

    I tried just adding a default bogus control but that shows up in the DataSheet view which I don't want

  10. #10
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    Have you tried building the Record Source for the Form(datasheet) in the OnOpen event? Syntax might not be right but general idea.


    dim ctl as control

    vRS = "Select "

    for each ctl in me.controls

    rst.movefirst
    do until rst.eof
    if clt.name = rst.name then
    vRS = vRS & ctl.Name & ", "
    rst.movenext
    loop

    vRS = Right(VRS, len(vRS-1)) & " From tblYourTable" '(first part gets rid of last comma)

    me.RecordSource = vRS
    me.Requery

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I like Bulzie's idea. If you stick with hiding, you can add an error trap for that specific error and have it skip that field/control.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82
    Quote Originally Posted by Bulzie View Post
    Have you tried building the Record Source for the Form(datasheet) in the OnOpen event? Syntax might not be right but general idea.


    dim ctl as control

    vRS = "Select "

    for each ctl in me.controls

    rst.movefirst
    do until rst.eof
    if clt.name = rst.name then
    vRS = vRS & ctl.Name & ", "
    rst.movenext
    loop

    vRS = Right(VRS, len(vRS-1)) & " From tblYourTable" '(first part gets rid of last comma)

    me.RecordSource = vRS
    me.Requery

    that is basically what I am doing (almost exactly) however when the form (it is a subform) opens it places focus on the first control. so if the user does not want to see that field in their view, it errors. I need to build an error handler to move the focus to a different control (that is visible) then continue hiding the control they don't want to see.

  13. #13
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    Do you have any button on the form header or footer for adding new records or Save etc. You can move the focus to one of those control buttons.

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

Similar Threads

  1. Hide Controls based on query
    By shank in forum Reports
    Replies: 5
    Last Post: 06-05-2019, 03:18 PM
  2. Hide/Show fields at Form based on checkbox
    By cap.zadi in forum Forms
    Replies: 8
    Last Post: 04-22-2016, 05:08 AM
  3. Replies: 1
    Last Post: 10-05-2011, 04:36 PM
  4. Combobox-dependent show/hide controls
    By focosi in forum Forms
    Replies: 3
    Last Post: 07-31-2011, 06:47 AM
  5. Replies: 6
    Last Post: 02-24-2011, 03:24 PM

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