Is there a way to determine what control I clicked using a global click event. I would like to be able to click a form text box table I created for my access app to enable to enable and unlock it without have a million on click event handlers

Hi Talguy,
I hope you can explain your second sentence. Most helpers will have trouble understanding that sentence.
Thanks

In a module add the following -

Public Sub UnlockTextBoxes(frm As Form)

'frm is the Form you are referring to...

Dim Control As Control

 For Each Control In frm.Controls
        If TypeOf Control Is TextBox Then
            If Control.Name = "MyTextBox" Then
               Control.Locked = False
            End If
        End If
       
    Next Control
End Sub

In your Form, call the sub -

Call UnlockTextBoxes (Me)

Is this what you wanted?

I wrote code like that to find a control by name but what I am trying to do is I have a weekly timecard where all the boxes deailing (Clock in, clock out, lunch start, lunch end, total hours worked) I would like to give th myself the ability to edit the time card incase I made a mistake that does not match up with my company's clockin machine. So their are seven days and under each day I have 6-8 textboxes. If I click on of those textboxes under a certain day it will unlock all the text boxes above and below it so that I can edit the information. I can do this by having a click event for each text box but that would be a whole lot of redundant code for 60+ boxes. How can I have one function that handles all the click events for the form and have it get the active control so that I can determine what day of the week needs to be unlocked for editing

Use an array of controls as in txtDay1(0) to say txtDay1(5) etc. With the above at the 'If Control.Name' you can use 'If Control.Index >0 And Control.Index <6' unlock the textboxes, assuming that you know which day of the week represents which Index number.

This will be about the quickest way of going through all 60 textboxes I think.

yea I did that with a dictionary. But how would I set it up so that when any of the text boxes are clicked they call the same event handler function

You can use something like -

Private Sub Text1_Click(Index As Integer)

'Lets say that Text1(0), (6), (12) has the same events -
Dim x As Integer

x = Index

If x = 0 Or x = 6 Or x = 11 Then
    Text1(x).Text = "yeah"
End If
End Sub

that looks to only work if I click a certain box under the day and then it would unlock the reset. I want it so that a user can click any of the boxes under a certain day and it would unlock all the boxes. But each text boxes when clicked calls the same onClick function like this

Private sub handler_Click() ' all textboxes call this automatically
    ' find out the active control name
end sub

but i don't want to do this

Private sub txt_1_Click()
 call handler_event(box_name)
end Sub

Private sub txt_2_Click()
 call handler_event(box_name)
end Sub

Private sub txt_3_Click()
 call handler_event(box_name)
end Sub

public sub handler_event(name as String)
    ' perform logic here
end sub

Sorry for only getting back now - the solution will be a for next loop as in -

Private Sub Text1_Click(Index As Integer)
 
'Lets say that Text1(0), (6), (12) has the same events -
Dim x As Integer
 
x = Index
 
For x = 0 To 10
If x = 0 Or x = 6 Or x = 9 Then
    Text1(x).Text = "yeah"
End If
Next x
End Sub

This changes all the textbox text to yeah in 1 go.

yea thats not too bad. I figured from searching the web that I can create a class module with the following statement at the top of the file.

WithEvents class_name as access.TextBox

and then provide my logic for handling the event

Question: Do I need to have my textbox enabled and/or unlocked for a click event to happen cause right now I have all the boxes disabled and locked?

Figured out my problem. I had to extend the textbox object with a class module. Inside the class module I had a function that handled onClick events that would search for every object that had the same tag as the active control. Below is my code for the extended class

Option Explicit

Private WithEvents TC_txtbox As TextBox
Private day As String
' Set the textbox so that its events will be handled
Public Property Set TextBox(ByVal m_tcTxtBox As access.TextBox)
    Set TC_txtbox = m_tcTxtBox
    TC_txtbox.OnClick = "[Event Procedure]"
    TC_txtbox.Enabled = True
End Property
' Handle and onClick event of the
Private Sub TC_txtbox_Click()
    ' Find out the controls that where clikck
    If Not IsNull(day) Then
        ' Highlight text inside text box
        Me.ActiveControl.SelStart = 0
        Me.ActiveControl.SelLength = Len(Me.ActiveControl.Text)
    Else
        ' Day has not been set enable text boxes
        day = Form_TimeCard.ActiveControl.Tag
        
        Dim ctl As Control
        For Each ctl In Form_TimeCard.Controls
            If day = ctl.Tag Then
                ctl.Enabled = True
                ctl.Locked = False
            End If
        Next ctl
        End If
End Sub
' Handle change event
Private Sub TC_txtbox_Change()
    Debug.Print "Shit Changed"
End Sub

Public Property Get Name() As String
    Name = TC_txtbox.Name
End Property

Nicely done Talguy, I like your "Debug.Print" statement which I presume came after there was no more hair left on your head to pull out. Hahaha..

Good luck in your future coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.