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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass function as a parameter?

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I'm trying to call function with a parameter and that parameter is a function with parmaters, so it can be bound to the onchange event of a form....

so I have a function header...
Code:
Public Function inputZoom(sTitle As String, Optional ByVal func As String)

I call it with the following
Code:
inputZoom("Documents Request Notes","setDocs([forms]![Check_Docs].Docs_Form.Form, ([forms]![Check_Docs].Notes)")

In the inputZoom function i have...
Code:
    If Not IsMissing(func) Then
        Forms("inputPopUp").inputTextBox.OnChange = func
    End If

Obviously this isn't working, but the logic is there , i just need the syntax.

how do you create a variable that is actually a dynamically created function and then execute the code?

Like you do with javascript
Code:
var f = new Function(“alert(‘Function called’);”);
 f();

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
It's OK, I worked it out thanks.

new function header
Code:
Public Function inputZoom(ByVal sTitle As String, Optional ByVal func As String, Optional ByVal arg1 As Object, Optional ByVal arg2 As Object)

call to function
Code:
inputZoom("Documents Request Notes","setDocs",[Forms]![Check_Docs].[Docs_Form].[Form],[Forms]![Check_Docs].[Notes])

code within function
Code:
    If Not IsMissing(func) And func <> "" Then
        Forms("inputPopUp").inputTextBox.OnChange = Application.Run(func, arg1, arg2)
    End If

Though it seems to be running the event even when the data hasn't changed!

Small teething problems I'm sure I'll iron out!







"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
Dang, it doens't work as it isn't assigning the function to the event handler, it's running the code at assignment!

hmmm, dynamic event handlers, anyone?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
Can you explain what you are trying to do? There is probably a better approach.
 
Hi Majp,

I have the 'zoom' popup function, which can be applied as an event handler 'dblclick' to any textbox form control, to 'zoom in', especially when some fields are varchar(max)/memo.

This is a generic feature I created has the ability that when the calling control is doubled clicked, the popup form opens and is passed the active control clicked on and binds it to the large textbox on the popup form.

I beleive you helped me with work on this popup at the time...

Anyway, I have a form which needs to auto complete a stamp on the record if it is changed, I already have an 'update' function which other tickbox controls call, I want to call this function if the popup 'zoom' box is changed.

Although it's a popup modal form with the underlying form control textbox bound to it, as it is a class in its own right (a form) it has it's own even handlers, so the underlyig notes box gets updated, but the update 'setDocs' function isn't called via any of the onchange/afterupdate/ondirty event handlers on the underlying form textbox control (there are also parameters to the update function).

I want to add to the popup 'zoom' form the event handlers from the underlying form, I want to do this in a dynamic way so altering the popup 'zoom' code doesn't make it specific to this one requirment, but enhances it's function and can be used elsewhere.

So I'm trying to create a way of calling the popup 'zoom' function with additonal parameters which are the function to call for the onchange event handler of the popup 'zoom' form textbox control and the parameters that function may need.

Does this help?





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
There are probably a few ways to do this.
1) You can set a global variable. So in the zoom function/form set
glblHasChanged = true
Since it is modal, when code execution returns to the calling form
do something like
if glblHasChanged then
do some code
glblHasChanged = false
end if

2) You could store the value, call the zoom, and check the value.

dim beforeValue as variant
beforeValue = me.somecontrol.value
'call zoom function. Code execution stops until modal closed
if not beforeValue = me.someControl.value then
do something
end if
 
