473,513 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

user function in a query

JC
The following function creates a display name from a [last name] and
[her name] and [his name]. I call the function from a query. When I
run the query those records that have BOTH a his and her name appear
correctly, but if one of the names is blank "#ERROR" appears in the
field where the combined name should appear. Why doesn't the function
process the else statement correctly? The If statement works just
fine.
Thank you in advance.
- jc -

This is how it is called from the query:
name:cname([his_name], [her_name])

The function: (resides in a module)
Public Function cName(strHisName As String, strHerName As String) As
String
On Error GoTo Err_cName

If (Len(Trim(Nz(strHisName))) > 0) And (Len(Trim(Nz(strHerName))) >
0) Then
cName = Trim(strHisName) & " & " & Trim(strHerName)
Else
cName = Trim(Nz(strHisName)) & Trim(Nz(strHerName))
End If

Exit_cName:
Exit Function

Err_cName:
cName = ""
MsgBox "Runtime Error # " & Err.Number & vbCrLf & vbLf &
Err.Description
Resume Exit_cName

End Function

Nov 13 '05 #1
9 1791
JC wrote:
The following function creates a display name from a [last name] and
[her name] and [his name]. I call the function from a query. When I
run the query those records that have BOTH a his and her name appear
correctly, but if one of the names is blank "#ERROR" appears in the
field where the combined name should appear. Why doesn't the function
process the else statement correctly? The If statement works just
fine.
Thank you in advance.
- jc -

This is how it is called from the query:
name:cname([his_name], [her_name])

The function: (resides in a module)
Public Function cName(strHisName As String, strHerName As String) As
String
On Error GoTo Err_cName

If (Len(Trim(Nz(strHisName))) > 0) And (Len(Trim(Nz(strHerName))) >
0) Then
cName = Trim(strHisName) & " & " & Trim(strHerName)
Else
cName = Trim(Nz(strHisName)) & Trim(Nz(strHerName))
End If

Exit_cName:
Exit Function

Err_cName:
cName = ""
MsgBox "Runtime Error # " & Err.Number & vbCrLf & vbLf &
Err.Description
Resume Exit_cName

End Function


Instead of:

cName = Trim(Nz(strHisName)) & Trim(Nz(strHerName))

Try:

cName = Trim(Nz(varHisName,"")) & Trim(Nz(varHerName,""))

Hint: Use variant variables so that Null values can be dealt with.

It would also be good to check for the case where both varHisName and
varHerName are Null. Return "Mr. & Mrs."?

James A. Fortune

Nov 13 '05 #2
On 10 Aug 2005 11:07:48 -0700, JC wrote:
The following function creates a display name from a [last name] and
[her name] and [his name]. I call the function from a query. When I
run the query those records that have BOTH a his and her name appear
correctly, but if one of the names is blank "#ERROR" appears in the
field where the combined name should appear. Why doesn't the function
process the else statement correctly? The If statement works just
fine.
Thank you in advance.
- jc -

This is how it is called from the query:
name:cname([his_name], [her_name])

The function: (resides in a module)
Public Function cName(strHisName As String, strHerName As String) As
String
On Error GoTo Err_cName

If (Len(Trim(Nz(strHisName))) > 0) And (Len(Trim(Nz(strHerName))) >
0) Then
cName = Trim(strHisName) & " & " & Trim(strHerName)
Else
cName = Trim(Nz(strHisName)) & Trim(Nz(strHerName))
End If

Exit_cName:
Exit Function

Err_cName:
cName = ""
MsgBox "Runtime Error # " & Err.Number & vbCrLf & vbLf &
Err.Description
Resume Exit_cName

End Function


Note:
regarding > name:cname([his_name], [her_name]) <
Name is a reserved Access/VBA/Jet word and should not be used as a
field name.
For additional reserved words, see the Microsoft KnowledgeBase article
for your version of Access:

109312 'Reserved Words in Microsoft Access' for Access 97
209187 'ACC2000: Reserved Words in Microsoft Access'
286335 'ACC2002: Reserved Words in Microsoft Access'
321266 'ACC2002: Microsoft Jet 4.0 Reserved Words'
Are you just trying to combine the His and Her names with a "&" if
they both exist?
No need for a user Defined function.
Just copy the below expression directly into the query grid.

BothNames: Nz([strHisName],[strHerName]) & IIf(Not
IsNull([strHisName]) And Not IsNull([strHerName])," & " &
[strHerName])

Mark & Beth
Mark
Beth

--
Fred
Please only reply to this newsgroup.
I do not reply to personal email.
Nov 13 '05 #3
There are many problems in this function. First off, the Nz function
normally returns 0 if the argument is null. So I guess what you would
do for a string is Nz(str, "").

Second off your If statements should be If (Len(Trim(strHisName & "")))
0). There is no need for Nz here since nulls are avoided by concatenating the empty string.


