Access VBA Import Text File Stops Halfway
up vote
0
down vote
favorite
I'm using Access 2013. I'm trying to import a .txt file into Access. The text file is 700MB (19MM records). My code filters the data and assigns a group value ("Inode") to keep associated records together - so I'm only bringing in roughly 600K records.
Here is a snippet of the source text file (you can see each Inode data group is separated by a dashed line):
I would like the final result to look like this:
For some reason, the program STOPS halfway through, at the SAME RECORD (roughly 8MM record mark). I can't locate what the issue is. I don't think it's a size issue as I have plenty of space. I've tried implementing error handling, but to no avail. The code simply bypasses it and the program ends (msgbox "done" appears). Opening the text file and reviewing the record where it stops does not help. There is nothing wrong/different about that record. It simply stops and I am baffled.
Here is the code:
Private Sub ImportTextFile()
On Error GoTo Err_LogError
Dim strFile As String, strLine As String
Dim lngFreeFile
Dim sInode_Num As String
Set db = CurrentDb()
DAO.DBEngine.SetOption dbMaxLocksPerFile, 1000000 <--- not sure if this helps
Set rs = db.OpenRecordset("tblImport")
strFile = "C:Datastore_data.txt"
lngFreeFile = FreeFile
Open strFile For Input As #lngFreeFile
Do Until EOF(lngFreeFile)
Line Input #lngFreeFile, strLine
If Left(LCase(Trim(strLine)), 9) = "inode_num" Then
sInode_Num = Trim(strLine)
End If
If InStr(LCase(strLine), "kmditemlastuseddate") > 0 Or _
InStr(LCase(strLine), "kmditemusecount") > 0 Or _
InStr(LCase(strLine), "kmditemuseddates") > 0 Or _
InStr(LCase(strLine), "kmditemdateadded") > 0 Then
rs.AddNew
rs![Inode_Num] = sInode_Num
rs![FieldValue] = Trim(strLine)
rs.Update
End If
Loop
Exit_LogError:
MsgBox "done."
Close #lngFreeFile
Set rst = Nothing
Exit Sub
Err_LogError:
strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Sub
NOTE: I used SSMS import wizard and was able to ingest the text file in its' entirety (19MM records) in just a few minutes. But the key to this is getting that Inode grouping so I can keep the associated records together. If there is a way to do that through the wizard i'd like to know.
Any assistance would be greatly appreciated.
Thank you!
EDIT: The following code seems to work:
Public Function ReadTextFile()
On Error GoTo Err_LogError
Dim objFSO As Object
Dim objTextStream As Object
Dim strTextLine As String
Dim strInputFileName As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblImport")
strInputFileName = "C:Datastore_data.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(strInputFileName)
Do While Not (objTextStream.AtEndOfStream)
strTextLine = objTextStream.ReadLine
If Left(LCase(Trim(strTextLine)), 9) = "inode_num" Then
sInode_Num = Trim(strTextLine)
End If
'
If InStr(LCase(strTextLine), "kmditemlastuseddate") > 0 Or _
InStr(LCase(strTextLine), "kmditemusecount") > 0 Or _
InStr(LCase(strTextLine), "kmditemuseddates") > 0 Or _
InStr(LCase(strTextLine), "kmditemdateadded") > 0 Then
'
rs.AddNew
rs![Inode_Num] = sInode_Num
rs![FieldValue] = Trim(strTextLine)
rs.Update
End If
Loop
Exit_LogError:
objTextStream.Close
Set objFSO = Nothing
Set objTextStream = Nothing
MsgBox "done."
Exit Function
Err_LogError:
strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function
vba ms-access
|
show 5 more comments
up vote
0
down vote
favorite
I'm using Access 2013. I'm trying to import a .txt file into Access. The text file is 700MB (19MM records). My code filters the data and assigns a group value ("Inode") to keep associated records together - so I'm only bringing in roughly 600K records.
Here is a snippet of the source text file (you can see each Inode data group is separated by a dashed line):
I would like the final result to look like this:
For some reason, the program STOPS halfway through, at the SAME RECORD (roughly 8MM record mark). I can't locate what the issue is. I don't think it's a size issue as I have plenty of space. I've tried implementing error handling, but to no avail. The code simply bypasses it and the program ends (msgbox "done" appears). Opening the text file and reviewing the record where it stops does not help. There is nothing wrong/different about that record. It simply stops and I am baffled.
Here is the code:
Private Sub ImportTextFile()
On Error GoTo Err_LogError
Dim strFile As String, strLine As String
Dim lngFreeFile
Dim sInode_Num As String
Set db = CurrentDb()
DAO.DBEngine.SetOption dbMaxLocksPerFile, 1000000 <--- not sure if this helps
Set rs = db.OpenRecordset("tblImport")
strFile = "C:Datastore_data.txt"
lngFreeFile = FreeFile
Open strFile For Input As #lngFreeFile
Do Until EOF(lngFreeFile)
Line Input #lngFreeFile, strLine
If Left(LCase(Trim(strLine)), 9) = "inode_num" Then
sInode_Num = Trim(strLine)
End If
If InStr(LCase(strLine), "kmditemlastuseddate") > 0 Or _
InStr(LCase(strLine), "kmditemusecount") > 0 Or _
InStr(LCase(strLine), "kmditemuseddates") > 0 Or _
InStr(LCase(strLine), "kmditemdateadded") > 0 Then
rs.AddNew
rs![Inode_Num] = sInode_Num
rs![FieldValue] = Trim(strLine)
rs.Update
End If
Loop
Exit_LogError:
MsgBox "done."
Close #lngFreeFile
Set rst = Nothing
Exit Sub
Err_LogError:
strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Sub
NOTE: I used SSMS import wizard and was able to ingest the text file in its' entirety (19MM records) in just a few minutes. But the key to this is getting that Inode grouping so I can keep the associated records together. If there is a way to do that through the wizard i'd like to know.
Any assistance would be greatly appreciated.
Thank you!
EDIT: The following code seems to work:
Public Function ReadTextFile()
On Error GoTo Err_LogError
Dim objFSO As Object
Dim objTextStream As Object
Dim strTextLine As String
Dim strInputFileName As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblImport")
strInputFileName = "C:Datastore_data.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(strInputFileName)
Do While Not (objTextStream.AtEndOfStream)
strTextLine = objTextStream.ReadLine
If Left(LCase(Trim(strTextLine)), 9) = "inode_num" Then
sInode_Num = Trim(strTextLine)
End If
'
If InStr(LCase(strTextLine), "kmditemlastuseddate") > 0 Or _
InStr(LCase(strTextLine), "kmditemusecount") > 0 Or _
InStr(LCase(strTextLine), "kmditemuseddates") > 0 Or _
InStr(LCase(strTextLine), "kmditemdateadded") > 0 Then
'
rs.AddNew
rs![Inode_Num] = sInode_Num
rs![FieldValue] = Trim(strTextLine)
rs.Update
End If
Loop
Exit_LogError:
objTextStream.Close
Set objFSO = Nothing
Set objTextStream = Nothing
MsgBox "done."
Exit Function
Err_LogError:
strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function
vba ms-access
How many characters does your text file contain?Open strFile For Input As #lngFreeFile
limits the file to 2^31 characters iirc, you might need to use WinAPI to be able to handle larger files
– Erik von Asmuth
13 hours ago
@Erik: Thanks...hmmm..well, not sure. But I would imagine a lot more than 2^31. Not sure how to use WINAPI with Access VBA - I'll peruse this site. Any links available?
– Craig
13 hours ago
Don't think there are links for accessing files with WinAPI using VBA, it's a niche subject. I have some code lying around from an attempt to do asynchronous file writes which you might be able to repurpose, but I think I'd better write a full answer after you verify this is the problem. You can keep a counter when reading, and I'm 99% sure you hit EOF after reading 2^31 characters. This docs page describes declaring external DLLs, but these are C++ apis and mapping types can prove difficult
– Erik von Asmuth
13 hours ago
Oh, and please let me know if you're on 64-bit or 32-bit Access. Writing code to work with large files/objects is generally a lot easier if you have access to theLongLong
data type.
– Erik von Asmuth
13 hours ago
1
Please share your solution as an answer, not an edit to the question
– Erik von Asmuth
11 hours ago
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using Access 2013. I'm trying to import a .txt file into Access. The text file is 700MB (19MM records). My code filters the data and assigns a group value ("Inode") to keep associated records together - so I'm only bringing in roughly 600K records.
Here is a snippet of the source text file (you can see each Inode data group is separated by a dashed line):
I would like the final result to look like this:
For some reason, the program STOPS halfway through, at the SAME RECORD (roughly 8MM record mark). I can't locate what the issue is. I don't think it's a size issue as I have plenty of space. I've tried implementing error handling, but to no avail. The code simply bypasses it and the program ends (msgbox "done" appears). Opening the text file and reviewing the record where it stops does not help. There is nothing wrong/different about that record. It simply stops and I am baffled.
Here is the code:
Private Sub ImportTextFile()
On Error GoTo Err_LogError
Dim strFile As String, strLine As String
Dim lngFreeFile
Dim sInode_Num As String
Set db = CurrentDb()
DAO.DBEngine.SetOption dbMaxLocksPerFile, 1000000 <--- not sure if this helps
Set rs = db.OpenRecordset("tblImport")
strFile = "C:Datastore_data.txt"
lngFreeFile = FreeFile
Open strFile For Input As #lngFreeFile
Do Until EOF(lngFreeFile)
Line Input #lngFreeFile, strLine
If Left(LCase(Trim(strLine)), 9) = "inode_num" Then
sInode_Num = Trim(strLine)
End If
If InStr(LCase(strLine), "kmditemlastuseddate") > 0 Or _
InStr(LCase(strLine), "kmditemusecount") > 0 Or _
InStr(LCase(strLine), "kmditemuseddates") > 0 Or _
InStr(LCase(strLine), "kmditemdateadded") > 0 Then
rs.AddNew
rs![Inode_Num] = sInode_Num
rs![FieldValue] = Trim(strLine)
rs.Update
End If
Loop
Exit_LogError:
MsgBox "done."
Close #lngFreeFile
Set rst = Nothing
Exit Sub
Err_LogError:
strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Sub
NOTE: I used SSMS import wizard and was able to ingest the text file in its' entirety (19MM records) in just a few minutes. But the key to this is getting that Inode grouping so I can keep the associated records together. If there is a way to do that through the wizard i'd like to know.
Any assistance would be greatly appreciated.
Thank you!
EDIT: The following code seems to work:
Public Function ReadTextFile()
On Error GoTo Err_LogError
Dim objFSO As Object
Dim objTextStream As Object
Dim strTextLine As String
Dim strInputFileName As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblImport")
strInputFileName = "C:Datastore_data.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(strInputFileName)
Do While Not (objTextStream.AtEndOfStream)
strTextLine = objTextStream.ReadLine
If Left(LCase(Trim(strTextLine)), 9) = "inode_num" Then
sInode_Num = Trim(strTextLine)
End If
'
If InStr(LCase(strTextLine), "kmditemlastuseddate") > 0 Or _
InStr(LCase(strTextLine), "kmditemusecount") > 0 Or _
InStr(LCase(strTextLine), "kmditemuseddates") > 0 Or _
InStr(LCase(strTextLine), "kmditemdateadded") > 0 Then
'
rs.AddNew
rs![Inode_Num] = sInode_Num
rs![FieldValue] = Trim(strTextLine)
rs.Update
End If
Loop
Exit_LogError:
objTextStream.Close
Set objFSO = Nothing
Set objTextStream = Nothing
MsgBox "done."
Exit Function
Err_LogError:
strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function
vba ms-access
I'm using Access 2013. I'm trying to import a .txt file into Access. The text file is 700MB (19MM records). My code filters the data and assigns a group value ("Inode") to keep associated records together - so I'm only bringing in roughly 600K records.
Here is a snippet of the source text file (you can see each Inode data group is separated by a dashed line):
I would like the final result to look like this:
For some reason, the program STOPS halfway through, at the SAME RECORD (roughly 8MM record mark). I can't locate what the issue is. I don't think it's a size issue as I have plenty of space. I've tried implementing error handling, but to no avail. The code simply bypasses it and the program ends (msgbox "done" appears). Opening the text file and reviewing the record where it stops does not help. There is nothing wrong/different about that record. It simply stops and I am baffled.
Here is the code:
Private Sub ImportTextFile()
On Error GoTo Err_LogError
Dim strFile As String, strLine As String
Dim lngFreeFile
Dim sInode_Num As String
Set db = CurrentDb()
DAO.DBEngine.SetOption dbMaxLocksPerFile, 1000000 <--- not sure if this helps
Set rs = db.OpenRecordset("tblImport")
strFile = "C:Datastore_data.txt"
lngFreeFile = FreeFile
Open strFile For Input As #lngFreeFile
Do Until EOF(lngFreeFile)
Line Input #lngFreeFile, strLine
If Left(LCase(Trim(strLine)), 9) = "inode_num" Then
sInode_Num = Trim(strLine)
End If
If InStr(LCase(strLine), "kmditemlastuseddate") > 0 Or _
InStr(LCase(strLine), "kmditemusecount") > 0 Or _
InStr(LCase(strLine), "kmditemuseddates") > 0 Or _
InStr(LCase(strLine), "kmditemdateadded") > 0 Then
rs.AddNew
rs![Inode_Num] = sInode_Num
rs![FieldValue] = Trim(strLine)
rs.Update
End If
Loop
Exit_LogError:
MsgBox "done."
Close #lngFreeFile
Set rst = Nothing
Exit Sub
Err_LogError:
strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Sub
NOTE: I used SSMS import wizard and was able to ingest the text file in its' entirety (19MM records) in just a few minutes. But the key to this is getting that Inode grouping so I can keep the associated records together. If there is a way to do that through the wizard i'd like to know.
Any assistance would be greatly appreciated.
Thank you!
EDIT: The following code seems to work:
Public Function ReadTextFile()
On Error GoTo Err_LogError
Dim objFSO As Object
Dim objTextStream As Object
Dim strTextLine As String
Dim strInputFileName As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblImport")
strInputFileName = "C:Datastore_data.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(strInputFileName)
Do While Not (objTextStream.AtEndOfStream)
strTextLine = objTextStream.ReadLine
If Left(LCase(Trim(strTextLine)), 9) = "inode_num" Then
sInode_Num = Trim(strTextLine)
End If
'
If InStr(LCase(strTextLine), "kmditemlastuseddate") > 0 Or _
InStr(LCase(strTextLine), "kmditemusecount") > 0 Or _
InStr(LCase(strTextLine), "kmditemuseddates") > 0 Or _
InStr(LCase(strTextLine), "kmditemdateadded") > 0 Then
'
rs.AddNew
rs![Inode_Num] = sInode_Num
rs![FieldValue] = Trim(strTextLine)
rs.Update
End If
Loop
Exit_LogError:
objTextStream.Close
Set objFSO = Nothing
Set objTextStream = Nothing
MsgBox "done."
Exit Function
Err_LogError:
strMsg = "Error: " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function
vba ms-access
vba ms-access
edited 12 hours ago
asked 13 hours ago
Craig
337
337
How many characters does your text file contain?Open strFile For Input As #lngFreeFile
limits the file to 2^31 characters iirc, you might need to use WinAPI to be able to handle larger files
– Erik von Asmuth
13 hours ago
@Erik: Thanks...hmmm..well, not sure. But I would imagine a lot more than 2^31. Not sure how to use WINAPI with Access VBA - I'll peruse this site. Any links available?
– Craig
13 hours ago
Don't think there are links for accessing files with WinAPI using VBA, it's a niche subject. I have some code lying around from an attempt to do asynchronous file writes which you might be able to repurpose, but I think I'd better write a full answer after you verify this is the problem. You can keep a counter when reading, and I'm 99% sure you hit EOF after reading 2^31 characters. This docs page describes declaring external DLLs, but these are C++ apis and mapping types can prove difficult
– Erik von Asmuth
13 hours ago
Oh, and please let me know if you're on 64-bit or 32-bit Access. Writing code to work with large files/objects is generally a lot easier if you have access to theLongLong
data type.
– Erik von Asmuth
13 hours ago
1
Please share your solution as an answer, not an edit to the question
– Erik von Asmuth
11 hours ago
|
show 5 more comments
How many characters does your text file contain?Open strFile For Input As #lngFreeFile
limits the file to 2^31 characters iirc, you might need to use WinAPI to be able to handle larger files
– Erik von Asmuth
13 hours ago
@Erik: Thanks...hmmm..well, not sure. But I would imagine a lot more than 2^31. Not sure how to use WINAPI with Access VBA - I'll peruse this site. Any links available?
– Craig
13 hours ago
Don't think there are links for accessing files with WinAPI using VBA, it's a niche subject. I have some code lying around from an attempt to do asynchronous file writes which you might be able to repurpose, but I think I'd better write a full answer after you verify this is the problem. You can keep a counter when reading, and I'm 99% sure you hit EOF after reading 2^31 characters. This docs page describes declaring external DLLs, but these are C++ apis and mapping types can prove difficult
– Erik von Asmuth
13 hours ago
Oh, and please let me know if you're on 64-bit or 32-bit Access. Writing code to work with large files/objects is generally a lot easier if you have access to theLongLong
data type.
– Erik von Asmuth
13 hours ago
1
Please share your solution as an answer, not an edit to the question
– Erik von Asmuth
11 hours ago
How many characters does your text file contain?
Open strFile For Input As #lngFreeFile
limits the file to 2^31 characters iirc, you might need to use WinAPI to be able to handle larger files– Erik von Asmuth
13 hours ago
How many characters does your text file contain?
Open strFile For Input As #lngFreeFile
limits the file to 2^31 characters iirc, you might need to use WinAPI to be able to handle larger files– Erik von Asmuth
13 hours ago
@Erik: Thanks...hmmm..well, not sure. But I would imagine a lot more than 2^31. Not sure how to use WINAPI with Access VBA - I'll peruse this site. Any links available?
– Craig
13 hours ago
@Erik: Thanks...hmmm..well, not sure. But I would imagine a lot more than 2^31. Not sure how to use WINAPI with Access VBA - I'll peruse this site. Any links available?
– Craig
13 hours ago
Don't think there are links for accessing files with WinAPI using VBA, it's a niche subject. I have some code lying around from an attempt to do asynchronous file writes which you might be able to repurpose, but I think I'd better write a full answer after you verify this is the problem. You can keep a counter when reading, and I'm 99% sure you hit EOF after reading 2^31 characters. This docs page describes declaring external DLLs, but these are C++ apis and mapping types can prove difficult
– Erik von Asmuth
13 hours ago
Don't think there are links for accessing files with WinAPI using VBA, it's a niche subject. I have some code lying around from an attempt to do asynchronous file writes which you might be able to repurpose, but I think I'd better write a full answer after you verify this is the problem. You can keep a counter when reading, and I'm 99% sure you hit EOF after reading 2^31 characters. This docs page describes declaring external DLLs, but these are C++ apis and mapping types can prove difficult
– Erik von Asmuth
13 hours ago
Oh, and please let me know if you're on 64-bit or 32-bit Access. Writing code to work with large files/objects is generally a lot easier if you have access to the
LongLong
data type.– Erik von Asmuth
13 hours ago
Oh, and please let me know if you're on 64-bit or 32-bit Access. Writing code to work with large files/objects is generally a lot easier if you have access to the
LongLong
data type.– Erik von Asmuth
13 hours ago
1
1
Please share your solution as an answer, not an edit to the question
– Erik von Asmuth
11 hours ago
Please share your solution as an answer, not an edit to the question
– Erik von Asmuth
11 hours ago
|
show 5 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203284%2faccess-vba-import-text-file-stops-halfway%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
How many characters does your text file contain?
Open strFile For Input As #lngFreeFile
limits the file to 2^31 characters iirc, you might need to use WinAPI to be able to handle larger files– Erik von Asmuth
13 hours ago
@Erik: Thanks...hmmm..well, not sure. But I would imagine a lot more than 2^31. Not sure how to use WINAPI with Access VBA - I'll peruse this site. Any links available?
– Craig
13 hours ago
Don't think there are links for accessing files with WinAPI using VBA, it's a niche subject. I have some code lying around from an attempt to do asynchronous file writes which you might be able to repurpose, but I think I'd better write a full answer after you verify this is the problem. You can keep a counter when reading, and I'm 99% sure you hit EOF after reading 2^31 characters. This docs page describes declaring external DLLs, but these are C++ apis and mapping types can prove difficult
– Erik von Asmuth
13 hours ago
Oh, and please let me know if you're on 64-bit or 32-bit Access. Writing code to work with large files/objects is generally a lot easier if you have access to the
LongLong
data type.– Erik von Asmuth
13 hours ago
1
Please share your solution as an answer, not an edit to the question
– Erik von Asmuth
11 hours ago