replace characters in a string

  • Thread starter Thread starter kenny.love
  • Start date Start date
K

kenny.love

I am currently scanning barcodes into a form. When they are scanned in,
they are in this format:

&d099430500683270

Firstly I would like to know how to strip off the first 2 characters ie
&d
Secondly, I would like to use the last two digits (in this case 70) to
come up with a check digit which would then replace the last two
digits.

In this case, the check digit for 70 is A. Therefore after following
the two conditions above, the barcode would now read:

0994305006832A

The rule for calculating check digits from the last two digits is as
follows:

60 to 69 correspond to check digits 0 to 9
70 to 95 correspond to check digits A to Z 96 corresponds to *

I really don't know where to start on this on. Any help would be
amazing.

Thanks.
 
Something like the following:

Function RecodeBarcode(BarcodeIn As String) As String

Dim intLast2Digits As Integer
Dim strBarcode As String
Dim strCheckDigit As String

' Remove the first 2 characters from the barcode
strBarcode = Mid$(BarcodeIn, 3)

' Save the last 2 digits as a number
intLast2Digits = CInt(Right$(BarcodeIn, 2))

' Remove the last 2 digits from the barcode
strBarcode = Left$(strBarcode, Len(strBarcode) - 2)

' Determine the Check Digit
Select Case intLast2Digits
Case 60 To 69
strCheckDigit = CStr(intLast2Digits Mod 10)
Case 70 To 95
' take advantage of the fact that A is 65 in Ascii,
' B is 66, and so on
strCheckDigit = Chr$(intLast2Digits - 5)
Case 96
strCheckDigit = "*"
Case Else
' What do you want to do if the last 2 digits are
' anything else?
End Select

RecodeBarcode = strBarcode & strCheckDigit

End Function
 
Kenny,
Using your example...
First strip the &d
BarCode = Mid(Barcode,3) yields 099430500683270
Second, determine the last two digits
Last2 = Right(Barcode,2) yields 70
Third - Calculate your CheckDigit/s... (I'll leave that to you)
ex. ChkDigit = "A"
Fourth - Strip the last 2 characters
BarCode = Left(Barcode, Len(Barcode)-2) yields 0994305006832
Fifth - Add the ChkDigit you calculated
BarCode = BarCode & ChkDigit

Check my syntax... but that should do it.
 
Back
Top