1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Access 2003 - Hiding Parts of Forms/Fields

Discussion in 'Business Applications' started by smooth, Jan 30, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. smooth

    smooth Thread Starter

    Joined:
    Sep 26, 2005
    Messages:
    4,029
    First Name:
    Garrett
    Hey everybody :D

    Okay, been playing around with our Access Database here.

    I was wondering if there is a way to hide certain elements on a form, when a button is clicked to view the form?

    For example, I have a form called newHireForm, that is shown when you click on a button on the switchboard. Now, I also have a form called recruitingContactForm, which has a lot of the same information that newHireForm has, except it has different dates, and a few different fields.

    Now, I figured I could just have one central form called newAgentsForm, for instance. Then on my switchboard have a button for seeing New Hire, and then a button on the switchboard that lets me see Recruits Contact. Is there a way to let certain fields be visible, while others are not based on what button I press on the switchboard?

    I hope I was pretty clear with explaining. It's hard to put in words exactly what I want to do. ;)

    Thanks :)
     
  2. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    Hey smooth.

    Every control on a form has a Visible property that you can adjust programmatically or via the control's Properties window. Here's a trick you can use:

    Every control also has a Tag property that you can use to create a virtual collection of diverse controls. The Tag property is a user-defined string. So, for example, you might add the tag "RecruitingContactControl" to all the controls for the recruiting side and "NewHireControl" to all the controls on the new hire side. Then, based on some user event, you could use a code model like so:

    Code:
    Dim ctl As Control
    
    For Each ctl In Me.Controls
      With ctl
        If .Tag = "RecruitingContactControl" Then
          .Visible = "True"
        Else
          .Visible = "False
        End If
      End With
    Next ctl
    
    You'll have to decide the best structure to handle the switch, but I'm sure you get the idea. This is probably the most elegant way to do it.

    [edit]
    The maximum length of the Tag string is (I think) 1024 characters. However, using something that large for this particular application is impractical.
    [/edit]

    Another really dirty way to do it is to ensure the layout is such that each set of controls is grouped, then add two rectangles with solid fill to overlay each group. The fill color would be the same as the form section backcolor. Send the rectangles to the front, then show/hide each rectangle based on user action. This is a bad solution if you use certain windowed controls, like subforms or combo/list boxes, because it is impossible to place anything over them.

    HTH, if you need more help just post back.

    chris.
     
  3. smooth

    smooth Thread Starter

    Joined:
    Sep 26, 2005
    Messages:
    4,029
    First Name:
    Garrett
    Awesome. Sounds like it might be exactly what I am looking for. I'll try it out tomorrow, and get back with you on what happens. :D

    Thanks chris. :)
     
  4. smooth

    smooth Thread Starter

    Joined:
    Sep 26, 2005
    Messages:
    4,029
    First Name:
    Garrett
    Hey everybody :D

    Okay, I tried out what you said chris, and I had to change a couple of things. I gave the elements that I wanted to always stay the tag "Both", the elements that I want just for newHire the tag "NewHire", and the elements that I want just for recruitingContact the tag "recruitingContact".

    Now, here is the code I have:

    Code:
    Private Sub newHire_Click()
    DoCmd.OpenForm ("newHireForm")
    Dim ctl As Control
    
    For Each ctl In Forms!newHireForm.Controls
      With ctl
        If .Tag = "NewHire" Or "Both" Then
          .Visible = "True"
        Else
          .Visible = "False"
        End If
      End With
    Next ctl
    End Sub
    I run it, and get the error: Type mismatch

    Any ideas on what's going on? Have I got the "OR" syntax wrong?
     
  5. smooth

    smooth Thread Starter

    Joined:
    Sep 26, 2005
    Messages:
    4,029
    First Name:
    Garrett
    Got it. :D

    I had to do:

    Code:
    Private Sub newHire_Click()
    DoCmd.OpenForm ("newHireForm")
    Dim ctl As Control
    
    For Each ctl In Forms!newHireForm.Controls
      With ctl
        If .Tag = "NewHire" Or .Tag = "Both" Then
          .Visible = "True"
        Else
          .Visible = "False"
        End If
      End With
    Next ctl
    End Sub
    :D Still working on it. Almost there (y)
     
  6. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    There's a technical name for the type of boolean logic you tried, i.e., A = (B | C), but I can't remember it. I'm pretty sure most languages don't support that anymore (if they ever did), but I might be completely wrong there. VBA for certain doesn't support that kind of logic. As you found out. You must use

    (A = B) | (A = C)

    (y)

    chris.
     
  7. smooth

    smooth Thread Starter

    Joined:
    Sep 26, 2005
    Messages:
    4,029
    First Name:
    Garrett
    (y) Still working with it. I have it where the elements disappear and appear as needed. That is awesome how that works out. :D Definitely saves creating new forms for everything that has similar data, but a few fields. :D

    I'll mark this one as solved, since I have solved the problem of hiding/showing elements. :) Thanks a lot chris.

    Now I am just finishing up with the various buttons I am going to implement. (y)
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/438515

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice