PDA

View Full Version : Solved: Java code to vba



toqilula
04-20-2011, 09:44 AM
Hello VBA lovers :hi:

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:


public class mod36 {
public static void main (String [] arg){
String _sIn = "GYM014650000005";
String sCharPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String[] sArrayIn = new String [15];
char c;
for (int i = 0; i < 15; i++){
c = _sIn.charAt(i);
sArrayIn[i] = Character.toString(c);
}
int iSum = 0;
for (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));
}}

hansup
04-20-2011, 11:34 AM
This code sample demonstrates a For loop it VBA. It simply prints the numbers 1 through 10 on separate lines in the Immediate Window. You can get to the Immediate Window from the Access application window with the Ctrl+g keyboard shortcut.

Public Sub toqilula()
Const clngLast As Long = 10
Dim i As Long
For i = 1 To clngLast
Debug.Print i
Next i
End Sub

You can find additional details about the For loop from Access' online help.

toqilula
04-20-2011, 05:53 PM
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:


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

toqilula
04-21-2011, 01:32 AM
shorter veersion, faster better, wiser :)



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)