Flaws in Module VB causing import failure?

  • Thread starter Thread starter amazonmike
  • Start date Start date
A

amazonmike

Guest
This module is called "modConvertUnixToWindows". Could someone take a look the code and see if you can spot a flaw? (I don't know VB well yet). I picked up this project from someone else, and it looks to me like she didn't finish modifying this module for our database. After the text is converted, it won't import properly into my table. I can't figire out why, and I'm wondering if one of these fuctions is to blame.

I would also like the message box and "successfully created" message to appear, but they don't, and they seem to be commented out in this example. How might I get it to work? Any feedback would be greatly appreciated. Thank you for your time.

Function TestImportText(ImportTextFile As String)
Dim ImportTextFilePath As String
Dim x As String, y As String
Dim NumberOfCarriageReturns As Long
Dim NumberOfLineFeeds As Long

NumberOfCarriageReturns = 0
NumberOfLineFeeds = 0

ImportTextFilePath = ("\\dummyFile\****\****\******\*******\********\*****\******." & ImportTextFile)
Open ImportTextFilePath For Input As #1
Do Until EOF(1)
Line Input #1, x
If InStr(1, x, Chr(13)) > 0 Then
NumberOfCarriageReturns = NumberOfCarriageReturns + 1
End If
If InStr(1, x, Chr(10)) > 0 Then
NumberOfLineFeeds = NumberOfLineFeeds + 1
End If
Loop
Close #1
'If no Carriage returns found, run the next function to modify
'the text file.

If NumberOfCarriageReturns < NumberOfLineFeeds And NumberOfLineFeeds > 0 Then
Dim NameOfNewText As String
NameOfNewText = ("\\DummyFile***\****\****\***********\****Files\" & ImportTextFile & ".txt")
y = ImportText(ImportTextFilePath, NameOfNewText)
End If
End Function
Function ImportText(OldText As String, NewText As String)
Dim x As String, Endvalue As Integer
Dim StartValue As Integer, OutputTxt As String

Open OldText For Input As #1
Open NewText For Output As #2
Do Until EOF(1)
Line Input #1, x
Endvalue = InStr(1, x, Chr(10))
StartValue = 1
Do Until Endvalue = 0
If Endvalue > 0 Then
OutputTxt = Mid(x, 1, (Endvalue - 1))
Print #2, OutputTxt
StartValue = Endvalue + 1
x = Mid(x, StartValue)
Endvalue = InStr(1, x, Chr(10))
End If
Loop
Loop
Close #1
Close #2
' MsgBox NewText & " Successfully created."
End Function
 
If you want the message box show just remove the ‘ from in front of that line. The ‘ indicates that the line is a comment.

My guess is that the purpose of these functions is to read a file, determine if the file has equal number of Chr(10) [Line feed] and Chr(13) [Carriage returns]. {Normally in a ASCII file each line or record is separated by a Chr(10) & Chr(13) or Line feed & Carriage returns.} If there aren’t any Chr(13)s then the 2nd function creates and new file {I’m guessing in a different directory }. You might try opening the created file in notepad and see if it has separate lines for each record. If it doesn’t then delete it and try running it again. {I don’t see anything in this code that deletes the file once you import it, you just be trying to import the same bad data over and over again.} I’d also check to see if the file has the correct number of fields. This would also cause it not import correctly. Without the file it’s hard to debug, but I say that if there is a problem with file not having Chr(13)s then you might be better off to open the file in binary mode and read it one character at a time. I believe the input mode assumes that this is a CSV file with Line feed & Carriage returns.

I have done this type of thing but I typically read the file into an Array one character at time and then using DAO code, load the data directly into the table
 

Users who are viewing this thread

Back
Top Bottom