Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
NZ Fails to Compile
Tim Gilbert 
    
4 years ago
I am completely befuddled (and more than a bit frustrated) that Access has developed a problem where the NZ function fails to compile for one of my main database files.  I can write the most simple query to use the Nz function and I always get a message box that says:

"There was a compile error on this function.  The Visual Basic module contains a syntax error.  Check the code and recompile it."  

What really confuses me is that I can open a different database file and the Nz function works fine. I have checked for missing references as recommended in the Jan 13 YouTube video.  I have also triple checked my spelling. It just seems to be any use of Nz causes this error.

I would greatly appreciate any suggestions.
Tim Gilbert OP  @Reply  
    
4 years ago

Adam Schwanz  @Reply  
           
4 years ago
Are you trying to put a string in a number field with the NZ replacement?
Tim Gilbert OP  @Reply  
    
4 years ago
No.  It fails every instance of Nz.  I created new queries to test the problem and even the most simple calculated query field would generate the error.  I could open a separate file and create a calculated query field with Nz that works perfectly.
Adam Schwanz  @Reply  
           
4 years ago
Does it do it in a fresh query with no fields being used in it, just do an expression like

Test: NZ(DLOOKUP("CustomerID","CustomerT","ID=1"),0)

Is this a top level query? Or does it have other queries in it? Sometimes the problem can be in another query that you're bringing in and then you have more to look through, can you upload a screenshot of the query/related queries if any?
Scott Axton  @Reply  
        
4 years ago
Tim -
If this was working and then it just started not working it may be related to this Bug Alert

They pushed a fix supposedly yesterday.  You may have to manually update your Office 365.

Tim Gilbert OP  @Reply  
    
4 years ago
I resolved the problem but I am still unsure of the true cause.  
Here's what I did:
1) I opened an Immediate window (Ctrl+G) and typed: ? Nz(Null, 0)
2) The window returned 0 so the problem seemed to be somewhere else than the calculation of Nz
3) Since the message box says a VBA module contains a syntax error I deleted the only module in the file (It was for a custom function)
4) Queries with Nz work exactly as expected now
5) My only problem is that I do not understand what was wrong with the VBA for the custom function (VBA below).  

Option Compare Database

Function TotalSec(DurHr As Variant, DurMin As Variant, DurSec As Variant) As Variant

If IsNull(DurHr) = True And IsNull(DurMin) = True And IsNull(DurSec) = True Then

TotalSec = Null

Exit Function

Else

If IsNull(DurHr) = True Then DurHr = 0

If IsNull(DurMin) = True Then DurMin = 0

If IsNull(DurSec) = True Then DurSec = 0

TotalSec = DurHr * 3600 + DurMin * 60 + DurSec

End If

End Function
Richard Rost  @Reply  
          
4 years ago
Tim, a few notes... I HATE Variants. Avoid them if you can. I also recommend not setting values equal to Null. Make it 0 if possible. Strings empty "". Null is a pain in the ass, pardon my French. It looks to me like you should be able to use Longs for your function instead of Variants. If you want to allow the user to not pass a value, just say something like:

Function TotalSec(Optional H as Long = 0, Optional M as Long = 0, Optional S as Long = 0) As Long
  TotalSec = (H * 3600) + (M * 60) + S
End If


Problem solved. No Variants or Nulls needed.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 8:51:22 AM. PLT: 0s