otineb
12-08-2008, 08:03 AM
Deal all,
I have a whole bunch of formulars with a whole bunch of controls in each ...
For each control I wan to associate an action when doubleclicking (set the control value to NULL) and i don't want to do that with hand :-(
here is the code I want to run on each formular:
For Each ctlControl In Forms(rstForms("NomFrm")).Controls
Select Case ctlControl.ControlType
Case acCheckBox, acComboBox, acListBox, acOptionButton, acOptionGroup, acTextBox
ctlControl.OnDblClick = ' associate a code to reset the control
End Select
Nextafter that I should get in the form module, for each of my control:
private sub MYCONTROL_DblClick(Cancel As Integer)
MYCONTROL.value=NULL
End Sub
do you have an idea of how I should do that ?
Thanks !
lagbolt
12-08-2008, 10:17 AM
What you can do is change the value of the OnDblClick property of a control. Commonly, to run an event procedure, you assign the value '[Event Procedure]' to the 'OnSomeEvent' property, but you can also assign a reference to a public function using syntax like '=SomeFunction("SomeParameter")'. In this case you run the named funtion rather than the standard Control_DblClick() handler.
So if you have a reference to a control, say tbTesting, you can do this...
dim ctrl as control
dim functionCall as string
set ctrl = me.tbTesting
'create a string that will call a function, and include the control name
functionCall = "=SetNull(" & chr(34) & ctrl.name & chr(34) & ")"
'assign the function call string to the OnDblClick property of the control
ctrl.OnDblClick = functionCall
Then you write the 'SetNull' function, which takes a control name as a parameter...
Private Function SetNull(ControlName as string)
' Sets the value of the control to Null, and disables future OnDblClick handling
With Me.Controls(ControlName)
.Value = Null
.OnDblClick = ""
End With
End Function
So now when you Double Click the tbTesting control it's value will be set to Null, and the custom handler, SetNull() will be disabled.
You're not actually dynamically re-writing code, but you can programmatically determine what handler is run in response to any event, any time, and supply any (non-object) parameters you want, which should be enough flexibility to accomplish your goal.
otineb
12-08-2008, 01:28 PM
Thank you for your help, nice one !
after some few hours searching here and there I actually end up with this dirty portion of code :-(
but very learning full !
For Each ctlControl In frm.Controls
Select Case ctlControl.ControlType
Case acCheckBox, acComboBox, acListBox, acOptionGroup, acTextBox
If ctlControl.ControlType = acCheckBox Then
'Check if the checkbox is part of an option group!
If frm.Controls(ctlControl.Parent.Name).ControlType = acOptionGroup Then
GoTo loopFor
End If
End If
If InStr(1, ctlControl.Name, ".", vbTextCompare) <> 0 Then
strCtlName = Replace(ctlControl.Name, ".", "_", 1, , vbTextCompare)
strCtlName = "Ctl" + strCtlName
ElseIf IsNumeric(Left(ctlControl.Name, 1)) Then
strCtlName = "Ctl" + ctlControl.Name
Else
strCtlName = ctlControl.Name
End If
strProc = strCtlName + "_DblClick"
If Not mdl.Find(strProc, 1, 1, -1, -1) Then
lngReturn = mdl.CreateEventProc("DblClick", strCtlName)
mdl.InsertLines lngReturn + 1, vbTab & strCtlName & ".value=NULL" + vbCrLf
End If
If ctlControl.OnDblClick = "" Then ctlControl.OnDblClick = "[Event Procedure]"
Case Else
'Debug.Print "Not done : " + ctlControl.Name
End Select
loopFor: NextTo conclude :
1 - Very easy to manipulate code with the module object by using methods like Find, CreateEventProc ...
2 - Don't give funny names to your control as I did, like 1.2_1.4_txtbox12 or you are forced to follow Access rules to create your procedure (adding Ctl at the beginning of your control name, replacing all the "." by "_" ...)
3 - To manipulate controls easily (for instance make them visible on clicking on another control), prefer the property Tag.
I came across it and it seems to be nice for doing that !