dbTalk Databases Forums  

Using DLookup inside a user-defined function which is inside a query

comp.databases.ms-access comp.databases.ms-access


Discuss Using DLookup inside a user-defined function which is inside a query in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Lane
 
Posts: n/a

Default Using DLookup inside a user-defined function which is inside a query - 01-03-2012 , 01:15 AM






Dear Access gurus,

I am not sure if using DLookup inside a user-defined function and then
using that function inside a query is forbidden. When I did that, the
following error is encountered;

"Cannot open a form whose underlying query contains a user-defined
function that attempts to set or get the form's RecordsetClone
property"

I never use RecordsetClone property in my code. Does anyone have any
clues? Thank you.

Reply With Quote
  #2  
Old   
Douglas J Steele
 
Posts: n/a

Default Re: Using DLookup inside a user-defined function which is inside a query - 01-03-2012 , 05:47 AM






What's the code of your function? And how are you attempting to use that
function when you get the error?


"Lane" wrote in message
news:a2c0386e-827b-495a-ad5c-07b6d9b29e62 (AT) b20g2000pro (DOT) googlegroups.com...

Dear Access gurus,

I am not sure if using DLookup inside a user-defined function and then
using that function inside a query is forbidden. When I did that, the
following error is encountered;

"Cannot open a form whose underlying query contains a user-defined
function that attempts to set or get the form's RecordsetClone
property"

I never use RecordsetClone property in my code. Does anyone have any
clues? Thank you.

Reply With Quote
  #3  
Old   
Lane
 
Posts: n/a

Default Re: Using DLookup inside a user-defined function which is inside a query - 01-03-2012 , 05:50 PM



Thanks for the replies. The function which is used inside the query
actually calls another query within. Is this a problem?

The code is below.

Public Function GetRecencyAnl(ID As String, AnnouncementDate As Date)
As Integer
On Error GoTo errorhandler
Dim latest_year As Integer
Dim Criteria As String

Criteria = "ID=" + ID
latest_year = DLookup("Latest_yr", "RecentAnlQry",
Criteria)
GetRecencyAnl = latest_year - Year(AnnouncementDate) + 1

Exit Function
errorhandler:
'MsgBox Err.Source & "-->" & Err.Description, , "Error"
End Function



On Jan 3, 7:47*pm, "Douglas J Steele"
<NOSPAM_djsteele (AT) NOSPAM_gmail (DOT) com> wrote:
Quote:
What's the code of your function? And how are you attempting to use that
function when you get the error?

"Lane" *wrote in message

news:a2c0386e-827b-495a-ad5c-07b6d9b29e62 (AT) b20g2000pro (DOT) googlegroups.com...

Dear Access gurus,

I am not sure if using DLookup inside a user-defined function and then
using that function inside a query is forbidden. When I did that, the
following error is encountered;

"Cannot open a form whose underlying query contains a user-defined
function that attempts to set or get the form's RecordsetClone
property"

I never use RecordsetClone property in my code. Does anyone have any
clues? Thank you.

Reply With Quote
  #4  
Old   
Phil
 
Posts: n/a

Default Re: Using DLookup inside a user-defined function which is inside a query - 01-03-2012 , 07:23 PM



On 03/01/2012 23:50:55, Lane wrote:
Quote:
Thanks for the replies. The function which is used inside the query
actually calls another query within. Is this a problem?

The code is below.

Public Function GetRecencyAnl(ID As String, AnnouncementDate As Date)
As Integer
On Error GoTo errorhandler
Dim latest_year As Integer
Dim Criteria As String

Criteria = "ID=" + ID
latest_year = DLookup("Latest_yr", "RecentAnlQry",
Criteria)
GetRecencyAnl = latest_year - Year(AnnouncementDate) + 1

Exit Function
errorhandler:
'MsgBox Err.Source & "-->" & Err.Description, , "Error"
End Function



On Jan 3, 7:47*pm, "Douglas J Steele"
NOSPAM_djsteele (AT) NOSPAM_gmail (DOT) com> wrote:
What's the code of your function? And how are you attempting to use that
function when you get the error?

"Lane" *wrote in message

news:a2c0386e-827b-495a-ad5c-07b6d9b29e62 (AT) b20g2000pro (DOT) googlegroups.com...

Dear Access gurus,

I am not sure if using DLookup inside a user-defined function and then
using that function inside a query is forbidden. When I did that, the
following error is encountered;

"Cannot open a form whose underlying query contains a user-defined
function that attempts to set or get the form's RecordsetClone
property"

I never use RecordsetClone property in my code. Does anyone have any
clues? Thank you.


Assuming ID is a number, try using & instead of +
Criteria = ID = " & ID

Phil

Reply With Quote
  #5  
Old   
JHB
 
Posts: n/a

Default Re: Using DLookup inside a user-defined function which is inside a query - 01-03-2012 , 10:55 PM



Quote:
"Lane" <lightaiyee (AT) gmail (DOT) com> skrev i en meddelelse news:1ff07fdc-336d-47b3-bf92-c5276cf9c77c (AT) t16g2000vba (DOT) googlegroups.com...
Thanks for the replies. The function which is used inside the query actually calls another query within. Is this a problem?

