Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2015
    Posts
    1

    Unanswered: Make command button visible based on value of control on another form

    Form 1: I have a combo box on my main menu(MenuMainF) called cboCompanyType with typical values 1,2,3,4,5,etc......

    Form 2: I have a command button on my administration menu(MenuAdminF) called cmdDevelopment

    My goal is to have the cmdDevelopment button of Form 2 be visible if cboCompanyType on Form 1 is equal to 1,2,3, or 4, but if it is any other value, then cmdDevelopment should be not visible.

    Any suggestions?

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    As answered elsewhere:

    This type of thing, testing for less than 5 and adjusting the form references:

    http://www.baldyweb.com/ConditionalVisibility.htm
    Paul

  3. #3
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    One solution consists in using a class WithEvent (Cls_Messenger) that will be shared by both forms. This class raises an Event Broadcast(Topic, Message) when you call its Send() method. This Event is received by all Forms (or classes) sharing an instance of the Messenger class.

    As both forms must share the same instance of the class Cls_Messenger, we use a standard module that will create the instance of the class when the CreateMessenger() function is called, store the reference to the created instance of the class into a private Collection object and return a reference to the instance of the class stored in the Collection when the GetMessenger() function is called.
    1. Here is the code for the Standard Module (Mod_Messenger):
    Code:
    Private m_colMessenger As Collection
    
    Public Function CreateMessenger() As String
    
        Dim clsMessenger As Cls_Messenger
    
        If m_colMessenger Is Nothing Then Set m_colMessenger = New Collection
        Set clsMessenger = New Cls_Messenger
        m_colMessenger.Add clsMessenger, clsMessenger.Identity
        CreateMessenger = clsMessenger.Identity
    
    End Function
    
    Public Function GetMessenger(ByVal MessengerID As String) As Cls_Messenger
    
        Dim clsMessenger As Cls_Messenger
    
        If m_colMessenger Is Nothing Then Exit Function
        If m_colMessenger.Count = 1 And MessengerID = 0 Then
            Set GetMessenger = m_colMessenger.Item(1)
        Else
            For Each clsMessenger In m_colMessenger
                If clsMessenger.Identity = MessengerID Then Exit For
            Next
            Set GetMessenger = clsMessenger
        End If
    
    End Function
    
    Public Sub KillMessenger(ByVal MessengerID As String)
    
        Dim i As Long
    
        For i = 1 To m_colMessenger.Count
            If m_colMessenger(i).Identity = MessengerID Then
                m_colMessenger.Remove i
                Exit For
            End If
        Next i
    
    End Sub
    2. Here is the code for the Class Cls_Messenger:
    Code:
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Public Event Broadcast(ByVal Topic As String, ByVal Message As Variant)
    Private m_strIdentity As String
    
    Private Sub Class_Initialize()
    
        m_strIdentity = Format(GetTickCount, "00000000") & Format(Int((99 - 1) * Rnd), "00") & Format(Int((99 - 1) * Rnd), "00")
        Randomize GetTickCount
        m_strIdentity = m_strIdentity & Format(Int((99 - 1) * Rnd), "00") & Format(Int((99 - 1) * Rnd), "00")
    
    End Sub
    
    Private Sub Class_Terminate()
    
        m_strIdentity = vbNullString
        
    End Sub
    
    Public Function FormatMessage(Optional ByVal SubTopic As Variant, _
                                  Optional ByVal Message As Variant, _
                                  Optional ByVal Sender As Variant, _
                                  Optional ByVal Extra As Variant) As Variant
    
        Dim var As Variant
    
        ReDim var(0 To 3)
        If IsMissing(SubTopic) Then SubTopic = Null
        If IsMissing(Message) Then Message = ""
        If IsMissing(Sender) Then Sender = ""
        If IsMissing(Extra) Then Extra = Null
        var(0) = SubTopic
        var(1) = Message
        var(2) = Sender
        var(3) = Extra
        FormatMessage = var
    
    End Function
    
    Public Property Get Identity() As String
    
        Identity = m_strIdentity
    
    End Property
    
    Public Sub Send(ByVal Topic As String, ParamArray Extra() As Variant)
    '
    ' The Send method raises the Broadcast(Topic, Message) event.
    '
        Dim varMessage() As Variant
        Dim varExtra As Variant
        Dim lngRow As Long
        Dim lngCnt As Long
        Dim i As Long
        Dim X As Long
    
        If Len(Topic) > 0 Then
            If IsObject(Extra(0)) Then
                Erase varMessage
                Set varExtra = Extra(0)
                RaiseEvent Broadcast(Topic, varExtra)
            Else
                If IsArray(Extra(0)) Then
                    varMessage = Extra(0)
                Else
                    ReDim varMessage(0 To 3)
                    For i = 0 To UBound(Extra)
                        lngCnt = lngCnt + 1
                        X = InStr(Nz(Extra(i), ""), ":")
                        If X > 0 Then
                            Select Case Left(Extra(i), X)
                                Case "Subtopic:", "T:": lngRow = 1
                                Case "Message:", "M:":  lngRow = 2
                                Case "Sender:", "S:":   lngRow = 3
                            End Select
                            If lngRow > 0 Then
                                varMessage(lngRow - 1) = Mid(Extra(i), X + 1)
                                Extra(i) = Null
                                lngCnt = lngCnt - 1
                            End If
                        End If
                    Next i
                    If IsNumeric(varMessage(0)) Then varMessage(0) = Val(varMessage(0))
                    Select Case lngCnt
                        Case 0
                            varMessage(3) = Null
                        Case 1
                            For i = 0 To UBound(Extra)
                                If Not IsNull(Extra(i)) Then
                                    varMessage(3) = Extra(i)
                                    Exit For
                                End If
                            Next i
                        Case Else
                            For i = 0 To UBound(Extra)
                                If Not IsNull(Extra(i)) Then
                                    If IsArray(varExtra) Then
                                        ReDim Preserve varExtra(0 To UBound(varExtra) + 1)
                                    Else
                                        ReDim varExtra(0)
                                    End If
                                    varExtra(UBound(varExtra)) = Extra(i)
                                End If
                            Next i
                            If IsArray(varExtra) Then varMessage(3) = varExtra
                    End Select
                End If
                RaiseEvent Broadcast(Topic, varMessage)
            End If
      End If
    
    End Sub
    3. When each form opens (Form_Open() procedure), it call the function GetMessenger(0) to try to obtain a reference to an existing instance of the Class Cls_Messenger. If such an instance does not exists (i.e. the Collection is empty), GetMessenger(0) returns Nothing. In that case, the form uses GetMessenger(CreateMessenger) to create the instance of Cls_Messenger.

    4. In the form MenuMainF, the event AfterUpdate of the Combo cboCompanyType calls the Send method of the common instance of Cls_Messenger (though its own reference to it), sending the name of the calling object (cboCompanyType) and its value.
    Here is the code used in the Class Module of the form MenuMainF:
    Code:
    Private WithEvents m_clsMessenger As Cls_Messenger
    
    Private Sub cboCompanyType_AfterUpdate()
    
        Dim var As Variant
        
        var = m_clsMessenger.FormatMessage(cboCompanyType.Value, "cboCompanyType")
        m_clsMessenger.Send "cboCompanyType", var
        
    End Sub
        
    Private Sub Form_Close()
    
        Set m_clsMessenger = Nothing
        
        ' m_strMessengerId <> vbNullString means that we created the instance of Cls_Messenger.
        ' We are then responsible for destroing it (otherwise it will remain in memory until the program ends).
        '
        If m_strMessengerId <> vbNullString Then KillMessenger m_strMessengerId
        
    End Sub
    
    Private Sub Form_Open(Cancel As Integer)
    
        ' Try to get a reference to an existing Cls_Messenger instance.
        '
        Set m_clsMessenger = GetMessenger(0)
        
        ' If no Cls_Messenger instance exists, create one.
        '
        If m_clsMessenger Is Nothing Then
            Set m_clsMessenger = GetMessenger(CreateMessenger)
            
            ' As we created the instance of Cls_Messenger,
            ' We are responsible for destroying it when closing (see Form_Close).
            '
            m_strMessengerId = m_clsMessenger.Identity
        End If
        
    End Sub
    5. The Class Module of the form MenuAdminF contains an Event Handler that receives the events sent by the Send method of the shared instance of the Class Cls_Messenger. When such an event is received, the code examines the Topic parameter which contains the name of the sender (i.e. the object that called the Send Method). If the name of the caller is "cboCompanyType", it parses the Message parameter (which represents the value of the combo -see above). If the range of Message is between 1 and 4, the Visible property of the Command Button cmdDevelopment is set to True, showing the button. If the value of Message is not within that range, the Visible property of the Command Button cmdDevelopment is set to False, hiding the button.
    Here is the code used in the Class Module of the form MenuAdminF:
    Code:
    Private WithEvents m_clsMessenger As Cls_Messenger
    Private m_strMessengerId As String
    
    Private Sub Form_Close()
    
        Set m_clsMessenger = Nothing
        If m_strMessengerId <> vbNullString Then KillMessenger m_strMessengerId
        
    End Sub
    
    Private Sub Form_Open(Cancel As Integer)
    
        Set m_clsMessenger = GetMessenger(0)
        If m_clsMessenger Is Nothing Then
            Set m_clsMessenger = GetMessenger(CreateMessenger)
            m_strMessengerId = m_clsMessenger.Identity
        End If
        
    End Sub
    
    Private Sub m_clsMessenger_BroadCast(ByVal Topic As String, ByVal Message As Variant)
    
        If Topic = "cboCompanyType" Then
            Select Case Message(0)
                Case 1 To 4:    Me.cmdDevelopment.Visible = True
                Case Else:      Me.Text_Dummy.SetFocus
                                Me.cmdDevelopment.Visible = False
            End Select
        End If
    
    End Sub
    Attached Files Attached Files
    Have a nice day!

Posting Permissions

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