Click here to Skip to main content
15,895,606 members

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:
VB
'---- Create File specification for expression table -------------------------

    EXPfilspec = EXPdir + prtNumNoPrt + ".exp"
     efil = Dir(EXPfilspec, vbDirectory)

 '---- If an old temp new file already exist delete it ---
  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
          
'Set Z-File name and Directory, Verify Z-File exists -------
  Zfilenam = Zfildir + optionPrt23 + ".TXT"
   Zfile = Dir(Zfilenam, vbDirectory)
    If Zfilenam = "" Then GoTo err11           'Error Flag

 '-----------------------------------------------------------
       'Define a variable for each character of the part number
              '---Up to 25 character part number ---
 '-----------------------------------------------------------

    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

'==========================================================
       'Open the Z-File for Evaluation '==========================================================
    'Open the Zfile, search for the part number character,
    'Locate its value by character position,
    'Create a new file, input the values, then close it
 '----------------------------------------------------------
 num = Len(fname)	‘Length of the part number
 
 Dim Info3 As String * 1
 
 Close #1           'Make sure the file is closed

'------------- Check for valid Z-file then ------------
'------------ Open z-file for random access ---------------
 
    Dim nZfil As String
    nZfil = Zfilenam

    Open nZfil For Random Access Read As #1 Len = Len(Info3)
    
 '---------- Create new Temporary text file to input data -------
 
 Close #2
 
    Nfile = EXPdir + "newfile.txt"
    
    Open Nfile For Append Access Write As #2

 '----------------------------------------------------------
 'Finding Part Number character in Z-file
 '----------------------------------------------------------
 
'Define variables
    cpos = 1
    cpstn = 1      		'part number character position
    loc = C1       			'part number character position string
lb20:
    recno = 1     			' Define record number.
    recnum = 1     		' define record number
    lin = 1        			' Define line number
    Pos = 0        			' Define z-file character position
 For recno = 1 To 7000   	' Loop x times.
  
' Read the Z-file using the Get statement.
 Get #1, recno, Info3                     	'Get first character from Z-file
         If Info3 = loc And Pos = 0 Then   'if it matches the part number
            recnum = recno                	'Define record number of character
          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

 '--------------------------------------------------------
            'locate value for character in Z-file
 '--------------------------------------------------------
 
    fnd = 0
    cnt = 0
    Pos = 0
    val = ""
    
LB10:
    Get #1, recnum, data1             			'Read first record
        recnum = recnum + 1
         If data1 = loc And Pos = 0 Then GoTo LB10 	'If location is zero
            Pos = Pos + 1
         If data1 = "," And fnd = 1 Then GoTo lb11 	'If comma after value
         If data1 = "," Then cnt = cnt + 1         		'Count comma's
         If data1 = "," Then GoTo LB10             		'Loop if comma
         If data1 = "*" Then GoTo LBf1DCerr1    		'End of line, Error no value found
         If cnt <> cpstn Then GoTo LB10    		'Loop if wrong position
             fnd = 1
           val = val + data1
GoTo LB10
lb11:
         If loc = "" Then val = ""      			 'Create no value for no character
 
 Dim chp As String
 
 chp = cpstn
 
    entry = "A'" + chp + "=" + val    			'Add "A'" variable string to value
         
'--- Open new file to input data ---
 Close #2
    Open Nfile For Append Access Write As #2 Len = Len(entry)

'--- Write variable and value to new file --- 
  Print #2, entry

   If cpos >= num Then GoTo lb30 'Escape loop after end of part Number
    
    saval = val
    val = ""
    cpos = cpos + 1                   'increment character position by one
    cpstn = cpstn + 1                 'next part number character position

  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 zfile.
 Close #2    ' Close new file.

 '---- If an old Expression file with the same name already exist, delete it ---
    newfile = EXPdir + fname + ".exp"       'Complete file spec

    fil = Dir(newfile, vbDirectory)
        If fil <> "" Then Kill EXPdir + fname + ".exp"

        
 '----Rename new file to name entered in txtPartNum.Text Box
 
    Name Nfile As newfile       'Rename new Expression file from newfile.txt


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.
Tags: