PC Review


Reply
Thread Tools Rate Thread

Custom Function in a Select Query

 
 
=?Utf-8?B?S2lyayBQLg==?=
Guest
Posts: n/a
 
      4th Jan 2005
Instead of using the built in IIF function, I wanted to try my hand at
building a custom function using SELECT CASE instead.

I want my function to look at the [Legal Entity Cd] field, and perform an
action based on the legal entity code contained within this field. The
Select Case would look something like this:

Function Entity()
Select Case [Legal Entity Cd]
Case "10"
Debug.Print "NSP-MN"
Case "11"
Debug.Print "NSP-WI"
Case Else
Debug.Print "Other"
End Select

When I try to incorporate this function into my select query, I get a
"External Name not Defined" error. Any ideas?
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Skw=?=
Guest
Posts: n/a
 
      4th Jan 2005
Hi Kirk,

The function "Entity" have to be public. I usually put them in the "Module"
area.
You also have pass the [Legal Entity Cd] as its parameter.

Function Entity(Arg1 as variant) as variant
Select Case Arg1
 
Reply With Quote
 
=?Utf-8?B?S2lyayBQLg==?=
Guest
Posts: n/a
 
      4th Jan 2005
I put the function in a Module, and tried passing in [Legal Entity Cd] as a
parameter. I'm getting an error message saying "compile error - expected
identifier" Here's my code:

Public Function Entity([Legal Entity Cd]As Variant)As Variant

Select Case [Legal Entity Cd]
Case "10"
Debug.Print "NSP-MN"
Case "11"
Debug.Print "NSP-WI"
Case Else
Debug.Print "Other"
End Select

End Function

"JL" wrote:

> Hi Kirk,
>
> The function "Entity" have to be public. I usually put them in the "Module"
> area.
> You also have pass the [Legal Entity Cd] as its parameter.
>
> Function Entity(Arg1 as variant) as variant
> Select Case Arg1
> .
> .
> .
> End Select
> End Function
>
> Hope this helps.
>
>
> "Kirk P." wrote:
>
> > Instead of using the built in IIF function, I wanted to try my hand at
> > building a custom function using SELECT CASE instead.
> >
> > I want my function to look at the [Legal Entity Cd] field, and perform an
> > action based on the legal entity code contained within this field. The
> > Select Case would look something like this:
> >
> > Function Entity()
> > Select Case [Legal Entity Cd]
> > Case "10"
> > Debug.Print "NSP-MN"
> > Case "11"
> > Debug.Print "NSP-WI"
> > Case Else
> > Debug.Print "Other"
> > End Select
> >
> > When I try to incorporate this function into my select query, I get a
> > "External Name not Defined" error. Any ideas?

 
Reply With Quote
 
=?Utf-8?B?Skw=?=
Guest
Posts: n/a
 
      4th Jan 2005
Hi Kirk,

Well, this is how it should be done.

In your query calling the function,
select .., Entity([Legal Entity Cd]), ...
from ...
.....

In your Module which containt the function "Entity",
Public Function Entity(EntityCd As Variant)As Variant

Select Case EntityCd
Case "10"
Entity = "NSP-MN"
Debug.Print "NSP-MN"
Case "11"
Entity = "NSP-WI"
Debug.Print "NSP-WI"
Case Else
Entity = "Other"
Debug.Print "Other"
End Select
End Function

Hope this is more clear for you.

"Kirk P." wrote:

> I put the function in a Module, and tried passing in [Legal Entity Cd] as a
> parameter. I'm getting an error message saying "compile error - expected
> identifier" Here's my code:
>
> Public Function Entity([Legal Entity Cd]As Variant)As Variant
>
> Select Case [Legal Entity Cd]
> Case "10"
> Debug.Print "NSP-MN"
> Case "11"
> Debug.Print "NSP-WI"
> Case Else
> Debug.Print "Other"
> End Select
>
> End Function
>
> "JL" wrote:
>
> > Hi Kirk,
> >
> > The function "Entity" have to be public. I usually put them in the "Module"
> > area.
> > You also have pass the [Legal Entity Cd] as its parameter.
> >
> > Function Entity(Arg1 as variant) as variant
> > Select Case Arg1
> > .
> > .
> > .
> > End Select
> > End Function
> >
> > Hope this helps.
> >
> >
> > "Kirk P." wrote:
> >
> > > Instead of using the built in IIF function, I wanted to try my hand at
> > > building a custom function using SELECT CASE instead.
> > >
> > > I want my function to look at the [Legal Entity Cd] field, and perform an
> > > action based on the legal entity code contained within this field. The
> > > Select Case would look something like this:
> > >
> > > Function Entity()
> > > Select Case [Legal Entity Cd]
> > > Case "10"
> > > Debug.Print "NSP-MN"
> > > Case "11"
> > > Debug.Print "NSP-WI"
> > > Case Else
> > > Debug.Print "Other"
> > > End Select
> > >
> > > When I try to incorporate this function into my select query, I get a
> > > "External Name not Defined" error. Any ideas?

 
Reply With Quote
 
