Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format text in textbox question

Status
Not open for further replies.

GaryWilsonCPA

Technical User
Jul 19, 2000
253
US


The code below works if you click the textbox one time, but if you click twice an amount 500,000.00 becomes 500.00

It works perfectly on the first click, just strange behaviour on second click.

LOANAMT = TEXTBOX NAME.


Any ideas???????

Private Sub LoanAmt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoanAmt.Click

Dim Fmt As String
Fmt = "#,##0.00"
LoanAmt.Text = Format(Val(LoanAmt.Text), Fmt)

End Sub

I have tried this in a button and gotten the same results.



Thanks
 
I have been dealing with this same problem, and after some experimentation I determined it is caused by the Val function. Apparently when a formatted string is passed to the Val function, the function only reads up to the first comma and uses that to return a value. So, a string of "500,000.00" returns 500.00 when run through the Val function.

The only way I have found to get around this is to change the format to General Number before passing a string to the Val function:

Private Sub LoanAmt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoanAmt.Click

Dim Fmt As String
Dim GenNum As String
GenNum = Format(LoanAmt.Text, "General Number")
Fmt = "#,##0.00"
LoanAmt.Text = Format(Val(GenNum), Fmt)

End Sub

Hope this helps.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Correct, Val will only get the value of any digits it finds until it hits a non-numeric character. Using format(x,"General Number") will work, and I beleive double.parse (and all numeric data types .parse) should be able to pull the value with comas and decimal points. Have to try it to be sure though.

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top