Translating characters in textbox from file.
Revision 2
So, I am currently working on a project where I have hit my limit. I have this code for posting for one of our mill machines and in the old way they translated every character from a textbox and made a location number for it. Then they read a textfile and used code to read through character by character in order to translate. My problem is I know there has to be a better way since this was done very adhoc. However, I don't know vb6 well at all and am not sure of how I would either improve the process or directly translate the code from vb6 to .Net. Here is the sample of the old code and one of the translation files.
fname = prtNumNoPrt
Old Code:
EXPfilspec = EXPdir + prtNumNoPrt + ".exp"
efil = Dir(EXPfilspec, vbDirectory)
Close #2
oldfname = EXPdir + "newfile.txt"
olfil = Dir(oldfname, vbDirectory)
If olfil <> "" Then Kill EXPdir + "newfile.txt"
‘If a Z-File is not required, jump over the Z-File checks
If NotZFile = 1 Then GoTo JumpZFileChk
Zfilenam = Zfildir + optionPrt23 + ".TXT"
Zfile = Dir(Zfilenam, vbDirectory)
If Zfilenam = "" Then GoTo err11
C1 = Mid(fname, 1, 1)
C2 = Mid(fname, 2, 1)
C3 = Mid(fname, 3, 1)
C4 = Mid(fname, 4, 1)
C5 = Mid(fname, 5, 1)
C6 = Mid(fname, 6, 1)
C7 = Mid(fname, 7, 1)
C8 = Mid(fname, 8, 1)
C9 = Mid(fname, 9, 1)
C10 = Mid(fname, 10, 1)
C11 = Mid(fname, 11, 1)
C12 = Mid(fname, 12, 1)
C13 = Mid(fname, 13, 1)
C14 = Mid(fname, 14, 1)
C15 = Mid(fname, 15, 1)
C16 = Mid(fname, 16, 1)
C17 = Mid(fname, 17, 1)
C18 = Mid(fname, 18, 1)
C19 = Mid(fname, 19, 1)
C20 = Mid(fname, 20, 1)
C21 = Mid(fname, 21, 1)
C22 = Mid(fname, 22, 1)
C23 = Mid(fname, 23, 1)
C24 = Mid(fname, 24, 1)
C25 = Mid(fname, 25, 1)
On Error GoTo LBf1DCerr1
num = Len(fname) ‘Length of the part number
Dim Info3 As String * 1
Close #1
Dim nZfil As String
nZfil = Zfilenam
Open nZfil For Random Access Read As #1 Len = Len(Info3)
Close #2
Nfile = EXPdir + "newfile.txt"
Open Nfile For Append Access Write As #2
cpos = 1
cpstn = 1
loc = C1
lb20:
recno = 1
recnum = 1
lin = 1
Pos = 0
For recno = 1 To 7000
Get #1, recno, Info3
If Info3 = loc And Pos = 0 Then
recnum = recno
Else
End If
If Info3 = "," Then
Pos = Pos + 1
Else
End If
If Info3 = "*" Then
lin = lin + 1
Pos = 0
Else
End If
Next recno
fnd = 0
cnt = 0
Pos = 0
val = ""
LB10:
Get #1, recnum, data1
recnum = recnum + 1
If data1 = loc And Pos = 0 Then GoTo LB10
Pos = Pos + 1
If data1 = "," And fnd = 1 Then GoTo lb11
If data1 = "," Then cnt = cnt + 1
If data1 = "," Then GoTo LB10
If data1 = "*" Then GoTo LBf1DCerr1
If cnt <> cpstn Then GoTo LB10
fnd = 1
val = val + data1
GoTo LB10
lb11:
If loc = "" Then val = ""
Dim chp As String
chp = cpstn
entry = "A'" + chp + "=" + val
Close #2
Open Nfile For Append Access Write As #2 Len = Len(entry)
Print #2, entry
If cpos >= num Then GoTo lb30
saval = val
val = ""
cpos = cpos + 1
cpstn = cpstn + 1
If cpos = 2 Then loc = C2
If cpos = 3 Then loc = C3
If cpos = 4 Then loc = C4
If cpos = 5 Then loc = C5
If cpos = 6 Then loc = C6
If cpos = 7 Then loc = C7
If cpos = 8 Then loc = C8
If cpos = 9 Then loc = C9
If cpos = 10 Then loc = C10
If cpos = 11 Then loc = C11
If cpos = 12 Then loc = C12
If cpos = 13 Then loc = C13
If cpos = 14 Then loc = C14
If cpos = 15 Then loc = C15
If cpos = 16 Then loc = C16
If cpos = 17 Then loc = C17
If cpos = 18 Then loc = C18
If cpos = 19 Then loc = C19
If cpos = 20 Then loc = C20
If cpos = 21 Then loc = C21
If cpos = 22 Then loc = C22
If cpos = 23 Then loc = C23
If cpos = 24 Then loc = C24
If cpos = 25 Then loc = C25
If cpos = 26 Then GoTo lb30
GoTo lb20
lb30:
GoTo Lb31
ExpErr:
Call GotFocus_Err
MsgBox "Expression file not found, Contact an M.E."
Call ErrMsgTxtBox
GoTo fini3
Err10:
Call GotFocus_Err
MsgBox "Error in Z-File Number *** Invalid Data ***", , "Z-File Error"
Call ErrMsgTxtBox
GoTo fini3
err11:
Call GotFocus_Err
MsgBox "Error in Z-File Number *** No Z-File Found ***", , "Z-File Error"
Call ErrMsgTxtBox
GoTo fini3
LBf1DCerr1:
Call GotFocus_Err
Dim yy As String
yy = cpstn
Label8.Caption = "No value found in Z-File for character postion " + yy + "." + vbCrLf + _
"Possible reasons: The part number you entered is incorrect or," + vbCrLf + _
"the part number you entered and the part number you selected" + vbCrLf + _
"are not of the same family or, the Z-File is incorrect" + vbCrLf + _
"Click in the Box above to start over"
Call ErrMsgTxtBox
Close #1
Close #2
Resume Next
GoTo fini3
Lb31:
‘Close the files
Close #1
Close #2
newfile = EXPdir + fname + ".exp"
fil = Dir(newfile, vbDirectory)
If fil <> "" Then Kill EXPdir + fname + ".exp"
Name Nfile As newfile
Translation File:
0,,,0,,,,,,,,,,,,,,,,,,0,,*
1,,,,1,,,1,,,,1,*
2,,,,,,,,,,,2,,,,,*
3,,,,,,,,,,,3,*
4,,,,,,,,,,,,,,,,,,,,4,*
5,,,,,,,,5,,,,,,,5,,*
6,,,,,,,,,,,,,,,,,,,,,,,,*
7,,,,,,,,,,,,,,7,,,,,*
8,,,,,,,,,,,,,*
9,,,,,,,,,,,,,*
A,,,,,11,11,,,11,11,*
B,,,,,,,,,12,12,*
C,13,,,,,,,,13,13,,,,,,,,13,*
D,,,,,,,,,14,14,*
E,,,,,,,,,15,15,*
F,,,,,,,,,16,16,*
G,,,,,,,,,17,17,*
H,,,,,,,,,18,18,*
I,,*
J,,*
K,,*
L,,*
M,,*
N,,,,,,,,,,,,24,,,,,24,,,,,*
O,,,*
P,,*
Q,,*
R,,*
S,,*
T,,*
U,,*
V,,32,*
W,,*
X,,,,,,,,,,,,,34,,,,*
Y,,*
Z,,*
_,,,,,,,,,,,,,,,,_,,,_,,,,*
-,,*
Basically, the character position matters in the textbox and honestly I am not that great with RegEx yet. Also, I know this is a lot of information so sorry for how much is in here.
Posted 16-Jul-13 7:25am
by
choren84.