Solved Function error (1 Viewer)

smig

Registered User.
Local time
Today, 16:44
Joined
Nov 25, 2009
Messages
2,209
Hi

I'm using this code, but it will fail on Me

Me.ProductPercent1.OnDblClick = "=fnCalcPercentFromQty_Click(Me, 'ProductPercent1')"

This how the function looks like, so Me as Form should not be a problem
Public Function fnCalcPercentFromQty_Click(frm As Form, strControlName As String)

It also works if I use it as normal function call
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 06:44
Joined
Aug 30, 2003
Messages
36,126
Me would be invalid in a form property sheet, which is what this appears to be creating. I might pass the name as a string and use that.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:44
Joined
Oct 29, 2018
Messages
21,474
Hi. Try it this way instead.
Code:
Me.ProductPercent1.OnDblClick = "=fnCalcPercentFromQty_Click([Form], 'ProductPercent1')"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:44
Joined
May 7, 2009
Messages
19,245
NO, that won't work.
Access is treating your Function as a Normal Control's Click Event.
and normal control Click event does not have parameter, therefore the error.
highly advise to Change the Name of the function by dropping the "_Click()" part.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:44
Joined
Feb 28, 2001
Messages
27,191
Arnel, to treat the call as a normal _Click event, wouldn't there have to be some control with the the name fnCalcPercentFromQty? Wouldn't you need that in order to bind the name (with _Click) to an actual event?

I can name a subroutine Freddy_Click() but unless I have a control named Freddy, there is no event slot in which to make that linkage between Access event and an event handler routine. Any call using that long name with the _Click suffix is just another subroutine name that would have to be declared by a SUB statement, in which case it could still have parameters.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:44
Joined
May 7, 2009
Messages
19,245
you are right, unless he has the same control name.

but, you are also an advocate of "naming" convention, reserved words.
is an event name not in your list?
surely, with suffix like _Click, _BeforeUpdate, _Current, etc, can be called
"reserved" and need to be avoided on your code.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:44
Joined
May 7, 2009
Messages
19,245
my advice to the OP is to change the function declaration and slight modification:

public function fnCalcPercentFromQty(byval pstrFormName As String, byval pstrFieldName as string)
dim frm As Form
set frm = Forms(pstFormName)
...
...



now on your code:

Me.ProductPercent1.OnDblClick = "=fnCalcPercentFromQty('" & Me.Name & "', 'ProductPercent1')"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:44
Joined
Feb 28, 2001
Messages
27,191
I agree that naming something ending in _Click is probably a poor choice of naming convention, one that I wouldn't use. But apparently the OP did use that. You and I would BOTH say "shouldn't do that" - but the question remains, why doesn't it work correctly in the way it was used?

Paul and theDBguy have suggested alternatives. I'm actually on your side that smig should simplify the routine name. There, I think I have to agree with Paul that this is an execution-time failure. Because of the quotes in the string used to define the OnDblClick routine entry point, that can't be evaluated until you actually take the double click. This is an incredibly fine point. ME has no meaning outside of VBA code.

Smig suggests that this works as a "normal" function call, which is to be expected, because in a normal function call, you HAVE to be in VBA context. But the question then becomes, if ACCESS is preparing to make a call to ENTER an event context, is it in VBA context before the actual entry? I think the answer here has to be no. The "ME." that is part of the event linkage has no meaning because that linkage is in Access context, not VBA context, and so the activation fails.

Your "Me.name" substituted into the string (and change the formal argument from form to form-name/string) would probably work. The substitution occurs in VBA context so should sail right through.
 

smig

Registered User.
Local time
Today, 16:44
Joined
Nov 25, 2009
Messages
2,209
Hi. Try it this way instead.
Code:
Me.ProductPercent1.OnDblClick = "=fnCalcPercentFromQty_Click([Form], 'ProductPercent1')"[/code
[/QUOTE]

Thank you
This did the job :love:
This one worked
Thanks :love:
 
Last edited:

smig

Registered User.
Local time
Today, 16:44
Joined
Nov 25, 2009
Messages
2,209
my advice to the OP is to change the function declaration and slight modification:

public function fnCalcPercentFromQty(byval pstrFormName As String, byval pstrFieldName as string)
dim frm As Form
set frm = Forms(pstFormName)
...
...



now on your code:

Me.ProductPercent1.OnDblClick = "=fnCalcPercentFromQty('" & Me.Name & "', 'ProductPercent1')"
Thank you

Case is already solved as suggested by theDBguy
I like your suggestion,and might go for this direction :)
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 23:44
Joined
Jan 20, 2009
Messages
12,852
you are right, unless he has the same control name.
surely, with suffix like _Click, _BeforeUpdate, _Current, etc, can be called
"reserved" and need to be avoided on your code.
I completely agree. I avoid underscores on procedure names because of the similarities to event syntax. And especially avoid using an actual event name after the underscore. While it might not cause a problem for the program, it certainly could mislead anyone reading the code.
 

Users who are viewing this thread

Top Bottom