Single Handler for the Click Event in Multiple Controls of a Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yoel Duady
    New Member
    • Mar 2016
    • 3

    Single Handler for the Click Event in Multiple Controls of a Form

    There are some combobox in my form .
    When the user clicks on any of them while the AllowEdits property is seting to false , I want to note him that it is'nt avilble to edit the data.
    I think should use the technique of class but as new in VBA cuold not find
    Sorry for my poor English and Thanks in advance
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32634

    #2
    I suspect you could define a class based on the ComboBox control.

    Beware, events only get passed on to the handler in the class if the object handled by the class already has a handler for that event.

    As an example, here's a fairly basic class module that I've used for handling hierarchical opening and closing of forms :
    Code:
    Option Compare Database
    Option Explicit
    
    '21/1/2004  Added Private Set & Public Get code for frmTo.
    '21/9/2004  Removed ResumeTo functionality. _
                Now handled by the OnTimer() subroutine in the calling form _
                checking for (Visible) which indicates the called form is finished.
    '24/2/2005  Added function Uninitialised to show if instance of this object _
                has yet been initialised with the callers info. _
                It also checks this before it tries to open a new form.
    '31/3/2008  Added varOpenArgs as optional parameter to ShowForm.  Simply to be _
                passed directly to the opened form using DoCmd.OpenForm(). _
                Also set .OpenForm() to treat Cancel of the open as NOT an error.
    
    Private Const conUnInitMsg As String = _
                      "Object uninitialised - unable to show form."
    
    Private frmParent As Form
    Private WithEvents frmCalled As Form
    
    Public Property Set frmFrom(frmValue As Form)
        Set frmParent = frmValue
    End Property
    
    Private Property Get frmFrom() As Form
        Set frmFrom = frmParent
    End Property
    
    Private Property Set frmTo(frmValue As Form)
        Set frmCalled = frmValue
    End Property
    
    Public Property Get frmTo() As Form
        Set frmTo = frmCalled
    End Property
    
    'Uninitialised returns True if frmFrom not yet initialised.
    Public Function Uninitialised() As Boolean
        Uninitialised = (frmParent Is Nothing)
    End Function
    
    'ShowForm opens form strTo and hides the calling form.  Returns True on success.
    Public Function ShowForm(strTo As String, _
                             Optional strFilter As String = "", _
                             Optional varOpenArgs As Variant = Null) As Boolean
        ShowForm = True
        'Don't even try if caller hasn't initialised Form object yet
        If Uninitialised() Then
            ShowForm = False
            Call ShowMsg(strMsg:=conUnInitMsg, strTitle:="classForm.ShowForm")
            Exit Function
        End If
        Call DoCmd.Restore
        'Handle error on OpenForm() only.
        On Error GoTo ErrorSF
        Call DoCmd.OpenForm(FormName:=strTo, _
                            WhereCondition:=strFilter, _
                            OpenArgs:=varOpenArgs)
        On Error GoTo 0
        Set frmTo = Forms(strTo)
        frmFrom.Visible = False
        Exit Function
    
    ErrorSF:
        ShowForm = False
        ' If open is cancelled (either by user or code) then simply exit
        If Err.Number <> 2501 Then _
            Call ErrorHandler(strName:=strTo, _
                              strFrom:=frmFrom.Name & ".ShowForm", _
                              lngErrNo:=Err.Number, _
                              strDesc:=Err.Description)
    End Function
    
    '************************* Contained Object Method(s) *************************
    'For these subroutines to be activated the contained object must have the
    ''On Close' property set to a valid subroutine within the contained object.
    Private Sub frmCalled_Close()
        frmFrom.Visible = True
        Call DoCmd.Restore
        Set frmTo = Nothing
    End Sub
    '******************************************************************************

    Comment

    • Yoel Duady
      New Member
      • Mar 2016
      • 3

      #3
      Thank you NeoPa ,for your quick response.
      At this point, I can not delve into the issue because of its important ingredients change.
      When I get to be able to return to the subject later - my response will be more specific.
      Thank You .

      Comment

      • calaxan
        New Member
        • Jan 2016
        • 12

        #4
        What i get from Yoel Duady question is to prevent the User modify/change value in the combobox. If it's so, why don't you use LimitToList property.
        Set LimitToList in the combobox properties to YES.

        Sorry if it's not giving you a solution.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32634

          #5
          That's not a correct understanding Calaxan. What they want is to update any and all controls on a form that have been changed, IE. The value is different from the saved data. Essentially, something that reflects dirtied controls. Ones that have been updated will have one background colour, while those that haven't will have another.

          @Yoel.
          That's fine. Come back when you're ready. We'll still be here :-)
          Last edited by NeoPa; Mar 17 '16, 03:23 AM.

          Comment

          Working...