Acess 2010 VBA Display Fraction As Decimal

Hi Experts,

I have the following calculation in VBA.

Dim Me.GRP_MAX_YTH_NBR As Integer.  Value = 3
Dim Me.GRP_MAX_LDR_NBR As Integer,  Value = 4
Dim Result As Long

Result = Me.GRP_MAX_YTH_NBR \ Me.GRP_MAX_LDR_NBR

The above calculation returns Result = 0

How do I get it to return a result of 0.75 which is the decimal equivalent the fraction 3/4?

Thanks,
Bob C.
Bob CollisonSystem ArchitectAsked:
Who is Participating?
 
NorieVBA ExpertCommented:
Change the declaration of Result from Long (integer) to Double.
Dim Result As Double 

Open in new window

1
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
hi Bob,

adding onto Norie's comment about changing data type ... (long integer cannot  store a decimal place, therefore rounding is done on factors and result is truncated) ...

Actually, though, there are two things wrong:

1. Dim Result As Long: whole numbers cannot have decimal places. Therefore, 0.75 cannot be written for the result.
2. Result = factor1 \ factor2  : formula is truncating decimal places by using \
   therefore Result does not even have a chance to get rounded (due to Dim as Long); it is already truncated

/ and \ are different operators for division

Table of operators
https://support.office.com/en-us/article/Table-of-operators-E1BC04D5-8B76-429F-A252-E9223117D6BD


use / for regular division

here are some examples using / and \ so you can see the difference:    
?3/4
 0.75 

?3\4
 0 

? 3 \ 5
 0 
? 7 \ 5
 1 

?3 / 5
 0.6 

? 7 / 5
 1.4 

Open in new window


have an awesome day,
crystal
0
 
Bob CollisonSystem ArchitectAuthor Commented:
Hi Experts,

Thanks for the solution / comments.

Bob C.
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
you're welcome, Bob ~ happy to help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.