Results 1 to 7 of 7

Thread: Help with formatting Shorter method

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    43

    Help with formatting Shorter method

    Thanks to all who assisted with my previous request. Here is another one

    Need to input weight and volume as 12kgs and Volume as 12ltr in database.
    Need also later to do calculations with the numbers therefore need to strip out alpha characters. (do not want to input twice)

    Any one with a better or shorter version as the one below that I use currently to strip invalid characters from customer name as it will take up a lot of programming lines to strip out all alpha characters from a string

    'Strip out invalid characters in the Customer name...
    If InStr(txtname.text, "#") > 0 Then
    Mid$(txtname.text, InStr(txtname.text, "#"), 1) = Chr$(32)
    ElseIf InStr(txtname.text, "$") > 0 Then
    Mid$(txtname.text, InStr(txtname.text, "$"), 1) = Chr$(32)
    ElseIf InStr(txtname.text, "%") > 0 Then
    Mid$(txtname.text, InStr(txtname.text, "%"), 1) = Chr$(32)
    End If

    End If

    Raycomp

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Help with formatting Shorter method

    you could loop through the string and check the ASCII val of each character, for example, a-z is 97-122 and A-Z is 65-90

    use Asc() to get the val of each letter, if its not between^ those vals then remove or do whatever
    Chris

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    43

    Re: Help with formatting Shorter method

    thanks but how in code please

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,692

    Re: Help with formatting Shorter method

    You want to strip out '#', '$' and '%'?

    txtname.text = Replace(Replace(Replace(txtname.text, "#", ""), "$", ""), "%", "")

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Re: Help with formatting Shorter method

    Quote Originally Posted by RayComp
    thanks but how in code please
    VB Code:
    1. Dim strTestData As String
    2. Dim lngIndex As Long
    3.  
    4. strTestData = "HGy^%jbkljkklh*(^%hfhhj;'"
    5. For lngIndex = Len(strTestData) To 1 Step -1
    6.     If (Asc(Mid$(strTestData, lngIndex, 1)) >= 65 And Asc(Mid$(strTestData, lngIndex, 1)) <= 90) _
    7.         Or _
    8.        (Asc(Mid$(strTestData, lngIndex, 1)) >= 97 And Asc(Mid$(strTestData, lngIndex, 1)) <= 122) Then
    9.        ' it's OK
    10.     Else
    11.         strTestData = Replace(strTestData, Mid$(strTestData, lngIndex, 1), "")
    12.     End If
    13. Next

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Help with formatting Shorter method

    If there's only certain characters you wish to exclude then you could do something like this:

    VB Code:
    1. Private Function StripChrs(ByVal sText As String) As String
    2.     Dim sRemove As String, N As Long
    3.     sRemove = "~@#" ' All the chrs you want to strip
    4.      
    5.     For N = 1 To Len(sText)
    6.         If InStr(sRemove, Mid$(sText, N, 1)) Then Mid$(sText, N, 1) = Chr(0)
    7.     Next N
    8.     StripChrs = Replace(sText, Chr(0), vbNullString)
    9. End Function

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Re: Help with formatting Shorter method

    If you are going to use the method I posted you might want to change it to

    VB Code:
    1. Dim strTestData As String
    2. Dim lngIndex As Long
    3.  
    4. strTestData = "HGy^%jbkljkklh*(^%hfhhj;'"
    5. For lngIndex = Len(strTestData) To 1 Step -1
    6.     If Asc(Mid$(UCase(strTestData), lngIndex, 1)) >= 65 And Asc(Mid$(UCase(strTestData), lngIndex, 1)) <= 90 Then
    7.        ' it's OK
    8.     Else
    9.         strTestData = Replace(strTestData, Mid$(strTestData, lngIndex, 1), "")
    10.     End If
    11. Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width