Solved

Determine if a string is all numerals

Posted on 2010-09-03
15
522 Views
Last Modified: 2013-11-27
Hi,

I am extracting an string of length 8 from a filename. I want to convert the string to a date so I need to check if it is composed of all numeric digits before I try to convert it or I will cause an error.

So basically I need to determine if the string is composed of all numeric digits or if there are some text digits instead.

i.e.

"12345678"                       this is good
"1234r567"                       any combination with any amount letters is bad

I'll probably have to loop through the components and check to see if they are numeric.

I will use this in existing code to decide whether to process the file or delete it.

Thanks,
scurvylion

0
Comment
Question by:scurvylion
  • 5
  • 4
  • 2
  • +2
15 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33598147

you can use the isnumeric() function

if isnumeric(string) then

 else
end if
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 33598166
Use the IsNumeric() Function, so something like this:

Dim strCheck as String
strCheck = "12345678"

If IsNumeric(strCheck) Then
    'Do what you want here, it is a valid number
Else
    'Not all numbers, do what you want here
End If
0
 
LVL 15

Accepted Solution

by:
dbbishop earned 250 total points
ID: 33598230
The problem with using IsNumeric is that 1234.43 is numeric and -342 is numeric, although neither are all numbers. Try the code below.
Declare Public Function NumCheck(Value as Variant) As Boolean

Dim I as Integer

NumCheck = 1

For I = 1 To Len(Value)

  If Mid(Value, I, 1) < '0' Or Mid(Value, I, 1) > '9' Then 

    NumCheck = 0

    Exit Function

Next I



Then execute by using:

 If NumCheck(myValue) = 0 Then ' not all numeric

Open in new window

0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 33598274
dbbishop,
While I agree with your point, I think that just ends up opening another can of worms.

He says this value will then be converted to a date, so should (s)he be checking for valid numbers that can also be converted to a valid date, and within what time frame of date allowed (only this week? last week? within a year or two), etc. etc.

I think it all boils down to how much this user needs out of this and what his/her full requirement is.

- Anthony
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33598275
i agree with dbbishop, but for the OP's requirement the isNumeric() function will do since
the string will be coming from ....

<I am extracting an string of length 8 from a filename.>

and i doubt that you will find a "-" sign at the start of the string and a dot(.) in the middle of the string...
0
 
LVL 15

Expert Comment

by:dbbishop
ID: 33598371
However, if I recall correctly, 123e34 is also numeric using the IsNumeric() function.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33598405
<123e34 is also numeric using the IsNumeric() function.>

correct, but not when you pass is as a String..


IsNumeric("123e34")
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33598411
>>However, if I recall correctly, 123e34 is also numeric using the IsNumeric() function.Indeed, it will :)
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33598439
i stand corrected...
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 250 total points
ID: 33598441
If your real concern is to validate that a string can convert to a date, then assuming your string is yyyymmdd...If IsDate(Left(MyStr, 4) & "-" & Mid(MyStr, 5, 2) & "-" & Right(MyStr, 2)) Then   'valid dateElse    'invalidEnd IfIf the string is, say, mmddyyyy....If IsDate(Right(MyStr, 4) & "-" & Left(MyStr, 2) & "-" & Mid(MyStr, 3, 2)) Then   'valid dateElse    'invalidEnd If
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33598458
In that code I just posted, if the expression passed to IsDate cannot be converted to a Date value, then the function simply returns False.IsDate will work in queries, too.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33598531
And, of course, if the string is ddmmyyyy....If IsDate(Right(MyStr, 4) & "-" & Mid(MyStr, 3, 2) & "-" & Left(MyStr, 2)) Then  'valid dateElse   'invalidEnd If
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33598594
And lastly, ignoring dates for a moment, if all you really want to do is ensure that a given string contains only digits...If Not MyString Like "*[!0-9]*" Then    'code for only digitsElse    'code for at least one non-digitEnd IfThat will work in queries too.
0
 

Author Comment

by:scurvylion
ID: 33598736
Thanks for all of your input guys. First of all, let me apologize for not being as clear as I could of been. I shouldn't have gotten lazy when writing the question up.

MatthewsPatrick hit the nail on the head with the solution for yyyymmdd but since I asked for a solution to whether the string was numeric or not, dbbishop also provided a valid solution. Therefore, I am going to split the points between you two guys.

I appreciate your timely advice.
0
 

Author Closing Comment

by:scurvylion
ID: 33598753
Thanks again guys.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now