Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Visibility

This is a discussion on Visibility within the Microsoft Access forums, part of the Question Forums category; Hi, Can anyone tell me how to set the visibility of a field in a form to either true or ...

  1. #1
    Board Regular
    Join Date
    Mar 2002
    Posts
    103

    Default Visibility

    Hi,

    Can anyone tell me how to set the visibility of a field in a form to either true or false using a macro. ie can anyone give me the code?

    thanks very much,

    Will

  2. #2
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    25,957

    Default Re: Visibility

    To show:
    FieldName.Visible = True

    To hide:
    FieldName.Visible = False

    You probably want to put this code in the "On Load", "On Open, or " On Current" event of the form.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  3. #3
    Board Regular
    Join Date
    Mar 2002
    Posts
    103

    Default Re: Visibility

    Hi, thanks very much for your reply. I had tried that though and it just wouldnt work. Maybe I was just putting it in the wrong place...i'll try again!

    Thanks again.

    Will

  4. #4
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    25,957

    Default Re: Visibility

    Yes, you need to make sure that the code is getting triggered and run in all instances that you want it to. This is usually done by putting the code in one of the events. If you view the Properties of your form, go to the Events tab to see all the possibilities, and click on the Code Builder of a particular event to enter your code.

    If you need help, please explain the circumstances in which you want the code to run. Like I said, the most common events I use are "On Open", "On Load", and "On Current".
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  5. #5
    Board Regular
    Join Date
    Mar 2002
    Posts
    103

    Default Re: Visibility

    Ok, thanks again for your reply. I have a 'Main' form with a load of SubForms on it. When the SubForms are being viewed in the 'Main' form, I want only certain fields to be shown. When the SubForms are being viewed as a form in their own right (if you get what i mean?), I want all the fields in the form to be visible.

    Is this possible? because if i use the '******' or 'OnOpen' events, the fields will hide when the form is loaded in the 'Main' form.

    Or can i use the 'OnOpen' event of the 'Main' form to hide the fields of the subforms??

    I hope this makes sense!

    Thanks again,

    Will

  6. #6
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    64,939

    Default

    In the open event for the subform test to see if the main from is open.

    If it is hide the controls you want to.
    If posting code please use code tags.

  7. #7
    Board Regular
    Join Date
    Mar 2002
    Posts
    103

    Default Re: Visibility

    When the 'Main' form opens, can i set the field visibility property of a subform to false and then set it to true again when the subform is opened on its own?

    What would the code be? Form!FieldName.Visibility?? cant seem to get it to work.

    Thanks,

    Will

  8. #8
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    25,957

    Default Re: Visibility

    Hmmm...

    I would be willing to bet there there is way that you can get them to act differently, depending on whether or not the Form is attached to another as a Subform, or is independent, but I am not sure how to do that.

    What I would probably do is make a second copy of the form/subform with a different name and set the fields to show in one, but not in the other.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  9. #9
    Board Regular
    Join Date
    Mar 2002
    Posts
    103

    Default Re: Visibility

    Good idea. I'll do that, but if anyone else knows how to do it using one form could they let me know tho please!

    Thanks for that!

    Will

  10. #10
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling
    Posts
    64,939

    Default

    Use something like this on the subform:

    Code:
    Private Sub Form_Open(Cancel As Integer)
    Dim frm As Form
        
    For Each frm In Forms
        If frm.Name = "Main Form" Then
    
    ' put code here to hide/show controls.
    
            MsgBox "Open"
        End If
    Next frm
    
    End Sub

    You could also create a function like this:

    Code:
    Function IsFormOpen(frmName As String) As Boolean
    Dim frm As Form
        
    IsFormOpen = False
    
    For Each frm In Forms
        If frm.Name = "Main Form" Then
            IsFormOpen = True
        End If
    Next frm
    
    End Function
    If posting code please use code tags.

Page 1 of 2 12 LastLast

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com