Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

VB: label

I have the following statement:

lblNetCharge.Caption = (DAYCHARGE * NumberOfDays) + (MILESDRIVENCHARGE * MilesDriven)

NumberOfDays = Val(txtNumberOfDays.Text)

MilesDriven = Val(lblMilesDriven)

the 1st part of the calculation worked fine but the other one gave me a "0"
But when i replace (MILESDRIVENCHARGE * MilesDriven)   by   (MILESDRIVENCHARGE * Val(lblMilesDriven))
it works fine.
how can i use a variable for the label value?
  • 2
1 Solution
show all of the relevant code, as it appears, from what you have shown, that you MIGHT have it written like this:

lblNetCharge.Caption = (DAYCHARGE * NumberOfDays) + (MILESDRIVENCHARGE * MilesDriven)

MilesDriven = Val(lblMilesDriven)
  so that MilesDrive is UNCALCULATED(hence value = 0) when you try to use it.

change to:

MilesDriven = Val(lblMilesDriven)

lblNetCharge.Caption = (DAYCHARGE * NumberOfDays) + (MILESDRIVENCHARGE * MilesDriven)

and it SHOULD be fine.

One thing that simplifies programming is to use variables to hold your values.  It becomes easier to keep track of, and you are less likely to have errors.

dim sgNetCharge as single

'set values to DAYCHARGE, NumberOfDays, ect

sgNetCharge = (DAYCHARGE * NumberOfDays) + (MILESDRIVENCHARGE * MilesDriven)

lblNetCharge.Caption = sgNetCharge
sam17Author Commented:
thnx a lot

what ur saying is that we can use the val() statement for a texbox only and not for the label..?
and the lbl.caption stt for the label?
The Val statement can be used for a label, the point I make regarding procedure in coding is that it is much easier to write programs that are a step by step process, as opposed to attempting to cram as many steps as possible in each line of code.  You will be a much more succesful (and sane) programmer if you attempt to keep things simple, and organized.  Debuging is much easier since each line of code is only providing one new step.  

I hope this was helpful.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now