Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WithEvents plus GotFocus not working 1

Status
Not open for further replies.

MartynM

Programmer
Nov 18, 2011
4
GB
thread702-1462022

The above thread shows how to subclass various events within Access.
This is working fine for me, except for GotFocus and SetFocus events, which do not appear to be trapped.

In the CommonControl class module I have
mTextBox.OnGotFocus = "[Event Procedure]"
mTextBox.OnLostFocus = "[Event Procedure]"

and

Private Sub mTextBox_OnGotFocus()
Call commonGotFocusProcedure(mTextBox)
End Sub

Private Sub mTextBox_OnLostFocus()
Call commonLostFocusProcedure(mTextBox)
End Sub

and in a standard module I have:

Public Function commonGotFocusProcedure(ctl As Access.Control)
MsgBox "GotFocus"
End Function

Public Function commonLostFocusProcedure(ctl As Access.Control)
MsgBox "LostFocus"
End Function

These latter functions never get called.

 
The property of a control is "OnGotFocus". This property tells what to run when the object gets focus: [Macro],[Event procedure], or embedded function

The event is "GotFocus" not "OnGotFocus". So

Private Sub mTextBox_GotFocus()
Call commonGotFocusProcedure(mTextBox)
End Sub

You can use intellisense to help you here. In the VBE you can pull down "mTextBox" from the combo and pick the correct event.
 
By the way, I'm using your code to do (potentially) several different actions for a control.
For instance, I might want to change a textbox backcolor on GotFocus, but also change its entry to all uppercase on LostFocus.

I put a single letter in the tag for each type of action - H for Highlight and U for UpperCase, say.

So if the tag says HU, commonGotFocusProcedure says
If ctl.Tag Like "*H*" Then ctl.BackColor=123

and commonLostFocusProcedure says
If ctl.Tag Like "*H*" Then ctl.BackColor=456
If ctl.Tag Like "*U*" Then ctl.Text=UCase(ctl.Text)

If the tag is needed for other things, then I can use brackets:
e.g. the tag would hold [HU]otherstuff
and the code would be Like "[*H*]" etc

Also, I've proceduralised the code which goes in the form, so that all that is needed in the Form_Load is:
SetupCommonControlActions (Me.Name)

and in a common code module I have:

Public ccControls As New CommonControls
Public Sub SetupCommonControlActions(ByVal FormName As String)
Dim ctl As Access.Control
On Error Resume Next

ccControls.Clear
For Each ctl In Forms(FormName).Controls
If ctl.ctlType = acTextBox Or ctl.ctlType = acLabel Or ctl.ctlType = acListBox Or ctl.ctlType = acComboBox Then
If ctl.Tag Like "*H*" Then
ccControls.Add ctl, ctl.Name
End If
End If
Next ctl
End Sub

 
I would also like to be able to cope with controls on a subform.
I know how to refer to them generally:
SubformControlName.Form.Controls(i)
but can you suggest how they could be incorporated in your scheme?
TIA
 
Use a Form object instead of a String:
Code:
Public Sub SetupCommonControlActions(objForm As Access.Form)
    Dim ctl As Access.Control
    On Error Resume Next
    ccControls.Clear
    For Each ctl In objForm.Controls
      If ctl.ctlType = acTextBox Or ctl.ctlType = acLabel Or ctl.ctlType = acListBox Or ctl.ctlType = acComboBox Then
        If ctl.Tag Like "*H*" Then
          ccControls.Add ctl, ctl.Name
        End If
      End If
    Next ctl
End Sub

And then in the Form_Load:
SetupCommonControlActions Me

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I'm using your code to do (potentially) several different actions for a control.

You may want to then add properties and functionality to the class. You could add properties like.

private mFocusBackColor as long
private mLostFocusBackcolor as long

then add get and let procedures so you can set/get these properties.

then in your common method you can simply do things like

ctl.backcolor = ctl.FocusBackColor

Then when you initialize the class you can do something like

dim ccCtrl as commonControl
.... your code to load the collection
set ccCtrl = ccControls.Add ctl, ctl.Name
with ccCtrl
.FocusBackColor = vbRed
.LostFocusBackColor = vbGreen
.yourotherproperties
end with

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top