Division gives whole numbers in VBA

Rowen

Registered User.
Local time
Today, 07:34
Joined
Apr 26, 2009
Messages
32
I'm hoping someone can help me, I'm trying to divide a number entered into a text box by 60 in VBA. However when I input a value such as 30 into the text box so that the equation is 30/60 the answer it is giving me is 0. I need this answer with decimal places, how do I get vba to do that for me?
 
Hello Rowen,

it seem that you force integer for the result. One thing to check: Make sure that you use slash, not backslash.
30/60 results in 0,5
30\60 results in 0
(you can try in the immediate window)

HTH
Thomas
 
The code I have is:

totalTime = (Me.txtMins / 60)

So I'm definitly using the slash, but I tried with the backslash and no joy there either.
 
Is the underlying field (assuming txtmins is a bound field) integer type?
Is totalTime defined as Integer type?

Is me.txtmins the right reference? (try typing ? YourFormName!txtmins in the immediate window, or putting msgbox me.txtmins on the event of some button click or other)

If it's neither of those, try:

totalTime = (CLng(Me.txtMins) / 60)
 
txtMins isn't a bound field, I've set it as a general number, but maybe that's the issue?

I can't set the field as a bound field, as the form is designed for the user to type in the number of hours and minutes that they've worked on something for. This is then multiplied by their costs per hour, but the only way I can see to do this is to get minutes into an hour format, hence the dividing by 60.
 
How is the time entered in txtMins (what would someone type in there)?
 
It's ok I figured out my mistake, I was using an integer type when I should have been using a varient. :rolleyes: Oh well at least I'll have learned from this.
 
It's ok I figured out my mistake, I was using an integer type when I should have been using a varient. :rolleyes: Oh well at least I'll have learned from this.
No, you shouldn't use a variant. However that might work also. One of the two must be a double. The result will be converted to a double also.
Code:
totalTime = (Cdbl(Me.txtMins) / 60)
If totalTime is an integer, the result will be an integer. If it's a Long then it'll be a Long. If it is a textbox configured as an unformatted general number, the result will be a double. If it's a variant, it will be a double. That's why variant also works. Because a variant must represent any type, this type produces a lot of overhead. You shouldn't use it unless there is no other way.

HTH:D
 

Users who are viewing this thread

Back
Top Bottom