Question : How to restrict user to key-in dot / decimal point only once in a Userform Textbox?

Hi everyone,

   I'm trying to make my own numeric text box in VBA since there's none (or is there?). I'm currently working on my validations and the problem is : I don't want the user to be entering the dot / decimal point (.) more than once in the Userform Textbox.

   As of now, I'm handing the KeyPress event of the textbox, having to loop through each character of the string, then find then replace the dot / decimal point with a blank ("" represents blank, am I right?). I don't understand why my program doesn't remove the latest keyed-in dot. Can anyone help me out? Or is there a much better way to do this? Thanks in advance.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
Dim ctrLast As Integer
        Dim tempSal As String

        tempSal = Me.txtEmpSal.Value

        'loop starting from end of string; replace latest dot /decimal point keyed in by user with blank
        For ctrLast = Len(tempSal) To 1 Step -1
            If Mid(tempSal, ctrLast, 1) = "." Then
                tempSal = Replace(tempSal, Mid(tempSal, ctrLast, 1), "", ctrLast, 1)
                Exit For
            End If
        Next ctrLast

        Me.txtEmpSal.Value = tempSal

Answer : How to restrict user to key-in dot / decimal point only once in a Userform Textbox?

Here is some code:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
   Case Asc("0") To Asc("9")
    Case Asc("-")
       If Instr(1,Me.TextBox1.Text,"-") > 0 Or Me.TextBox1.SelStart > 0 Then
           KeyAscii = 0
       End If
   Case Asc(".")
       If InStr(1, Me.TextBox1.Text, ".") > 0 Then
           KeyAscii = 0
       End If
   Case Else
       KeyAscii = 0
End Select
End Sub

from here:http://www.cpearson.com/excel/TextBox.htm

Random Solutions  
 
programming4us programming4us