Hi guys,
I am having a little difficulty with my importing in Access. Every time I import my text file, the lines will be jumbled. I have been reading up and I found this recordset code that seems to be what I need:
Basically, this code will extract data from the text file as long as it fulfills the Mid$ criteria. Here's where my problem comes. Each line in my text file is of different lengths and I have to capture the entire line.
I think using the Left$ function would help, but I don't know how to determine the character count such that the entire line of text would be inserted into the table.
Another difference between what I need and the code above is that, I am required to store each line into each row of my table, meaning
Line 1 is placed in Row 1 Column 1
Line 2 is placed in Row 2 Column 1
Line 3 is placed in Row 3 Column 1
.
.
.
Line X is placed in Row X Column 1.
Can somebody help me with this please? Especially the part about the character count?
I am having a little difficulty with my importing in Access. Every time I import my text file, the lines will be jumbled. I have been reading up and I found this recordset code that seems to be what I need:
Code:
Dim strLine As String
Dim intLineNum As Integer
Dim MyDB As DAO.Database
Dim rst As DAO.Recordset
Open "C:\Test\Test.txt" For Input As #1
Set MyDB = CurrentDb
Set rst = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
CurrentDb.Execute "DELETE * FROM tblResults", dbFailOnError 'Clear tblResults
With rst
.AddNew 'for 1st Block of Data only
Do While Not EOF(1)
Line Input #1, strLine 'Read line into variable.
intLineNum = intLineNum + 1 'Line Counter
If InStr(strLine, "Sample:") > 0 Then
![sample] = Mid$(strLine, InStr(strLine, "Sample:") + 8)
ElseIf InStr(strLine, "Additional Info:") > 0 Then
![AdditionalInfo] = Mid$(strLine, InStr(strLine, "Additional Info:") + 17)
ElseIf InStr(strLine, "Reference:") > 0 Then
![Reference] = Mid$(strLine, InStr(strLine, "Reference:") + 11)
ElseIf InStr(strLine, "Analyte") > 0 Then
'Debug.Print Mid$(strLine, InStr(strLine, "Analyte") + 8)
ElseIf Left$(strLine, 1) = "%" Then
![Percent] = Mid$(strLine, 3)
ElseIf InStr(strLine, "Scaling Ref. :") > 0 Then
![ScalingRef] = Mid$(strLine, InStr(strLine, "Scaling Ref. :") + 15)
ElseIf intLineNum Mod 14 = 0 Then
intLineNum = 0 'RESET Line Counter for next Block of Data
![Date] = Date
.Update 'Record Separator, time to Save Record
.AddNew 'Return to ADD Mode for next Block
End If
Loop
End With
Close #1
rst.Close
Set rst = Nothing
Basically, this code will extract data from the text file as long as it fulfills the Mid$ criteria. Here's where my problem comes. Each line in my text file is of different lengths and I have to capture the entire line.
I think using the Left$ function would help, but I don't know how to determine the character count such that the entire line of text would be inserted into the table.
Another difference between what I need and the code above is that, I am required to store each line into each row of my table, meaning
Line 1 is placed in Row 1 Column 1
Line 2 is placed in Row 2 Column 1
Line 3 is placed in Row 3 Column 1
.
.
.
Line X is placed in Row X Column 1.
Can somebody help me with this please? Especially the part about the character count?
