On more Q! Why can't I calculate with decimals???

  • Thread starter Guus van Waardenburg
  • Start date
G

Guus van Waardenburg

Hi all,

Another problem I can't solve singlehandedly...

When I fill in digits in textboxes, want to calculate them and put the
outcome in a label the outcome always round up to an integer digit...
Even when I set the format of the txtbxs and the label so it wil show
decimals, the decimals always stay at 0.

Example:
txtbox1 = 13,23
txtbox2 = 1,00
lable1 should be 14,23 offcourse but
label1 = 14,00

Can anyone help me?
 
G

Guus van Waardenburg

Here is an example of my code, lightly changed, since it contains some dutch...

Dim i As Currency
i = textbox1.Value * textbox2.Value \ 1000
If textbox1.Value = True Then
If textbox1.Value = False Then
label1.Caption = False
Else
label1.Caption = i
End If
Else
label1.Caption = False
End If
 
E

Er

Guus van Waardenburg said:
Here is an example of my code, lightly changed, since it contains some
dutch...

Dim i As Currency
i = textbox1.Value * textbox2.Value \ 1000

I am no expert, but shouldn't this read:

i = textbox1.Value * textbox2.Value / 1000


The '\' rounds off the results to a whole number, with MOD giving the
remainder.

Erwin
 
H

Helmut Weber

Hi Guus,

apart from datatype conversion issues,
this will hardly make sense:
If textbox1.Value = True Then
If textbox1.Value = False Then

Depending on regional settings
on how to display numbers,
you have to be very careful,
when converting strings to doubles or singles.

Whereas the surface of windows
may use the international settings,
the VBA-code doesn't.

When you convert a string to a single,
the VBA-code expects a string like "1000.14".
So you have to remove possible 1000s seperators,
and to make sure, that the decimal seperator is a period (.)

If I had to create a userform, which should accept decimal fractions,
i would use _two_ textboxes. One for the whole number,
one for the fractions:
like [xxxxx],[xxxxxx] on the european continent.

And as input I would allow nothing but digits.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Guus van Waardenburg

Thank you Guys,

Ik know I have to work on the functionality of my code! It's my first one.

I flipped the slash and indeed it works out fine.

The program works fine now. Now for stripping the code to functionality...

Best regards.

Helmut Weber said:
Hi Guus,

apart from datatype conversion issues,
this will hardly make sense:
If textbox1.Value = True Then
If textbox1.Value = False Then

Depending on regional settings
on how to display numbers,
you have to be very careful,
when converting strings to doubles or singles.

Whereas the surface of windows
may use the international settings,
the VBA-code doesn't.

When you convert a string to a single,
the VBA-code expects a string like "1000.14".
So you have to remove possible 1000s seperators,
and to make sure, that the decimal seperator is a period (.)

If I had to create a userform, which should accept decimal fractions,
i would use _two_ textboxes. One for the whole number,
one for the fractions:
like [xxxxx],[xxxxxx] on the european continent.

And as input I would allow nothing but digits.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top