The code is below.

Public Function GetRecencyAnl(ID As String, AnnouncementDate As Date) As Integer
On Error GoTo errorhandler
Dim latest_year As Integer
Dim Criteria As String

Criteria = "ID=" + ID
latest_year = DLookup("Latest_yr", "RecentAnlQry", Criteria)
GetRecencyAnl = latest_year - Year(AnnouncementDate) + 1

Exit Function
errorhandler:
'MsgBox Err.Source & "-->" & Err.Description, , "Error"
End Function


Hello Lane.



You have declared ID as a string value, (. GetRecencyAnl(ID As String, ...), if so, you have to tread it as a string in the query.



Criteria = "ID=" & "'" & ID & "'"



Regards

Jørn

Reply With Quote
  #6  
Old   
Lane
 
Posts: n/a

Default Re: Using DLookup inside a user-defined function which is inside a query - 01-04-2012 , 12:13 AM



Thank you very much. The problem is solved. The error message from
Microsoft led me to look at the wrong places. Wasted so much time.

On Jan 4, 12:55*pm, "JHB" <jhb... (AT) hotmail (DOT) com> wrote:
Quote:
"Lane" <lightai... (AT) gmail (DOT) com> skrev i en meddelelsenews:1ff07fdc-336d-47b3-bf92-c5276cf9c77c (AT) t16g2000vba (DOT) googlegroups.com...
Thanks for the replies. The function which is used inside the query actually calls another query within. Is this a problem?
The code is below.
Public Function GetRecencyAnl(ID As String, AnnouncementDate As Date) AsInteger
On Error GoTo errorhandler
* *Dim latest_year As Integer
* *Dim Criteria As String

* *Criteria = "ID=" + ID
* latest_year = DLookup("Latest_yr", "RecentAnlQry", Criteria)
* *GetRecencyAnl = latest_year - Year(AnnouncementDate) + 1

* *Exit Function
errorhandler:
* *'MsgBox Err.Source & "-->" & Err.Description, , "Error"
End Function

Hello Lane.

You have declared ID as a string value, (. GetRecencyAnl(ID As String, ..), if so, you have to tread it as a string in the query.

Criteria = "ID=" & "'" & ID & "'"

Regards

Jørn

Reply With Quote
  #7  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Using DLookup inside a user-defined function which is inside a query - 01-04-2012 , 08:39 AM



JHB wrote:
Quote:
"Lane" <lightaiyee (AT) gmail (DOT) com> skrev i en meddelelse
news:1ff07fdc-336d-47b3-bf92-c5276cf9c77c (AT) t16g2000vba (DOT) googlegroups.com...
Thanks for the replies. The function which is used inside the query
actually calls another query within. Is this a problem?

The code is below.

Public Function GetRecencyAnl(ID As String, AnnouncementDate As
Date) As Integer
On Error GoTo errorhandler
Dim latest_year As Integer
Dim Criteria As String

Criteria = "ID=" + ID
latest_year = DLookup("Latest_yr", "RecentAnlQry", Criteria)
GetRecencyAnl = latest_year - Year(AnnouncementDate) + 1

Exit Function
errorhandler:
'MsgBox Err.Source & "-->" & Err.Description, , "Error"
End Function



Hello Lane.



You have declared ID as a string value, (. GetRecencyAnl(ID As
String, ..), if so, you have to tread it as a string in the query.



Criteria = "ID=" & "'" & ID & "'"



That's not quite true. it only has to be treated as a string in the query if
the expression on the left side of the comparison operator is also a string.
IOW, if the ID field in the table is text, then the value being compared has
to be delimited. If the ID variable (to avoid confusion, it's a good idea to
differentiate your field and variable names) contains a non-numeric value,
the sql will fail if you failed to delimit the value due to a type
conversion failure. But that has nothing to do with why it should be
delimited. It is the field's datatype that is the dermining factor, not the
variable type.

Reply With Quote
  #8  
Old   
JHB
 
Posts: n/a

Default Re: Using DLookup inside a user-defined function which is inside a query - 01-05-2012 , 08:33 PM



"Bob Barrows" <reb01501 (AT) NOyahooSPAM (DOT) com> skrev i en meddelelse news:je1ob2$dj8$1 (AT) dont-email (DOT) me...
Quote:
That's not quite true. it only has to be treated as a string in the query if
the expression on the left side of the comparison operator is also a string.
IOW, if the ID field in the table is text, then the value being compared has
to be delimited. If the ID variable (to avoid confusion, it's a good idea to
differentiate your field and variable names) contains a non-numeric value,
the sql will fail if you failed to delimit the value due to a type
conversion failure. But that has nothing to do with why it should be
delimited. It is the field's datatype that is the dermining factor, not the
variable type.


I do agree.



What I meant was that if the ID was a string value, then it should be treated as a string value in the query.



What made me believe that it was a string value was that Lane has declared ID as a string in the procedure call, therefor I wrote "if so", (if it is correct that .., then .).

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.