全局点击事件处理程序(WithEvents)

时间:2010-08-06 14:10:19

标签: ms-access vba vb6 access-vba

我正在尝试创建一个类模块,它将充当全局处理程序,以便当有人点击我在表单中的六十个文本框中的一个时。文本框表示本周的时间卡,显示信息为时钟输入,时钟输出,午餐开始,结束,持续时间,每周七天的每日总时数。当有人在一天内点击任何一个方框时,所有方框都将解锁并启用,以便用户可以编辑其中的信息。

在搜索全局点击事件的解决方案之后,我发现我可以创建一个处理事件的类模块,而不会为调用单独函数来处理事件的每个文本框创建一个click事件。我遇到的问题是我的类模块似乎没有处理我的事件,并想知道是否有人可以建议解决我的问题。仅供参考,我的所有文本框以及锁定和禁用以防止数据损坏。以下是我的代码:

''# Class module    
    Option Compare Database
    Option Explicit

    Public WithEvents TC_txtbox As TextBox
    ''# Set the textbox so that its events will be handled
    Public Property Set TextBox(ByVal m_tcTxtBox As TextBox)
        TC_txtbox = m_tcTxtBox
    End Property


    ''# Handle and onClick event of the
    Private Sub TC_txtbox_Click()
        ''# Find out the controls that where clikck
        Debug.Print Form_TimeCard.ActiveControl.Name
        Dim ctl As Control
        For Each ctl In access.Forms.Controls
            Debug.Print ctl.Name
        Next ctl
    End Sub

表格代码

Option Compare Database
Option Explicit
''# Global Variables
Public clk_inout As Boolean
Public settings
Public weekDict
Public weekOf As Variant
Public curDay As Variant
Public txtBxCollection As Collection
''# Event Handler for when the form opens
Private Sub Form_Open(Cancel As Integer)
    ''# Configure varaibles
    Me.TimerInterval = 60000 ''# 10 sec Interval
    weekOf = getFirstDayofWeek(Date)
    curDay = Date
    Set weekDict = CreateObject("Scripting.Dictionary")
    Set settings = CreateObject("Scripting.Dictionary")
    Set txtBxCollection = New Collection

    ''# Load Time Card Data
    Call initSettings
    ''# Debug.Print "Work Day Goal " & settings.Item("Work_day_goal_hrs")
    Call initDict
    Call initTextBoxEventHandler
    Debug.Print "Collection count " & txtBxCollection.Count
    Call loadDates(Date)
    Call clearDay
    Call selectDay(Date)
    Call loadWeeksData(weekOf)

    Dim ctl As Control
    Set ctl = weekDict.Item(Weekday(curDay)).Item("In")

    If IsDate(ctl.Value) And (Not ctl.Value = "") Then
        Me.but_clk_inout.Caption = "Clock Out"
        Me.but_lunch.Visible = True
        clk_inout = False
    Else
        Me.but_clk_inout.Caption = "Clock In"
        Me.but_lunch.Visible = False
        clk_inout = True
    End If
    ''# Debug.Print "Work Day Goal " & settings.Item("Salary")
End Sub

Public Sub initTextBoxEventHandler()
    Dim eventHandler As TextBoxEventHandler
    Set eventHandler = New TextBoxEventHandler
    Debug.Print "Collection count " & txtBxCollection.Count
    Set eventHandler.TextBox = Me.txt_F_in
    txtBxCollection.Add eventHandler

    Debug.Print "Collection count " & txtBxCollection.Count
End Sub

2 个答案:

答案 0 :(得分:1)

你错过了Set吗?公共财产集应

Public Property Set TextBox(ByVal m_tcTxtBox As TextBox)  
  Set TC_txtbox = m_tcTxtBox  ' dont forget the Set! '
End Property 

答案 1 :(得分:1)

我弄清楚我在设置文本框的类模块中的问题我忘了添加"TC_txtbox.OnClick = "[Event Procedure]""如果未在声明中声明[事件过程],VBA将不会在扩展文本框中触发自定义偶数处理程序您想要处理的事件的属性