Senin, 22 Agustus 2016

Re: [MS_AccessPros] Need to know how many of each digit in a series of unit numbers.

 

Good morning John,

For what I'm trying to do, your solution works fine. I was playing with this function and it was giving me the numbers I want in debug.print but I didn't know how to get them back to the query. I used the value of 3 because each trailer has the unit number on it in 3 places.


Public Function CountDigits(UNITID As String)
    Dim D1 As Integer
    Dim D2 As Integer
    Dim D3 As Integer
    Dim D4 As Integer
    Dim D5 As Integer
    Dim TL0 As Integer
    Dim TL1 As Integer
    Dim TL2 As Integer
    Dim TL3 As Integer
    Dim TL4 As Integer
    Dim TL5 As Integer
    Dim TL6 As Integer
    Dim TL7 As Integer
    Dim TL8 As Integer
    Dim TL9 As Integer

    D1 = Left(UNITID, 1)
    D2 = Mid(UNITID, 2, 1)
    D3 = Mid(UNITID, 3, 1)
    D4 = Mid(UNITID, 4, 1)
    D5 = Mid(UNITID, 5, 1)

    TL0 = 0
    TL1 = 0
    TL2 = 0
    TL3 = 0
    TL4 = 0
    TL5 = 0
    TL6 = 0
    TL7 = 0
    TL8 = 0
    TL9 = 0


    Select Case D1
    Case 0
        TL0 = TL0 + 3
    Case 1
        TL1 = TL1 + 3
    Case 2
        TL2 = TL2 + 3
    Case 3
        TL3 = TL3 + 3
    Case 4
        TL4 = TL4 + 3
    Case 5
        TL5 = TL5 + 3
    Case 6
        TL6 = TL6 + 3
    Case 7
        TL7 = TL7 + 3
    Case 8
        TL8 = TL8 + 3
    Case 9
        TL9 = TL9 + 3
    End Select
   
    Select Case D2
    Case 0
        TL0 = TL0 + 3
    Case 1
        TL1 = TL1 + 3
    Case 2
        TL2 = TL2 + 3
    Case 3
        TL3 = TL3 + 3
    Case 4
        TL4 = TL4 + 3
    Case 5
        TL5 = TL5 + 3
    Case 6
        TL6 = TL6 + 3
    Case 7
        TL7 = TL7 + 3
    Case 8
        TL8 = TL8 + 3
    Case 9
        TL9 = TL9 + 3
    End Select
       
    Select Case D3
    Case 0
        TL0 = TL0 + 3
    Case 1
        TL1 = TL1 + 3
    Case 2
        TL2 = TL2 + 3
    Case 3
        TL3 = TL3 + 3
    Case 4
        TL4 = TL4 + 3
    Case 5
        TL5 = TL5 + 3
    Case 6
        TL6 = TL6 + 3
    Case 7
        TL7 = TL7 + 3
    Case 8
        TL8 = TL8 + 3
    Case 9
        TL9 = TL9 + 3
    End Select
   
    Select Case D4
    Case 0
        TL0 = TL0 + 3
    Case 1
        TL1 = TL1 + 3
    Case 2
        TL2 = TL2 + 3
    Case 3
        TL3 = TL3 + 3
    Case 4
        TL4 = TL4 + 3
    Case 5
        TL5 = TL5 + 3
    Case 6
        TL6 = TL6 + 3
    Case 7
        TL7 = TL7 + 3
    Case 8
        TL8 = TL8 + 3
    Case 9
        TL9 = TL9 + 3
    End Select
   
    Select Case D5
    Case 0
        TL0 = TL0 + 3
    Case 1
        TL1 = TL1 + 3
    Case 2
        TL2 = TL2 + 3
    Case 3
        TL3 = TL3 + 3
    Case 4
        TL4 = TL4 + 3
    Case 5
        TL5 = TL5 + 3
    Case 6
        TL6 = TL6 + 3
    Case 7
        TL7 = TL7 + 3
    Case 8
        TL8 = TL8 + 3
    Case 9
        TL9 = TL9 + 3
    End Select
   
    Debug.Print "TL0 = " & TL0
    Debug.Print "TL1 = " & TL1
    Debug.Print "TL2 = " & TL2
    Debug.Print "TL3 = " & TL3
    Debug.Print "TL4 = " & TL4
    Debug.Print "TL5 = " & TL5
    Debug.Print "TL6 = " & TL6
    Debug.Print "TL7 = " & TL7
    Debug.Print "TL8 = " & TL8
    Debug.Print "TL9 = " & TL9
End Function


Thanks for your help!!


Doyce



---In MS_Access_Professionals@yahoogroups.com, <JohnV@...> wrote :

Doyce-

Sure wish our old pal, A.D. Tejpal, was still around - I'm sure he would have an elegant solution.

Do you want all ten counts returned in one function call, or do you want to call the function 10 times?

Simple call for each digit:

intNine = CountDigit(Me.UnitNumber, 9)

Function CountDigit(lngUnit As Long, intDigit As Integer) As Integer
Dim strUnit As String, intCount As Integer, intI As Integer

    ' Convert the input number to a string
    strUnit = Format(lngUnit, "00000")
    For intI = 1 To 5
        If Mid(strUnit, intI, 1) = intDigit Then intCount = intCount + 1
    Next intI

     ' Return the answer
     CountDigit = intCount
End Function 

John Viescas, author
Effective SQL
SQL Queries for Mere Mortals
Microsoft Office Access 2010 Inside Out
Microsoft Office Access 2007 Inside Out
Building Access Applications

On Aug 22, 2016, at 06:18, winberry.doyce@... [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:

Hello Pros,

At my work, we are engaged in a project where we are replacing the unit numbers on groups of trailers. I need a function that will take the unit number and tell me how many of each digit there are, for example unit number 44095 has 1 zero, 2, fours, 1, five and 1 nine. All the unit numbers have five digits. I'm not sure how to approach this. I know how to extract each digit from the unit number and I assume I should use a select case statement to return the value. But I'm not sure how to put it together and get the totals back from the function. I appreciate all help I receive.

Doyce

__._,_.___

Posted by: winberry.doyce@con-way.com
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (3)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.


.

__,_._,___

Tidak ada komentar:

Poskan Komentar