Third off your function is more complicated than it should be. This is
what it can be reduced to:
Trim(strHisName) & IIf(IsNull(strHisName) or IsNull(strHerName), ""," &
") & Trim(strHisName)

I also thought about using the + operator like this:
Trim(strHisName) & (" & " + Trim(strHisName))

but couldn't see how it would work in your case.

Nov 13 '05 #4
JC wrote:
The following function creates a display name from a [last name] and
[her name] and [his name]. I call the function from a query. When I
run the query those records that have BOTH a his and her name appear
correctly, but if one of the names is blank "#ERROR" appears in the
field where the combined name should appear. Why doesn't the function
process the else statement correctly? The If statement works just
fine.
Thank you in advance.


As others have noted, you can use NZ. However, in your function you
care declaring His/Her names to be strings. Why not declare them as
Variants and then check in the function if His/Her is null or not.
Nov 13 '05 #5
JC
Thank you for all the replys and the constructive comments. I have
much to learn... I guess I have had the wrong understanding of Nz up
until this point.
A couple of questions:
If you create a record and all the string fields are not filled in,
don't they have a value of null at that point? They haven't been
initialized, right? So how do you test for that?

Fred, that expression you provided does just what I want. Thank you.

- jc -

Nov 13 '05 #6
JC wrote:
Thank you for all the replys and the constructive comments. I have
much to learn... I guess I have had the wrong understanding of Nz up
until this point.
A couple of questions:
If you create a record and all the string fields are not filled in,
don't they have a value of null at that point? They haven't been
initialized, right? So how do you test for that?
Open up your table. Is there a default value? The default value is
what is inserted into the field when the record is created and no value
provided. If blank, it will be null for new records.

IsNull() is one way to test for null.

Fred, that expression you provided does just what I want. Thank you.

- jc -

Nov 13 '05 #7
JC
Thank you - as I said before, I have much to learn.

Nov 13 '05 #8
"JC" <jc********@gmail.com> wrote in
news:11**********************@g43g2000cwa.googlegr oups.com:
The following function creates a display name from a [last name]
and [her name] and [his name]. I call the function from a query.
When I run the query those records that have BOTH a his and her
name appear correctly, but if one of the names is blank "#ERROR"
appears in the field where the combined name should appear. Why
doesn't the function process the else statement correctly? The If
statement works just fine.


I believe it was Trevor Best who recommended this method for
concatenating Lastname + Firstname without requiring a bunch of
conditionals. It depends on the use of the + operator to propagate
Nulls.

Mid(("12" + LastName) & (", " + FirstName), 3)

If either LastName or FirstName is Null, that part of the equation
returns Null. As long as either is not Null.

A variation this should be able to handle your problem, without
needing to make it into an external function.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #9
"JC" <jc********@gmail.com> wrote in
news:11**********************@z14g2000cwz.googlegr oups.com:
Thank you - as I said before, I have much to learn.


A couple of helpful articles about Nulls:

http://allenbrowne.com/casu-02.html

http://allenbrowne.com/casu-11.html

http://allenbrowne.com/casu-12.html

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2028
by: markus | last post by:
This string does not work in php: $sql="UPDATE mytable SET myfield=myuserfunction(myfield)" What I want is my function to evaluate each field and put the new value in the field. For...
1
2074
by: LineVoltageHalogen | last post by:
I have many stored procs in my database and I can call them just by their name uspMyProc with success always. However, I just created a user function ufnMyFunction as the same user that I created...
1
9085
by: muelli75 | last post by:
Hi! Im getting insane by solving a problem .... I try to define a function which uses a code-snippet from another file. My base are the codes from the great book "WebDataBase-Book by H....
6
4370
by: Paul M. | last post by:
Hello, does anyone have either a User Function Library (or the source for one) to create a CRC32 checksum for a given string? I want to use the function in a crystal formula thus: formula =...
0
1252
by: Carlos Lozano | last post by:
Hello, I am converting a MS Access application into ASP.NET. It has a many CR reports that calls external functions in a VBA library. I have been trying to create a user Function Library (UFL)...
3
9208
by: njgreen2001 | last post by:
I am attempting an access database which tracks blood components in our medical facility. When a component is issued to a patient there is a button to push which links to a report that runs a...
2
1758
by: icyajax | last post by:
HI All, I m trying to write an SQL query in VBA to a sql string. It will be used for a search form. I want the query to evaluate multiple text boxes filled in by the user. For example, they are...
32
2647
by: wexx | last post by:
I have been looking for some time now (reading books off Safari, searching through forums,etc) I have found no solution to this problem. I turn to anyone of you that may be able to help me. I'm...
11
1958
by: trackside | last post by:
I'm using the great dd.dragdrop Library for many parts of the page, including loading in a user selected image via the "swapImage()" function. var imgRe = /^.+\.(jpg|jpeg|gif|png)$/i; ...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.