=?Utf-8?B?S2lyayBQLg==?=
Guest
Posts: n/a
 
      4th Jan 2005
Works perfectly. Thanks for your help!

"JL" wrote:

> Hi Kirk,
>
> Well, this is how it should be done.
>
> In your query calling the function,
> select .., Entity([Legal Entity Cd]), ...
> from ...
> ....
>
> In your Module which containt the function "Entity",
> Public Function Entity(EntityCd As Variant)As Variant
>
> Select Case EntityCd
> Case "10"
> Entity = "NSP-MN"
> Debug.Print "NSP-MN"
> Case "11"
> Entity = "NSP-WI"
> Debug.Print "NSP-WI"
> Case Else
> Entity = "Other"
> Debug.Print "Other"
> End Select
> End Function
>
> Hope this is more clear for you.
>
> "Kirk P." wrote:
>
> > I put the function in a Module, and tried passing in [Legal Entity Cd] as a
> > parameter. I'm getting an error message saying "compile error - expected
> > identifier" Here's my code:
> >
> > Public Function Entity([Legal Entity Cd]As Variant)As Variant
> >
> > Select Case [Legal Entity Cd]
> > Case "10"
> > Debug.Print "NSP-MN"
> > Case "11"
> > Debug.Print "NSP-WI"
> > Case Else
> > Debug.Print "Other"
> > End Select
> >
> > End Function
> >
> > "JL" wrote:
> >
> > > Hi Kirk,
> > >
> > > The function "Entity" have to be public. I usually put them in the "Module"
> > > area.
> > > You also have pass the [Legal Entity Cd] as its parameter.
> > >
> > > Function Entity(Arg1 as variant) as variant
> > > Select Case Arg1
> > > .
> > > .
> > > .
> > > End Select
> > > End Function
> > >
> > > Hope this helps.
> > >
> > >
> > > "Kirk P." wrote:
> > >
> > > > Instead of using the built in IIF function, I wanted to try my hand at
> > > > building a custom function using SELECT CASE instead.
> > > >
> > > > I want my function to look at the [Legal Entity Cd] field, and perform an
> > > > action based on the legal entity code contained within this field. The
> > > > Select Case would look something like this:
> > > >
> > > > Function Entity()
> > > > Select Case [Legal Entity Cd]
> > > > Case "10"
> > > > Debug.Print "NSP-MN"
> > > > Case "11"
> > > > Debug.Print "NSP-WI"
> > > > Case Else
> > > > Debug.Print "Other"
> > > > End Select
> > > >
> > > > When I try to incorporate this function into my select query, I get a
> > > > "External Name not Defined" error. Any ideas?

 
Reply With Quote
 
=?Utf-8?B?Skw=?=
Guest
Posts: n/a
 
      4th Jan 2005
Hi Kirk,

Sorry that I did not mention another way. There are so many way to do the
same stuff.
Instead of use IIF or function, you can also use a look up table to join
with your table to get the value that you need. That is my prefer method to
handle lookup code.
Just for your information.


"Kirk P." wrote:

> Instead of using the built in IIF function, I wanted to try my hand at
> building a custom function using SELECT CASE instead.
>
> I want my function to look at the [Legal Entity Cd] field, and perform an
> action based on the legal entity code contained within this field. The
> Select Case would look something like this:
>
> Function Entity()
> Select Case [Legal Entity Cd]
> Case "10"
> Debug.Print "NSP-MN"
> Case "11"
> Debug.Print "NSP-WI"
> Case Else
> Debug.Print "Other"
> End Select
>
> When I try to incorporate this function into my select query, I get a
> "External Name not Defined" error. Any ideas?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TableAdapter - Custom Select Query Mark Microsoft ADO .NET 1 16th May 2008 05:28 PM
nesting a function with query select =?Utf-8?B?c3RpY2thbmRyb2Nr?= Microsoft Access VBA Modules 0 7th Sep 2007 06:06 PM
Select query count function help =?Utf-8?B?QnJpYW4gQ29sZmF4?= Microsoft Access Queries 1 27th Jul 2006 02:16 PM
SELECT function in Query alexparks Microsoft Access Queries 9 5th Jul 2004 11:31 AM
Excel Custom Function with Select Case Heather Microsoft Excel Programming 14 6th Nov 2003 11:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:22 PM.