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
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
Re: Help with formatting Shorter method
thanks but how in code please
Re: Help with formatting Shorter method
You want to strip out '#', '$' and '%'?
txtname.text = Replace(Replace(Replace(txtname.text, "#", ""), "$", ""), "%", "")
Re: Help with formatting Shorter method
Quote:
Originally Posted by RayComp
thanks but how in code please
VB Code:
Dim strTestData As String
Dim lngIndex As Long
strTestData = "HGy^%jbkljkklh*(^%hfhhj;'"
For lngIndex = Len(strTestData) To 1 Step -1
If (Asc(Mid$(strTestData, lngIndex, 1)) >= 65 And Asc(Mid$(strTestData, lngIndex, 1)) <= 90) _
Or _
(Asc(Mid$(strTestData, lngIndex, 1)) >= 97 And Asc(Mid$(strTestData, lngIndex, 1)) <= 122) Then
' it's OK
Else
strTestData = Replace(strTestData, Mid$(strTestData, lngIndex, 1), "")
End If
Next
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:
Private Function StripChrs(ByVal sText As String) As String
Dim sRemove As String, N As Long
sRemove = "~@#" ' All the chrs you want to strip
For N = 1 To Len(sText)
If InStr(sRemove, Mid$(sText, N, 1)) Then Mid$(sText, N, 1) = Chr(0)
Next N
StripChrs = Replace(sText, Chr(0), vbNullString)
End Function
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:
Dim strTestData As String
Dim lngIndex As Long
strTestData = "HGy^%jbkljkklh*(^%hfhhj;'"
For lngIndex = Len(strTestData) To 1 Step -1
If Asc(Mid$(UCase(strTestData), lngIndex, 1)) >= 65 And Asc(Mid$(UCase(strTestData), lngIndex, 1)) <= 90 Then
' it's OK
Else
strTestData = Replace(strTestData, Mid$(strTestData, lngIndex, 1), "")
End If
Next