Link to home
Start Free TrialLog in
Avatar of abidsml
abidsml

asked on

Text Box

I am using the dataformat of a text box as currency, but when i display the number it is not showing that just a number with plenty of decimal places, how can i correct this please.

thanks
ASKER CERTIFIED SOLUTION
Avatar of Foyal
Foyal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Éric Moreau
The DataFormat property can only be used if the control is bound (ie DataSource and DataField also set).

For numeric input, I always use this class:

Create a new project.

To this project add a class (that you will name clsNumeric) and copy this code to it:

Option Explicit

Public WithEvents gctlTextBox As TextBox

Private mblnNegatif As Boolean
Private mbytNumericLongueur As Byte
Private mbytNumericDecimal As Byte

'Convertit en majuscule au besoin
Private Sub gctlTextBox_KeyPress(KeyAscii As Integer)
    Call ValiderChampNombre(KeyAscii)
End Sub

'S'assure que le format est respecté
Private Sub gctlTextBox_Validate(Cancel As Boolean)
Dim blnNegatif As Boolean
Dim intPosSepDec As Integer

    '===============================================================
    'Recherche la position du séparateur de décimales
    '===============================================================
    intPosSepDec = InStr(gctlTextBox.Text, ".")

    '===============================================================
    'Détecte le symbole négatif
    '===============================================================
    blnNegatif = InStr(gctlTextBox.Text, "-")

    'Si pas de séparateur de décimales
    If intPosSepDec = 0 Then
        If Len(gctlTextBox.Text) - IIf(blnNegatif, 1, 0) > mbytNumericLongueur Then GoTo Invalide
    'Si séparateur de décimales
    Else
        If Len(gctlTextBox.Text) - intPosSepDec > mbytNumericDecimal Then GoTo Invalide
        If Len(gctlTextBox.Text) - Len(Mid$(gctlTextBox.Text, intPosSepDec)) - IIf(blnNegatif, 1, 0) > mbytNumericLongueur Then GoTo Invalide
    End If

    Exit Sub

Invalide:
    Cancel = True
    Beep
End Sub

'Initialise la longueur d'un champ numérique
Public Sub InitChampNombre(ByVal pbytLong As Byte, _
                           ByVal pbytDec As Byte, _
                           Optional ByVal pblnNegatif As Boolean = False)
    mbytNumericLongueur = pbytLong
    mbytNumericDecimal = pbytDec
    mblnNegatif = pblnNegatif
End Sub

Private Sub ValiderChampNombre(ByRef KeyAscii As Integer)
Dim blnNegatif As Boolean
Dim intPosSepDec As Integer

    '===============================================================
    'Si BackSpace = OK
    '===============================================================
    If KeyAscii = 8 Then Exit Sub

    '===============================================================
    'Valide si le caractère est légal (séparateur de décimales, -, 0 à 9)
    '===============================================================
    If Not (Chr$(KeyAscii) = "." Or _
       Chr$(KeyAscii) = "-" Or _
       IsNumeric(Chr$(KeyAscii)) Or _
       KeyAscii = 0) _
    Then GoTo Invalide
     
    '===============================================================
    'Si c'est le signe négatif
    '===============================================================
    If Chr$(KeyAscii) = "-" Then
        'Si ce n'est pas permis
        If Not mblnNegatif Then GoTo Invalide
         
        'Si ce n'est pas au début
        If gctlTextBox.SelStart > 0 Then GoTo Invalide
        'If Len(gctlTextBox.Text) <> 0 Then GoTo Invalide
    End If
     
    '===============================================================
    'Recherche la position du séparateur de décimales
    '===============================================================
    intPosSepDec = InStr(gctlTextBox.Text, ".")
     
    '===============================================================
    'Détecte le symbole négatif
    '===============================================================
    blnNegatif = InStr(gctlTextBox.Text, "-")
     
    '===============================================================
    'Si séparateur de décimales
    '===============================================================
    If Chr$(KeyAscii) = "." Then
        'Si ce n'est pas permis
        If mbytNumericDecimal = 0 Then GoTo Invalide
     
        'Si il y a déjà un séparateur de décimales
        If intPosSepDec <> 0 Then GoTo Invalide
         
        'Ne pas dépasser le nombre de décimales
        If Len(gctlTextBox.Text) - gctlTextBox.SelStart > mbytNumericDecimal Then GoTo Invalide
     
        'Ne pas dépasser le nombre d'entiers
        If gctlTextBox.SelStart - IIf(blnNegatif, 1, 0) > mbytNumericLongueur Then GoTo Invalide
    End If

    '===============================================================
    ' Le reste
    '===============================================================
    'Si pas de séparateur de décimales
    If intPosSepDec = 0 Then
        If Len(gctlTextBox.Text) - gctlTextBox.SelLength - IIf(blnNegatif, 1, 0) >= mbytNumericLongueur Then
            If Not (Chr$(KeyAscii) = "." Or Chr$(KeyAscii) = "-") Then GoTo Invalide
        End If
    'Si séparateur de décimales
    Else
        If intPosSepDec > 0 And gctlTextBox.SelStart >= intPosSepDec Then
            If Len(gctlTextBox.Text) - intPosSepDec >= mbytNumericDecimal Then GoTo Invalide
        Else
            If Len(gctlTextBox.Text) - Len(Mid$(gctlTextBox.Text, intPosSepDec)) - IIf(blnNegatif, 1, 0) >= mbytNumericLongueur Then
                If Not (Chr$(KeyAscii) = ".") Then GoTo Invalide
            End If
        End If
    End If
     
    Exit Sub
     
Invalide:
    KeyAscii = 0
    Beep
End Sub


Then, on the form, place a textbox (without changing any properties) and copy this code:

Option Explicit

Private MyNumericTextBox As clsNumeric

Private Sub Form_Load()
    Text1.Text = "" 
    Set MyNumericTextBox = New clsNumeric
    With MyNumericTextBox
        Set .gctlTextBox = Text1
        .InitChampNombre 5, 2
    End With
End Sub
Avatar of abidsml
abidsml

ASKER

thanks