Results 1 to 7 of 7
  1. #1
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151

    Using the visible setting in multiple areas of VBA code on a form

    Hi,

    I am sure there is a complex way to do this through programming which i am not strong at which is why i am doing this the long way but would like some advise if there is an easier way to do this for a non-programmer like me.

    I have many fields on my form which i make visible or not depending on conditions. But basically in the VBA on the form i call these fields many times and repeat the code. Here is just a small example:

    I have multiple command buttons for First, Next, Previous and last. For each of these command buttons i have this code repeated

    me.label1.visible = true
    me.label2.visible = true


    me.label3.visible = true
    etc....
    me.label4.visible = false

    everytime i need to change the code or add i have to this multiple times.

    I would like to have this code once and be able to call it for the command buttons that i want.

    Thanks

    Shmuli

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    One way is to use the Tag Property and loop over the forms controls.
    Something along the lines of:
    Code:
    Dim ctl as control
    
    For each ctl in Me.Controls
         If ctl.Tag = "XYX" then
             ctl.visible = true
         else
            ctl.visible = false
         end if
    next
    If you need to use the Tag property for several things you can do:
    Code:
    if Instr(1,ctl.Tag,"xyz") then
    A lot depends on your requirements which I'm not sure of from your question.

    I have multiple command buttons for First, Next, Previous and last.
    Is this for a custom navigation bar? And you use them on more than one form?
    There are many code examples out there.

    Here's a NavBar class that you can use just about anywhere with 3 lines of code.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Well, put the code to set control properties in a Sub and call that Sub instead of repeating code. The Sub can have an argument passed to it.

    Coding for custom navigation buttons is a common topic. Example from my db.
    Code:
    Public Sub ViewData(strDirection)
    'called by forms used to view and navigate records
    
    If Not IsNull(Form_SampleInfo.tbxViewDataFormNAME) Then
        DoCmd.Close acForm, Form_SampleInfo.tbxViewDataFormNAME, acSaveNo
    End If
    
    With Form_SampleInfo
    
    .RecordsetClone.Bookmark = .Bookmark
    Select Case strDirection
        Case "Quit"
            DoCmd.Close acForm, "SampleInfo", acSaveNo
        Case "Next"
            .RecordsetClone.MoveNext
            If Not .RecordsetClone.EOF Then
                DoCmd.GoToRecord acForm, "SampleInfo", acNext
            Else
                .RecordsetClone.MoveLast
                MsgBox "Last record."
                .btnNext.Enabled = False
            End If
            .btnPrevious.Enabled = True
        Case "Previous"
            .RecordsetClone.MovePrevious
            If Not .RecordsetClone.BOF Then
                DoCmd.GoToRecord acForm, "SampleInfo", acPrevious
            Else
                .RecordsetClone.MoveFirst
                MsgBox "First record."
                .btnPrevious.Enabled = False
            End If
            .btnNext.Enabled = True
    End Select
    
    End With
    
    End Sub
    Calling Sub:
    Code:
    Private Sub btnNext_Click()
    Me.btnViewLabData.SetFocus
    Call ViewData("Next")
    End Sub
    
    Private Sub btnPrevious_Click()
    Me.btnViewLabData.SetFocus
    Call ViewData("Previous")
    End Sub
    
    Private Sub btnQuit_Click()
    Call ViewData("Quit")
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    If multiple forms use the same pattern, then this:
    Insert a module.
    Write "Option Explicit" on top
    Add a subroutine that looks like this:
    Code:
    Public Sub HandleLabels(frm As Form)
      frm.label1.visible = true
      frm.label2.visible = true
      frm.label3.visible = true
      'etc....
      frm.label4.visible = false
    End Sub
    Then, in your form code, call this Sub like this:
    Code:
    HandleLabels Me
    If only one form does it, then in that form's code add:
    Code:
    Private Sub HandleLabels()
      Me.label1.visible = true
      Me.label2.visible = true
      Me.label3.visible = true
      'etc....
      Me.label4.visible = false
    End Sub
    And just call it from your command buttons like:
    Code:
    Call HandleLabels
    Or just
    Code:
    HandleLabels
    And there are a lot of other ways to modularize your code. You could create a class where you can initialize a button as a Next button and have the functionality of a Next button without even specifying what happens on click, or what happens on hover, etc., you'd just write:
    Code:
    Public nxt As New NextButton, nxt2 As New NextButton
    
    Private Sub Form_Open(Cancel As Integer)
        nxt.Initialize Me.someBtn
        nxt2.Initialize Me.someOtherBtn
    End Sub
    And all functionality defined in the class would be passed to the buttons.

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    I have an example app showing how I use the tag property for multiple controls: Set Controls (isladogs.co.uk)

    For example if A,B & X are tag values each set to several controls
    Code:
    ShowControls True, "A", "B" 'visible
    ShowControls False, "X" 'hidden
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    If you want to control how the forms look and act depending on the user please feel free to have a look at my free utility here:
    http://forestbyte.com/ms-access-util...access-levels/

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151

    Worked best for me

    Thank you everyone for your suggestions. this was the simplest for me and worked great


    Quote Originally Posted by Edgar View Post
    If multiple forms use the same pattern, then this:
    Insert a module.
    Write "Option Explicit" on top
    Add a subroutine that looks like this:
    Code:
    Public Sub HandleLabels(frm As Form)
      frm.label1.visible = true
      frm.label2.visible = true
      frm.label3.visible = true
      'etc....
      frm.label4.visible = false
    End Sub
    Then, in your form code, call this Sub like this:
    Code:
    HandleLabels Me
    If only one form does it, then in that form's code add:
    Code:
    Private Sub HandleLabels()
      Me.label1.visible = true
      Me.label2.visible = true
      Me.label3.visible = true
      'etc....
      Me.label4.visible = false
    End Sub
    And just call it from your command buttons like:
    Code:
    Call HandleLabels
    Or just
    Code:
    HandleLabels
    And there are a lot of other ways to modularize your code. You could create a class where you can initialize a button as a Next button and have the functionality of a Next button without even specifying what happens on click, or what happens on hover, etc., you'd just write:
    Code:
    Public nxt As New NextButton, nxt2 As New NextButton
    
    Private Sub Form_Open(Cancel As Integer)
        nxt.Initialize Me.someBtn
        nxt2.Initialize Me.someOtherBtn
    End Sub
    And all functionality defined in the class would be passed to the buttons.

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

Similar Threads

  1. Replies: 2
    Last Post: 03-09-2021, 05:52 PM
  2. Replies: 4
    Last Post: 01-24-2020, 02:15 PM
  3. Replies: 2
    Last Post: 04-30-2019, 03:44 AM
  4. Replies: 16
    Last Post: 03-26-2015, 08:35 PM
  5. Replies: 5
    Last Post: 09-06-2010, 11:50 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