Hi,
How to check that the value entered into a text box has only one dot (.) ?
Example:
246 and 234.98 are 2 correct inputs.
but 248..3 is not a correct input.
Sincerely yours
Khalil
Hi,
How to check that the value entered into a text box has only one dot (.) ?
Example:
246 and 234.98 are 2 correct inputs.
but 248..3 is not a correct input.
Sincerely yours
Khalil
No code needed. The box will yell at you for entering it wrong.
it knows what a valid decimal is.
Hi,
Thank you for the reply. I should have mentioned that the Data Type for the field is Text and not Decimal because in some cases I need to show leading zeros if exists.
I might have something like 020.78
Any suggestions?
Numbers don't have leading zeros.
Hi,
Let me ask the question in a different way.
How to count the number of a specific character in a string?
Example: count number of (a) in abdcasdb
the answer should be 2.
Are you looking for contiguous (side by side) dots OR just more than 1 dot in the control value?
' ----------------------------------------------------------------
' Procedure Name: countChar
' Purpose: Count the number of certain char in the input string
Code:' Procedure Kind: Function ' Procedure Access: Public ' Parameter instring (String): ' Parameter inchar (String): ' Return Type: Integer ' Author: Jack ' Date: 29-Jun-18 ' ---------------------------------------------------------------- Function countChar(inString As String, inchar As String) As Integer Dim i As Integer, cnt As Integer 10 For i = 1 To Len(inString) 20 If Mid(inString, i, 1) = inchar Then 30 cnt = cnt + 1 40 End If 50 Next i 60 countChar = cnt End Function
Test routines
3Code:Sub charcntTest() Dim x As String: x = "abcdadefan" Debug.Print countChar(x, "a") End Sub
4Code:Sub charcntTest() Dim x As String: x = "ab.cda..de.fan" Debug.Print countChar(x, ".") End Sub
0Code:Sub charcntTest() Dim x As String: x = "ab.cda..de.fan" Debug.Print countChar(x, " ") End Sub
You could try:
use the code above in the BeforeUpdate event of the text box and put the following function in the forms code moduleCode:If fnValidation(Me.ActiveControl) = False Then Cancel = True End If
EDIT:Code:Private Function fnValidation(txt As String) As Boolean If InStr(1, txt, ".") > 0 And InStr(InStr(1, txt, ".") + 1, txt, ".") > 0 Then MsgBox "Only one full stop (.) permitted" fnValidation = False Exit Function End If If Len(txt) - InStr(1, txt, ".") > 2 Then 'And InStr(txt, ".") > 0 Then MsgBox "only 2 decimal places allowed" fnValidation = False Exit Function Else fnValidation = True End If End Function
Sorry, I hadn't noticed that Orange had already answered
If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
Bob Fitzpatrick
Thank you all.
Worked well.
I tried Bob Fitz first and it worked well.
I will try Orange to see if will also work.
I was looking not only for side by side dots, it could be anywere
Thank you both
If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
Bob Fitzpatrick