I added your comments and recommendations and unfortunately nothing changed. I can't seem to find a way to attached a copy of the db and text file. At least I don't I see an add attachment button, in this reply section. How can I get you the db and files?
FYI, there was a typo in the code. The line If intCount >= 2 Then SHOULD read If intCount >= 256 Then. I thought this was capture the first 255 columns and anything after would be inserted into the second table.
Next to the "Post Quick Reply" Button under the quick reply box should be a Button called "Go Advanced", when you click that there should be a Section called "Additional Options" which has "Manage Attachments" with a "Manage Attachments" Button.
I won't be able to handle an Access file with 2010 advanced veatures as I only have Access 2007.
Ok let me see how I can get it in a previous format. I dont think you need a mdb format so I will see how to save as a 2007 accdb. I thought as long the db was a .accdb file 2007 and newer versions could open it.
OBP, good news by adding what you suggested to the code I see via the msgBox the data is being read from the text file. So, the file is opening, it is looping through each record, but nothing is being adding to the table. It loops to the end and exits the code. I have an ADO reference set in my library. Any idea what is preventing the records from being added?
Yes that is good news indeed.
So we now have 2 things to check the division of StrInput in to individual fileds and then the creation of the record.
But before we do so, do the fields actually exist in the tables
"tblImport1"
"tblImport2"
Do they have names?
Is the text file definitely Tab delimited?
Which is what Varsplit is looking for.
You could also now try just putting StrInput straight in to a table with a memo field to ensure the data actually gets into the database.
Yes, the tables have the true field headers. I imported the text file into two tables and deleted the records leaving only field names. tblImport1 having the first 200 fields and tblImport2 having the remaining 62.
The file is a .txt extension but every record is separated by a comma. Is the file actually a .csv with .txt extension? Should I make it a csv instead of txt. I've been back and forth with it but like I said the txt format works just fine when < 255 fields.
I don't think that will necessarily work either, it is not the Record Delimiter that Split is working with it is the Field Delimiter.
So now we need to know what the Split Function is actualy doing.
I would suggest that you now move the msgbox to below this line
I would then add another msgbox below that one and use this just for comparison
Msgbox Left(strInput, 25)
That will provide you with the lefthand first 25 characters to see if you can identify the Field Delimiter.
You can of course make the 25 any value that you want to find the Delimiter.
ps You could also use Msgbox Left(strInput, 25) & " - " & Len(strInput)
The Len function will tell you how many characters are in each record found by strInput.
I made the changes you suggested. It still seems to get to the portion of code to start the loop and insert records but it skips it. I was able to make save a copy of import portion of the db in Access 2007. If the format doesn't work for you, just let me know.
I have identified the problem after making a few corrections to the VBA and have it putting data into the table.
However the lineinput statement is importing the whole txt file as one record, it is not recognising the Record delimiter, so it puts in just one record of 40,000+ characters.
It also crashes on field 151 with "record too large".
I am not sure how to overcome this issue of just one record, other than to try and split it up, to do so I need to identify the record delimiter.
I have not found any reference to the line input not seperating the records, the problem being that if you have sufficient records it will even excede the character limit for string.
However I may have a solution although a bit round about.
Excel 2007 and above will import the file directly into Excel without any problems, there are 46 records.
So depending on how often you or the users need to do this it is possible to import the data into Excel split the data on to 2 worksheets and then import them in to your Access tables.
ps Interestingly the Excel version of VBA Line Input also cannot detect the Record Delimiters, even though the Data Import works fine, wierd indeed.
Yes, this is strangely weird. Currently I can take the same file provided to you and it will import correctly into Access no issues whatsoever. The problem is it only inputs up to 255. Thus is why I was attempting to split the file into two tables and then combine the two tables by deleting out what isn't needed. The file is generated from an external interface as a .dat file. I have a import form setup. Using select file dialog syntax the user selects the file and the vba takes the .dat file and creates a temporary text file for import. I had to do those because unless I'm missing something Access can not import .dat files. This was all well and good until the file exceeded 255 columns. I can't uses .xlsx extension because it doesn't separate the records.
Unfortunately importing to Excel, splitting it out to two worksheets, and then importing to Access is not an option for this user application. Is it possible to open an instance of Excel, import the data, and specify ranges of the excel worksheet I would like to import to Access? Can you upload what you have working from what you referenced in your previous post? I'd like to see what you did. Thanks for the feedback. I've got to get this sorted out very soon because I'm running out of time.
OK, here is the database and the Excel workbook.
The plot thickens as far as Excel is concerned, it happily imports the data manually and as a Recorded Macro, it also Manually Parses the data OK, but if you try and Macro record the Parsing of the data it crashes at 132 columns, which again is wierd.
Why would it Parse manually but not allow a VBA Version?
Te database form now has 3 buttons all doing the same thing with minor mods.
The original button uses this code
TestPos = InStr(strInput, Chr$(32))
MsgBox intCount & " = " & Len(strInput) & " = " & TestPos & " = " & Left(strInput, TestPos)
strInput = Right(strInput, Len(strInput) - TestPos)
to trim the strinput (I have been trying to find what the file uses as the Record delimiter without success)
It import the data in to table starting at character 3338 and then tells the second attempt has field 151 too long, but I fon't try to import 151?
The second button uses the same code without those 3 lines and imports the Table headings.
The third button tries to skip the first record to go on to the second, there is no second record, all the data is held in one.
Hi andycl, sorry for the late reply. Please find code below (provided by OBP previously) that I am using currently and hope that it is helpful to you. Btw, I am using Access 2003 only.
Private Sub Cmd_Trf_Record_Accepted_Click()
Dim data As String, count As Integer, count2 As Integer, Start As Integer, finish As Integer, rstable As Object
Dim recount As Integer, fname As String, start2 As Integer, records As Integer, rs As Object, x As Integer
Dim first As Integer, fieldcount As Integer, rs2 As Object
On Error GoTo errorcatch
Set rstable = CurrentDb.OpenRecordset("PA_Accepted")
Set rs = CurrentDb.OpenRecordset("tblone")
fieldcount = rs.Fields.count
rstable.MoveLast
recount = rstable.RecordCount
rstable.MoveFirst
For records = 2 To recount
x = x + 1
rs.AddNew
rstable.MoveNext
count2 = 1
data = rstable.Field1
Start = 1
For count = 1 To Len(data)
If Mid(data, count, 1) = "|" Then
fname = Mid(data, Start, (count - Start))
Start = count + 1
count2 = count2 + 1
If Not IsNull(fname) And fname <> "" Then
rs(count2 - 1).Value = fname
Else
rs(count2 - 1).Value = Null
End If
End If
If count2 = fieldcount Then Exit For
Next count
rs.Update
rs.Bookmark = rs.LastModified
Set rs2 = CurrentDb.OpenRecordset("tbltwo")
rs2.AddNew
first = InStr(1, data, "|")
count2 = 1
rs2(count2 - 1).Value = Left(data, first - 1)
start2 = Start
For count = start2 To Len(data) - 1
If Mid(data, count, 1) = "|" Then
fname = Mid(data, start2, (count - start2))
start2 = count + 1
count2 = count2 + 1
If Not IsNull(fname) And fname <> "" Then
rs2(count2 - 1).Value = fname
Else
rs2(count2 - 1).Value = Null
End If
End If
Next count
'add Last Fields data
fname = Right(data, ((count + 1) - start2))
If Not IsNull(fname) And fname <> "" Then
rs2(count2).Value = fname
Else
rs2(count2).Value = Null
End If
rs2.Update
rs2.Bookmark = rs2.LastModified
Next records
rs.Close
Set rs = Nothing
rs2.Close
Set rs2 = Nothing
rstable.Close
Set rstable = Nothing
MsgBox "added " & x & " records"
Exit Sub
errorcatch:
MsgBox records & " " & count & " " & count2 & " " & Err.Description & " " & fname
Resume Next
End Sub
Winxmun, thanks for digging up my old code.
Unfortunately I don't think it will help in this case as the "|" is not used in this txt file, it uses the comma for Field seperators, the biggest problem is that the Line Input does not bring the records in as records, just one lump of data, with no recognisable Record seperators, I have tried most of the usual ones
newline
linefeed
newline + linefeed
"¦"
"|"
Tab
without success.
But Excel can import it successfully, so it is recognising something that I can't find for the record delimiter.
I would go back and ask for the file to be changed to only include the required fields.
Or use SQL Express or something to do the import and transfer data from there.