Java code to vba

toqilula

Registered User.
Local time
Today, 11:31
Joined
Dec 13, 2009
Messages
24
Hello VBA lovers
039.gif


I Have wrote a simple code in java and i would very much like to be able to use it in ms access.
Could someone translate it in vba please.
I know it will be easy for most of the ppeople programming in VBA, cose it has only two loopes and some string variables.
So I would really much appreciate if you can help me.
here is the code writen i java:
Code:
[LEFT][COLOR=blue]Public[/COLOR] class mod36 { 
[COLOR=blue]Public[/COLOR] static void main ([COLOR=blue]String[/COLOR] [] arg){ 
[COLOR=blue]String[/COLOR] _sIn = "GYM014650000005"; 
[COLOR=blue]String[/COLOR] sCharPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
String[] sArrayIn = [COLOR=blue]New[/COLOR] [COLOR=blue]String[/COLOR] [15]; 
char c; 
[COLOR=blue]For[/COLOR] (int i = 0; i < 15; i++){ 
    c = _sIn.charAt(i); 
    sArrayIn[i] = Character.toString(c); 
} 
    int iSum = 0; 
    [COLOR=blue]For[/COLOR] (int iCount = 0; iCount < 15; iCount++) { 
        iSum = iSum + sCharPool.indexOf(sArrayIn[iCount].toUpperCase()) * (16 - iCount); 
} 
        int iCtrl = iSum % 36; 
        System.out.println ("rez: " + _sIn + sCharPool.substring(iCtrl, iCtrl + 1)); 
 }} [/LEFT]
 
Hello VBA lovers
039.gif


I Have wrote a simple code in java and i would very much like to be able to use it in ms access.
Could someone translate it in vba please.
I know it will be easy for most of the ppeople programming in VBA, cose it has only two loopes and some string variables.
So I would really much appreciate if you can help me.
here is the code writen i java:
Code:
[LEFT][COLOR=blue]Public[/COLOR] class mod36 { 
[COLOR=blue]Public[/COLOR] static void main ([COLOR=blue]String[/COLOR] [] arg){ 
[COLOR=blue]String[/COLOR] _sIn = "GYM014650000005"; 
[COLOR=blue]String[/COLOR] sCharPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
String[] sArrayIn = [COLOR=blue]New[/COLOR] [COLOR=blue]String[/COLOR] [15]; 
char c; 
[COLOR=blue]For[/COLOR] (int i = 0; i < 15; i++){ 
  c = _sIn.charAt(i); 
  sArrayIn[i] = Character.toString(c); 
} 
  int iSum = 0; 
  [COLOR=blue]For[/COLOR] (int iCount = 0; iCount < 15; iCount++) { 
      iSum = iSum + sCharPool.indexOf(sArrayIn[iCount].toUpperCase()) * (16 - iCount); 
} 
      int iCtrl = iSum % 36; 
      System.out.println ("rez: " + _sIn + sCharPool.substring(iCtrl, iCtrl + 1)); 
}} [/LEFT]

I could be a little off, it's been a few years since I've done any java, but here goes nothing.

Some of your variable names need to be changed as they are either VBA functions, or contain characters not allowed in VBA.
Code:
Function mod36()
Dim ssIn As String: ssIn = "GYM014650000005"
Dim sCharPool As String: sCharPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim sArrayIn(1 To 15) As String
Dim c As String
Dim i As Long, iCount As Long
For i = 1 To 15
    c = Mid$(ssIn, i, 1)
    sArrayIn(i) = c
    Dim iSum As Long: iSum = 0
 
    For iCount = 1 To 15
        iSum = iSum + (InStr(1, sCharPool, UCase(sArrayIn(iCount))) * (16 - (iCount - 1)))
 
        Dim iCtrl As Long: iCtrl = (iSum Mod 36)
        Debug.Print "rez: " & ssIn & Mid$(sCharPool, iCtrl, iCtrl + 1)
 
    Next iCount
Next i
End Function
 
I am trying to learn Java, just wanted to say that this is really cool to see :D
 
lol, thank you mate I really appreciate any help i can get
good luck
 
DJKarl your the best,
I am trying to adapt your code, and i think its working as i need it,
ill keep you guys posted
thanx a milion
 
Hi,

after 19 houres I am proud to present the solution.
I wouldn't have been able to do it without DJKarl's help.
thank you again for your help.

please find below the code
Code:
Private Sub Command0_Click()
Dim ssIn As String: ssIn = "GYM019680000005"
Dim sCharPool As String: sCharPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim sArrayIn(1 To 15) As String
Dim c As String
Dim i As Long, iCount As Long, iCtrl As Long
Dim iSum As Long: iSum = 0
 
    For i = 1 To 15
        c = Mid$(ssIn, i, 1)
        sArrayIn(i) = c
    Next i
    For iCount = 1 To 15
       iSum = iSum + (InStr(sCharPool, UCase(sArrayIn(iCount))) - 1) * (16 - (iCount - 1))
       iCtrl = (iSum Mod 36)
    Next iCount
 
    MsgBox "" & ssIn & Mid$(sCharPool, (iCtrl + 1), 1)
End Sub

P.S. if anyone hase time and maby simplify the code, please feel free.
Best Regards

Fatos from Kosovo
 
You could also do this:
Code:
Dim ssIn As String: ssIn = "G;Y;M;0;1;9;6;8;0;0;0;0;0;0;5"
Dim v As Variant 
Dim c As Long, i As Long

v = Split(UCase(ssIn), ";")
c = Ubound(v)

For i = 0 to c
       iSum = iSum + (InStr(sCharPool, v(i) - 1) * (16 - (iCount - 1))
       iCtrl = (iSum Mod 36)
Next i
(untested aircode)
 
There's not really any need for an array...

Code:
Dim ssIn As String: ssIn = "GYM019680000005"
Dim sCharPool As String: sCharPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim i As Long, iCtrl As Long
Dim iSum As Long: iSum = 0
 
    For i = 1 To 15
       iSum = iSum + (InStr(sCharPool, Mid(ssIn, i, 1)) - 1) * (16 - (i - 1))
       iCtrl = (iSum Mod 36)
    Next i
 
    MsgBox "" & ssIn & Mid$(sCharPool, (iCtrl + 1), 1)

Also, I've removed UCase because the VBA will consider the lower and upper case equivalent to be the same.

Chris
 
I just noticed the line iCtrl = (iSum Mod 36) should not be in the loop. It should be after the loop...

Code:
Dim ssIn As String: ssIn = "GYM019680000005"
Dim sCharPool As String: sCharPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim i As Long, iCtrl As Long
Dim iSum As Long: iSum = 0
 
    For i = 1 To 15
       iSum = iSum + (InStr(sCharPool, Mid(ssIn, i, 1)) - 1) * (16 - (i - 1))
    Next i
    
    iCtrl = (iSum Mod 36)
    
    MsgBox "" & ssIn & Mid$(sCharPool, (iCtrl + 1), 1)
 
Perfect Stopher,

this does the same, funny now that i see it i didnt really need an array lol :)
thank you for your time man.
 

Users who are viewing this thread

Back
Top Bottom