Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Look at each individual digit of a numeric field? 1

Status
Not open for further replies.

DahMurf

Programmer
Apr 12, 2003
44
US
I have a 6 digit number (ie: 123456) I’d like to be able to look at one digit at a time.
For example isolate the first digit of (ie: 1) then take an action. Then move to the next digit & take action. I’m thinking this can be done in an array but I can’t quite put it together in my mind. Can anyone help?
Thanks!
 
I would do a for loop with I = 1 to len(<number>)

example

for i = 1 to len(<number>)
digit=mid(<number>,i,1)
<do stuff with the digit>
next
 
You can use the mid function to extract part of a string; for example:
[tt]
ThirdChar = mid(&quot;abcdef&quot;,3,1)
[/tt]
will retrieve from the third character, for one character; ie retrieve the letter c ... and so on.

mid should also work if the first parameter you provide it is numeric, as it automatically converts it to a string representation before extracting the required sub-string.

To iterate through the string, you could use a For ... Next look, as follows:
[tt]
YourString = &quot;ABCDEF&quot;
For i = 1 To Len(YourString)
ThisChar = Mid(YourString, i, 1)
'Do your stuff with Thischar here
Next i
[/tt]

Hope this helps

Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
Paste the following function in a module (new or existing).
Code:
Function ParseValue(varValue)
    Dim intCtr As Integer
    Dim intMax As Integer
    Dim strValue As String
    
    If IsNull(varValue) Or Len(varValue) = 0 Then
        MsgBox &quot;Passed value is empty&quot;, vbExclamation
        DoEvents
        Exit Function
    End If
    
    strValue = CStr(varValue)
    intMax = Len(strValue)
    For intCtr = 1 To intMax
        Debug.Print intCtr & &quot; = &quot; & Mid(strValue, intCtr, 1) & &quot;-&quot;;
        Select Case Mid(strValue, intCtr, 1)
            Case &quot;0&quot;
                Debug.Print &quot;ZERO&quot;
            Case &quot;1&quot;
                Debug.Print &quot;ONE&quot;
            Case &quot;2&quot;
                Debug.Print &quot;TWO&quot;
            Case Else
                Debug.Print &quot;WHATEVER&quot;
        End Select
    Next
End Function
To run:
1. Press Ctrl-G
2. In the Immediate window, type the following and press Enter:
Code:
   ? ParseValue(123456)
Just modify the Case Values (add more for 3 thru 9)...

Jim Kraxberger
Developing Access solutions since 1995
jkraxberger@scp4me.com
 
Excellent! Thank you all!
35.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top