473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Invalid use of Null

Seth Schrock
2,965 Expert 2GB
I'm trying to use the Nz() function in a replace function, but I'm getting an Invalid use of Null error and I don't know why. I thought that Nz() was supposed to take care of that. Here is what I have:

Expand|Select|Wrap|Line Numbers
  1. With Forms!frmIncidents
  2.      strLegalWording = Replace(strLegalWording, "%Day%", Nz(Day(!IncDate), "__________") & "")
  3.      strLegalWording = Replace(strLegalWording, "%Month%", Nz(MonthName(Month(!IncDate)), "__________") & "")
  4.      strLegalWording = Replace(strLegalWording, "%Year%", Nz(Year(!IncDate), "__________") & "")
  5.  
  6. End With
I tried taking out the Day() function out of the Nz() function in line 2 so that the line looks like this:
Expand|Select|Wrap|Line Numbers
  1. strLegalWording = Replace(strLegalWording, "%Day%", Nz(!IncDate, "__________") & "")
and it works (however, I get the whole date instead of just the day). Is there something about having the Day() function in there that makes it so that I can't use this in the Nz() function?
Jan 1 '13 #1

✓ answered by Stewart Ross

Hi Seth.

Nz on the outside of your expression cannot prevent the date functions from failing when the !IncDate argument is null on the inside of your expression.

As you are repeating the same operation three times I suggest separating the replace component into a single function which can guard against null dates more easily:

Expand|Select|Wrap|Line Numbers
  1. Public Function fReplaceDateText(LegalText As String, ByVal WhichPart As String, WhatDate As Variant) As String
  2.     Dim strSubstitute As String
  3.     Const cNoValueText = "____________"
  4.     If IsNull(WhatDate) Then
  5.         strSubstitute = cNoValueText
  6.     Else
  7.         Select Case WhichPart
  8.             Case "%Day%":
  9.                 strSubstitute = CStr(Day(WhatDate))
  10.             Case "%Month%":
  11.                 strSubstitute = CStr(Month(WhatDate))
  12.             Case "%Year%":
  13.                 strSubstitute = CStr(Year(WhatDate))
  14.             Case Else:
  15.                 strSubstitute = cNoValueText
  16.         End Select
  17.     End If
  18.     fReplaceDateText = Replace(LegalText, WhichPart, strSubstitute)
  19. End Function
You can then call this function to do the replacements for you:

Expand|Select|Wrap|Line Numbers
  1. With Forms!frmIncidents
  2.     strLegalWording = fReplaceDateText(strLegalWording, "%Day%", !IncDate)
  3.     strLegalWording = fReplaceDateText(strLegalWording, "%Month%", !IncDate)
  4.     strLegalWording = fReplaceDateText(strLegalWording, "%Year%", !IncDate)
  5. End With
-Stewart

2 2729
Stewart Ross
2,545 Expert Mod 2GB
Hi Seth.

Nz on the outside of your expression cannot prevent the date functions from failing when the !IncDate argument is null on the inside of your expression.

As you are repeating the same operation three times I suggest separating the replace component into a single function which can guard against null dates more easily:

Expand|Select|Wrap|Line Numbers
  1. Public Function fReplaceDateText(LegalText As String, ByVal WhichPart As String, WhatDate As Variant) As String
  2.     Dim strSubstitute As String
  3.     Const cNoValueText = "____________"
  4.     If IsNull(WhatDate) Then
  5.         strSubstitute = cNoValueText
  6.     Else
  7.         Select Case WhichPart
  8.             Case "%Day%":
  9.                 strSubstitute = CStr(Day(WhatDate))
  10.             Case "%Month%":
  11.                 strSubstitute = CStr(Month(WhatDate))
  12.             Case "%Year%":
  13.                 strSubstitute = CStr(Year(WhatDate))
  14.             Case Else:
  15.                 strSubstitute = cNoValueText
  16.         End Select
  17.     End If
  18.     fReplaceDateText = Replace(LegalText, WhichPart, strSubstitute)
  19. End Function
You can then call this function to do the replacements for you:

Expand|Select|Wrap|Line Numbers
  1. With Forms!frmIncidents
  2.     strLegalWording = fReplaceDateText(strLegalWording, "%Day%", !IncDate)
  3.     strLegalWording = fReplaceDateText(strLegalWording, "%Month%", !IncDate)
  4.     strLegalWording = fReplaceDateText(strLegalWording, "%Year%", !IncDate)
  5. End With
-Stewart
Jan 1 '13 #2
Seth Schrock
2,965 Expert 2GB
Works perfectly! I see now what the error was so I should be able to work around this in the future. Thanks Stewart.
Jan 1 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kris M | last post by:
How do i handle a null value for a date variable type. I am retrieving date data from an access database and storing the records in an array for processing. The array field has a date type and the...
69
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
1
by: Patrick Dunnigan | last post by:
Hi, I am attempting a bulk copy from a c program into SQL Server 2000 using DBLib in freeTDS 0.63 RC11 (gcc 3.4.3, RH 9). I am getting an error message that I cannot find any documentation on. ...
2
by: headware | last post by:
I'm getting a weird problem in an Access query. I have a table that contains a field calle F1 that's a 2 character text field. The first character is always a number. What I'd like to do is find...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
13
by: Federico Balbi | last post by:
Hi, I was wondering if PGSQL has a function similar to binary_checksum() of MS SQL Server 2000. It is pretty handy when it comes to compare rows of data instead of having to write long boolean...
1
by: shonclark | last post by:
"Hi there, I am having a bit of a headache trying to get the following coding to work. What I am trying to accomplish is turn around totals between and open date and resolution date. My coding...
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
1
by: hough747 | last post by:
I am an Access newbie and know nothing of database design. A Coder the Database in MS 2003 (XP) Access. When i am looking at the Main Spreadsheet and click on a form, everything works fine on the...
1
by: Dave55 | last post by:
This is my code.... import javax.swing.JOptionPane; public class AdmissionsSpence { public static void main(String args) { String entranceScore, gradePointAverage; gradePointAverage...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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,...

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.