I don't like it but I've gone down the global var route 'has changed' :-(

I did find a way of dynamically creating event handlers, only it doesn't work when the project is protected and running as ACCDE/ACCDR

Code:
 With Forms("inputPopUp").Module
           Line = .CreateEventProc("keypress", "inputTextBox")
           .InsertLines Line + 1, "Call " & func
        End With

It also doesn't work until the changes are saved to the forms's module, so 'on the fly' isn't really achieved!

I guess to make this work I would need to create the form on the fly also in memory referenced by a variable and then hook into that!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
option 1 doesn't seem to work, the code does not halt as you suggest when the popup form is opened!

I have to change the form opening to acdialog, but then the form close code doesn't run because it can no longer communicate with other forms that are open!

back to the drawing board!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
I've had to put my own code halt in to make it all work
Code:
    Call inputZoom("Documents Request Notes")
    
    Do Until Not IsLoaded("inputPopUp")
        DoEvents
    Loop

    If bPopZoomChange Then
        Call setDocs(Me.Docs_Form.Form, Me.Notes)
    End If

Not ideal but serves my purpose!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
it can no longer communicate with other forms that are open
???
 
my assumption was that the pop up opens as dialog. If that is the case then code execution in the calling form most certainly stops until the popup dialog form closes. If the pop up is not dialog then a better way is to trap the popup's events. Again you can only do that if the form is not open dialog.

in the calling form.

Code:
public withevents frm as Access.form
public withevents ctrl as access.textbox

public sub someSub
  'some code to pop up the form not dialog
  set frm = forms("the_pop_up_form_name")
  set ctrl = frm.sometextboxname
end sub

'now you can trap the pop up events from the form that called it

public sub frm_someFormEvent()

end sub

Public sub ctrl_SomeControlEvent()

end sub
 
still can't get it to work?

Code:
Private Sub Notes_DblClick(Cancel As Integer)

    Call inputZoom("Documents Request Notes")
    
    Set frm = Forms("inputPopUp")
    Set ctrl = frm.inputTextBox

End Sub
Public Sub ctrl_Dirty(Cancel As Integer)

    MsgBox "control dirty"
    Call setDocs(Me.Docs_Form.Form, Me.Notes)
    
End Sub

inputzoom opens the popup form, and binds the active control from the calling form.

The firstime the form opens it runs without error , although the event handler is not executed!

if I close the popup form and double click in the control again, this time it errors on
Code:
    Set ctrl = frm.inputTextBox

with
RT '2467' app-defined or object-defined error

and the hover error states it can't find the referenced form, yet it is open?



PHV -> ????

My form_close event errors when the form is opened as dialog
Code:
Private Sub Form_Close()

    oControl.SetFocus
    If Not IsNull(oControl) And Not IsNull(Me.inputTextBox) Then
        oControl.SelStart = Len(Me.inputTextBox)
    End If
    
End Sub





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
This is totally messed up.

The control event only works if you create a dummy blank event on the actual form.

But now everytime the event is executed, the content in the popup form (the bound text box) gets wiped, so each time I type a character the onchange event fires and all text in the input box is deleted?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
I've also tried...
Code:
  With zoomCtl
    .ControlSource = BoundControl.ControlSource
    .Locked = BoundControl.Locked
    .ValidationRule = BoundControl.ValidationRule
    .ValidationText = BoundControl.ValidationText
    .Format = BoundControl.Format
    .ControlTipText = BoundControl.ControlTipText
    .ForeColor = BoundControl.ForeColor
    .TextAlign = 1
    [b].OnChange = BoundControl.OnChange[/b]
  End With

  MsgBox BoundControl.OnChange
So in theory I attach to the zoom textbox control, the OnChange event from the underlying form.

The message box even shows [Event Procedure], but when you type into the zoom text box, the event isn't executed!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
Not sure I am following everything here but it seems like a lot of effort is being put into an ON Change event on the 'zoom' form instead of just putting a button on it that the user has to click when data entry is done...
 
button to click? how horrid. Avoid all extra clicks where ever possible, that's my motto, having carpol tunnel, it makes for a nicer user experience!

Anyway, it wouldn't change anything, i'd still want the onchange event to fire for the underlying textbox when the button was pressed.

what's the difference?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
I finally cracked it!

As I have the control object, on the popup form i create a change event as follows...

Code:
Private Sub inputTextBox_Change()
    
    CallByName Forms(oControl.Parent.Name), oControl.Name & "_Change", VbMethod
   
End Sub

Wasn't so hard after all, well easy when you know how!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Electronic Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top