I have a table with a text field in which I want to search and replace (or skip) special characters writing out the cleaned up file to a new text file.
The steps I'm doing are:
1. read text file
2. replace characters
3. write to new text file
I can read and write fine but the skipping or replacing with a null, isn't working the way I expect. Here is my code:
One of the unprintable letters that I'm getting (original file is from mainframe), is chr(26). The code recognizes it but it truncates the line instead of skipping the character.
Any help would be greatly appreciated.
Thanks
lhuffst6
The steps I'm doing are:
1. read text file
2. replace characters
3. write to new text file
I can read and write fine but the skipping or replacing with a null, isn't working the way I expect. Here is my code:
Code:
Function ReadStraightText()
Const AccChars As String = "Ñ^¢][Ñ{}~\!-ÆÔê;"
Const RegChars As String = " "
Dim myControl As Recordset
Dim i As Long, j As Long
Dim tempString As String
Dim currentCharacter As String
Dim found As Boolean
Dim foundPosition As Long
Dim Chari As Integer
Set myDB = CurrentDb()
Set myControl = myDB.OpenRecordset("tblcontrol")
myControl.MoveFirst
InputFileName = myControl("billingoutbound")
OutputFileName = myControl("routemapinbound")
RowCounter = 1
'clean the file first
Open InputFileName For Input As #1
Open OutputFileName For Output As #2
Do While Not EOF(1)
Line Input #1, lineoftext
tempString = lineoftext ' loop through the shorter string
Select Case True
Case Len(AccChars) <= Len(lineoftext)
' accent character list is shorter (or same)
' loop through accent character string
For i = 1 To Len(AccChars) ' get next accent character
currentCharacter = Mid$(AccChars, i, 1) ' replace with corresponding character in "regular" array
If InStr(tempString, currentCharacter) > 0 Then
tempString = Replace(tempString, currentCharacter, Mid$(RegChars, i, 1))
End If
'Debug.Print currentCharacter
'Debug.Print Asc(currentCharacter)
'Debug.Print tempString
If currentCharacter = Chr(26) Then
tempString = Replace(tempString, currentCharacter, Chr(32))
End If
Next i
Case Len(AccChars) > Len(lineoftext)
' input string is shorter
' loop through input string
For i = 1 To Len(lineoftext)
' grab current character from input string and
' determine if it is a special char
currentCharacter = Mid$(lineoftext, i, 1)
'if can't match on special characters tried the reverse but it didn't work
'If (currentCharacter >= Chr(48) And currentCharacter >= Chr(57)) Or _
' (currentCharacter >= Chr(65) And currentCharacter >= Chr(90)) Then
' currentCharacter = currentCharacter
'Else
' currentCharacter = vbNullString
'End If
found = (InStr(AccChars, currentCharacter) > 0)
If found Then ' find position of special character in special array
foundPosition = InStr(AccChars, currentCharacter)
' replace with corresponding character in "regular" array
tempString = Replace(tempString, currentCharacter, Mid$(RegChars, foundPosition, 1))
End If
Next i
End Select
Write #2, lineoftext 'should it be tempstring?
Loop
Close #1
Close #2
End Function
Any help would be greatly appreciated.
Thanks
lhuffst6