Results 1 to 11 of 11
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    241

    occurance of a specific character in a text box


    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

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    No code needed. The box will yell at you for entering it wrong.
    it knows what a valid decimal is.

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    241
    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?

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Numbers don't have leading zeros.

  5. #5
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    241
    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.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    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

    Code:
    Sub charcntTest()
    Dim x As String: x = "abcdadefan"
    Debug.Print countChar(x, "a")
    End Sub
    3

    Code:
    Sub charcntTest()
    Dim x As String: x = "ab.cda..de.fan"
    Debug.Print countChar(x, ".")
    End Sub
    4

    Code:
    Sub charcntTest()
    Dim x As String: x = "ab.cda..de.fan"
    Debug.Print countChar(x, " ")
    End Sub
    0

  7. #7
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by Khalil Handal View Post
    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?
    You could try:

    Code:
    If fnValidation(Me.ActiveControl) = False Then
        Cancel = True
    End If
    use the code above in the BeforeUpdate event of the text box and put the following function in the forms code module

    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
    EDIT:
    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

  8. #8
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    241
    Thank you all.

    Worked well.

  9. #9
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by Khalil Handal View Post
    Thank you all.

    Worked well.
    What did you use.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  10. #10
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    241
    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

  11. #11
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by Khalil Handal View Post
    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
    You're welcome. Glad you have a solution
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 15
    Last Post: 02-17-2019, 06:19 PM
  2. Replies: 18
    Last Post: 10-11-2017, 03:07 PM
  3. Replies: 44
    Last Post: 04-26-2017, 01:53 PM
  4. Replies: 5
    Last Post: 05-15-2013, 03:18 PM
  5. Replies: 3
    Last Post: 12-22-2012, 05:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums