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 
Significant Figures
Damion Brown 
    
14 months ago
I found this code online to calculate signicant figures, but each time I run it the "Compile Error: Sub or Function not defined" error pops up. I named the module "RoundFunction" in my database. I am using it in an update query to covert all my result_values to 3 sig figs. My query is this:

UPDATE tbl_PercentCalculations SET Result_Value = RoundToSignificantFigures([Result_Value], 3);  


The Function code is below:

Option Explicit
Option Compare Database

Public Function RoundToSignificantFigures(num As Double, sig As Integer) As Double
    If num = 0 Then
        RoundToSignificantFigures = 0
    Else
        Dim d As Double
        d = Int(Log10(Abs(num)))
        RoundToSignificantFigures = Round(num / 10 ^ d, sig - 1) * 10 ^ d
    End If
End Function
Damion Brown OP  @Reply  
    
14 months ago
Any help would be greatly appreciated
Richard Rost  @Reply  
          
14 months ago
What happens when you Compile?
Richard Rost  @Reply  
          
14 months ago
(Hint: I know the answer, I'm just trying to lead you to figure it out yourself).
Damion Brown OP  @Reply  
    
14 months ago
Thanks for the video. So it is trying to take my code and turn it into something that the computer can understand? I deleted that line of code and i was able to compile the database, but not with the code.. Which suggest there is something bad in my code but I am not able to tell which line is the problem. It is highlighting the first row "Public Function RoundToSignificantFigures(num As Double, sig As Integer) As Double" which suggest that the problem is there?
Richard Rost  @Reply  
          
14 months ago
OK, I see looking at your account, you're just a beginner. So I don't know how much VBA experience you have. But when you try to compile this code, it should stop you on the line that says.

d = Int(Log10(Abs(num)))

And that's because Log10 isn't a valid VBA function. This is the problem with just getting code from the internet and copying and pasting it into your database. If you don't understand what it does, it's almost impossible to figure out why it's not working.

Now you can provide your own Log10 function, something like this:

Public Function Log10(x As Double) As Double
    Log10 = Log(x) / Log(10)
End Function


But then you're still going to run into the problem that if you want true mathematical rounding you'll have to also use your own rounding function because access uses something called bankers rounding which might not be what you're looking for depending on the context of your database. In a true scientific fashion you don't want bankers rounding.

So then you'd have to use something like this:

DetailsPublic Function RoundHalfUp(ByVal num As Double, ByVal decimals As Integer) As Double
    Dim factor As Double
    factor = 10 ^ decimals
    If num >= 0 Then
        RoundHalfUp = Int(num * factor + 0.5) / factor
    Else
        RoundHalfUp = -Int(-num * factor + 0.5) / factor
    End If
End Function

Public Function Log10(x As Double) As Double
    Log10 = Log(x) / Log(10)
End Function

Public Function RoundToSignificantFigures(num As Double, sig As Integer) As Double
    If num = 0 Then
        RoundToSignificantFigures = 0
    Else
        Dim d As Double
        d = Int(Log10(Abs(num)))
        RoundToSignificantFigures = RoundHalfUp(num / 10 ^ d, sig - 1) * 10 ^ d
    End If
End Function


Now again I don't recommend you just go copying and pasting this into your database although it should work. But you should take the time to learn all this stuff and exactly what it is doing. I would recommend starting with my Intro to VBA video and then if you want to learn how to program in VBA take a look at my developer course.
Damion Brown OP  @Reply  
    
14 months ago
When I run from the immediate window. Looks like it keeps highlighting this line the line below:

d = Int(Log10(Abs(num)))

More specifically, it highlights "Log10"
Richard Rost  @Reply  
          
14 months ago
Exactly. Read what I just said in my previous comment.
Damion Brown OP  @Reply  
    
14 months ago
I really appreciate it Richard. And yes, I need improve my VBA skills. Cheers, D.

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/6/2026 8:32:51 AM. PLT: 0s