If string doesn't contain a number seeking a slicker way of implementing!

peskywinnets

Registered User.
Local time
Today, 20:16
Joined
Feb 4, 2014
Messages
578
I need to test a field to establish if there's a number present withing the string & if there is capitalize all characters in the string, but if not then to make the field a Null. There must be a slicker way of doing the checking part than this...

Code:
 If (InStr(MyString, "0") > 0) Or (InStr(MyString, "1") > 0) Or (InStr(MyString, "2") > 0) Or (InStr(MyString, "3") > 0) Or (InStr(MyString, "4") > 0) Or (InStr(MyString, "5")) Or (InStr(MyString, "6")) Or (InStr(MyString, "7") > 0) Or (InStr(MyString, "8") > 0) Or (InStr(MyString, "9") > 0) Then

top tips warmly received :-)
 
Last edited:
Is the number anywhere in the string or a specific/fixed location ?
 
(btw, I've edited my original post slightly to give a fuller picture)

Is the number anywhere in the string or a specific/fixed location ?


It's buyer postcode (zip) information ....many parts of Ireland don't use (or have) postcodes, so customers enter all manner of text for the postcode field when checking out...

NA
--
.
------------
None


....etc, etc

So I'm looking to clean up this data. If there's proper postcode information there's be a number in there somewhere....if not throw the data away.

My code posted in my opening post works, but I'm just curious if there's a slicker way!
 
Maybe:

Code:
If myString Like "*[0-9]*" Then
    myString = UCase(myString)
End If
 
This probably works too:

Code:
If myString Like "*#*" Then
    myString = UCase(myString)
End If
 
I agree with stopher.
Here is same code as a function if you need it

Code:
'---------------------------------------------------------------------------------------
' Procedure : containsDigit
' Author    : mellon
' Date      : 09-Nov-2016
' Purpose   :This routine will determine if a string contains at least 1 digit.
'
' Input:  strIn a string
' Return: Boolean   True if a digit within strIn
'                              False if no digit within strIn
'---------------------------------------------------------------------------------------
'
Function containsDigit(strIn As String) As Boolean

10       On Error GoTo containsDigit_Error

20        If strIn Like "*[0-9]*" Then
30            containsDigit = True
40        Else
50            containsDigit = False
60        End If

70       On Error GoTo 0
80       Exit Function

containsDigit_Error:

90        MsgBox "Error " & Err.Number & " in line " & Erl & " (" & Err.Description & ") in procedure containsDigit of Module JED_CompanyEdit_Module_GOLD_Lib"
End Function
 
Last edited:
Thanks chaps...I knew there must have been a better way (this is all part of my learning experience :-))
 
Last edited:

Users who are viewing this thread

Back
Top Bottom