Mutlipy every 2nd digit by 2

jomuir

Registered User.
Local time
Today, 16:38
Joined
Feb 13, 2007
Messages
154
I have a field “Ref Number” which is number 11digits long. I want to double the value of ever second digit (starting with the first digit)

So:-
8 7 2 5 6 0 0 2 1 2 2 (There is no gaps in actual data)
Would Be:-
16 7 4 5 12 0 0 2 1 2 4

Thank you
 
would the result after doubling also contain 11 digits and if so how would you strip out numbers to get down to 11 digits?
 
No it is the first part of my calculation

No, I would then be looking to add the individual digits.

First Step:-
So:-
8 7 2 5 6 0 0 2 1 2 2 (There is no gaps in actual data)
Would Be:-
16 7 4 5 12 0 0 2 1 2 4

Second Step:-
1+6+7+4+5+1+2+0+0+2+1+2+4 = 35

Then subtract this (35) from the next highest number ending in a 0 (tens complement) i.e. 40 - 35 = 5

So the number I am looking for here is 5

IF the number in the second step ends in a zero then the number I am looking for is zero 0
 
Is this a form of checkdigit if so Modulus 11 is your best bet
 
dim strWholeNumber As String
dim strPartNumber As String
dim intPartNumber As Integer
dim intPos As Integer
dim strNewString As String
dim intOldNumSum As Integer
dim intNewNumSum As Integer

strWholeNumber = "87256002122"
strNewString = ""
intOldNumSum = 0
intNewNumSum = 0

for intPos = 1 to Len(strWholeNumber)
strPartNumber = mid(strWholeNumber,intPos,1)
intPartNumber = cint(strPartNumber)
intOldNumSum = intOldNumSum + intPartNumber

If CInt(intPos/2) = intPos/2 then 'Number is even
strNewString = strNewString & cstr(intPartNumber * 2)
intNewNumSum = intNewNumSum + (intPartNumber * 2)
Else
strNewString = strNewString & strPartNumber
intNewNumSum = intNewNumSum + intPartNumber
End If
next intPos
 
So should I replace:-

strWholeNumber = "87256002122"
with
strWholeNumber = "RefNumber"

Where do I put this code? I presumed (sorry new to this all) I am on the form I have created to collect this information and right click on the field I want the output, properties/event/after update/Code Builder and it opens a VB section.

I did have a choice of Expression Builder/Macro Builder and Code Builder – code builder was the only one that made sense……..but no data appears in this field.

Sorry again – this is my proper form.


Thank you very much for all the code you provided...VERY quickly :)
 
you would put this in the OnClick event of a button and if RefNumber was the name of a field on the form then you would use strWholeNumber = me.RefNumber to reference the form object ;)
 
Jomuir
You sample is flawed, 87256002122 should return 1674512002224

Stallyon
Your logic is flawed, Jomuir wanted to double the Odd digits not the Even ones and then add up the individual digits of the new number not the middle steps.
Second Step:-
1+6+7+4+5+1+2+0+0+2+1+2+4 = 35
not
16+7+4+5+12+0+0+2+1+2+4 = 53

It was a along the right lines though :)


Jomuir
do you actualy want the final number or just to know if it is 0/non 0 ?

Code:
Function CheckStr(strWholeNumber)
Dim strPartNumber As String
Dim intPartNumber As Integer
Dim intPos As Integer
Dim strNewString As String
Dim intOldNumSum As Integer
Dim intCheckSum As Integer

For intPos = 1 To Len(strWholeNumber)
    strPartNumber = Mid(strWholeNumber, intPos, 1)
    intPartNumber = CInt(strPartNumber)
  
    If CInt(intPos / 2) = intPos / 2 Then 'Number is even
        strNewString = strNewString & strPartNumber
    Else
        strNewString = strNewString & CStr(intPartNumber * 2)
    End If
Next intPos
For intPos = 1 To Len(strNewString)
    strPartNumber = Mid(strNewString, intPos, 1)
    intPartNumber = CInt(strPartNumber)
    intCheckSum = intCheckSum + intPartNumber
Next intPos
intPos = Abs((intCheckSum Mod 10) - 10)
If intPos = 10 Then
    CheckStr = 0
Else
    CheckStr = intPos
End If
End Function

Sub asfdga()
Dim strWholeNumber As String
strWholeNumber = "87256002122"
Debug.Print CheckStr(strWholeNumber)
End Sub

I have done it as a fuction so you can just call it and do what you want with the return value. (I have printed to the debug window, use ctr-G to call it up)


HTH

Peter
 
Stallyon
Your logic is flawed, Jomuir wanted to double the Odd digits not the Even ones and then add up the individual digits of the new number not the middle steps.

I had realised that Bat17 but wanted to give the idea of how it may be done and let him work the rest out for himself rather than do it all and for jomiur not to learn anything
 

Users who are viewing this thread

Back
Top Bottom