I'm having problem regarding a text box max length.

If i set it manually from the properties tab (i set it to 10), output would like

"10000000.0"

What i want is that it would be "1000000000.00". So i tried using this code:

Private Sub txtrate_LostFocus()

If txtrate.Text = vbNullString Then txtrate.Text = FormatNumber(0, 2)
txtrate.Text = FormatNumber(txtrate.Text, 2)

End Sub

But still doesn't work. Any idea?

Your textbox will not show the decimal values because the first 10 values is already used. You need to determine that if there is a decimal value, increase the max length to 13 to allow the "." and the two decimal characters "00".

If MyNumber > 10 Then
txtRate.MaxLength = 13
End If

Why not go for the Masked Edit Control instead of a regular textbox.

That control allows you to set a predefined format, and also allows for validation on entry, instead of after entry.

Still don't work sir.

Design and Run time, but didn't work. It always counts the period as an element or text.

I've tried removing all my codes but its just plain numbers without decimal places.

It should limit the first 5 numbers as its max length then at lost focus, it will automatically adds the ".00" value to the text.


Like 10000 turns to 10,000.00.

@Abe, please post all your code here so I can have a look at what you are trying to achieve.

I replaced my original code (with yours) with this one:

Private Sub txtrate_LostFocus()

txtrate.Text = FormatNumber(txtrate.Text, 2)

End Sub

Change your code to -

Private Sub txtrate_LostFocus()

Dim strLen As String
strLen = FormatNumber(txtrate.Text, 2)

If Len(strLen) > 10 Then txtrate.MaxLength = Len(strLen)
txtrate.Text = strLen
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.