MOD36 check digit

toqilula

Registered User.
Local time
Today, 11:13
Joined
Dec 13, 2009
Messages
24
Greetings everyone,
Im strugeling for to long now and i just cant figure it out how to implement mod36 checkdigit in my form, i have a 15 character and i need to add another check digit to make 16 characters,
iv cheched something with mod16 and iv heard mod36 is just the same only characters "AtoZ" are added.
any help will be appreciated
thank you in advance
 
I don't know if there's a built in way to do this, but I can write you code to stick in a module:
Code:
Public Function Mod36(Num As Integer) As String
    Dim R As Byte
    
    R = Abs(Num) Mod 36
    
    If R <= 9 Then
        Mod36 = Trim(Str(R))
    Else
        Mod36 = Chr(R + 55)
    End If
End Function

Note that use Abs(Num) means the code will cope with negative numbers, and that you need to use the Trim function with the Str function or you get a leading space.
 
Thank You a lot Kafrin, Ill jump to it right away, I hope i know what to do next when i create a module, if not i think I will bother you more :(
thanx
 
It's always difficult to judge people's knowledge of Access, so I didn't want to go into too much detail of things you might already know. It also depends which version of Access you're using.

In Access 2003 or earlier, go onto Modules and click New. I can't remember in Access 2007 where you add a new module from, but I can check if you need me to. Whatever version you're using, you then just copy my code above (from Public Function to End Function) and paste it into the module underneath anything that's automatically put in. Press Ctrl+S to save the module - I usually name it something like Utilities rather than Module1.

To then use the code, you call it where you need it. So if you already have your string of 15 characters and your code to produce a number from that, then you use:
Code:
Dim MyStr As String, MyNum As Integer
 
MyStr = [your 15 character string]
MyNum = [the number you produce from your string]
MyStr = MyStr & Mod36(MyNum)

Then use MyStr as your 16 character string.

To check an input for the check digit, you go through a similar process:
Code:
Dim TheirStr As String, TheirNum As Integer
 
TheirStr = [the 16 character string that your user has input]
TheirNum = [the number you produce from their string using the same method you used to produce the check digit]
If Right(TheirStr, 1) = Mod36(TheirNum) Then
    [do something if check digit correct]
Else
    [do something else if check digit wrong]
End if

Note that these bits of code go inside your existing Subs.
 
Hello again Kafrin,
It’s nice of you to bring out the people knowledge of MS Access. It’s my mistake I dint precise it.
I am a totally beginner in Access, I like to think that I’m a fast learner but I’m stocked with this database for weeks now :( and mod36 check digit isn’t the only issue, but I thought its better to go one thing at the time, so maybe its better for me to explain once again what I’m trying to accomplish in here, so it will be easier for you to help me. I have a form and in that form I have a Text field (Text1) where I generate a random number like this:
Default Value ="GYM01" & Int((9999999-1000000+1)*Rnd()+1000000)
so i have a 15 characters random string, for example (GYM012547787854)
now, I have another text field (Text2) where I need to add the mod36 check digit to this 15 characters string I generated, meaning in this Text2 I would like to have GYM012547787854 + (check digit, that can be 0 to 9 or A to Z) for example after the "formula or code" it should bring GYM0125477878547 (automatically generated complete 16 digit reference code that I need)
So I’m pretty sure that what you wrote to me can do this, the only thing is for me to know how to put all that code in place :(
I really appreciate your help and thank you for your time.
Sincerely
toqilula
 
OK, first off make sure you've put the function in a new module. Use this one as I've adjusted it slightly:
Code:
Public Function Mod36(Num As Single) As String
    Dim R As Byte
    
    R = Abs(Int(Num)) Mod 36
    
    If R <= 9 Then
        Mod36 = Trim(Str(R))
    Else
        Mod36 = Chr(R + 55)
    End If
End Function

Then when you create your random string for Text1 you can also create the string with the check digit for Text2:
Code:
Dim MyNum as Single
 
Me!Text1[COLOR=black][FONT=Verdana] = "GYM01" & Int((9999999-1000000+1)*Rnd()+1000000)[/FONT][/COLOR]
MyNum = Val(Mid(Text1, 6))
Me!Text2 = Text1 & Mod36(MyNum)

The bit I'm guessing a bit is the MyNum line - basically I'm taking the number that comes after "GYM01" from the Text1 value.
 
Thanx Kafrin for your quick respons, Ill jump to it right away, and tell you how im doing :)
 
Last edited:
Hi again,
I have tried all day long now, either its so easy and i just cant see it, or its really that complicate.
I have gone deeper in to it and could you please look in to it and see whats what i have done:

Code:
my module:
Option Compare Database
Public Function Mod36(Val As String) As String
Dim R As Byte
Dim S() As String
Dim chpool As String
Dim sum As Integer
Dim ctrl As Integer
Dim ctrlNr As Integer
 
chpool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Val = VBA.StrConv(Val, vbUpperCase)
 
sum = 0
 
For i = 1 To 15
sum = sum + (InStr(chpool, Mid(Val, i, 1)) * (16 - i))
Next i
 
ctrl = sum Mod 36
 
Mod36 = Val & Mid(chpool, ctrl, 1)
 
End Function
[\code]
 
and I have my form with:
Text1
Default Value: = "GYM01" & Int((9999999-1000000+1)*Rnd()+1000000)
and Text2
Default Value: = Mod36(<<Val>>)
and Text3
Default Value: =[Text1] & [Text2]
 
but im getting errors
my brain has blocked :(
im desperet now in resolving this issue
 
regards
 
Last edited:
I'm not sure exactly what the problem is, but I would highly recommend you rename the variables Val and Sum. You should never give variables the same names as built-in functions, which both of these are. This will be throwing up at least some of your errors.
 
I'm not sure exactly what the problem is, but I would highly recommend you rename the variables Val and Sum. You should never give variables the same names as built-in functions, which both of these are. This will be throwing up at least some of your errors.
I agree. The variable "sum" is defined in your procudure, but the variable "val" is not. That might be another problem.
 

Users who are viewing this thread

Back
Top Bottom