G
Guest
OK, that rids the "item not found in this collection" error.
And the code will now import records; but it still errors
when it hits memo data in the text file.
Run-Time Error 3163: The field is too small to accept
the amount of data you attempted to add; try inserting
or pasting less data.
I assume that will require an additional if statement using the Append Chunk
function?
--
LF
And the code will now import records; but it still errors
when it hits memo data in the text file.
Run-Time Error 3163: The field is too small to accept
the amount of data you attempted to add; try inserting
or pasting less data.
I assume that will require an additional if statement using the Append Chunk
function?
--
LF
Ken Snell (MVP) said:If the hover value is this:
"string"
then it's correct.
If it's this:
""string""
then the value in the variable needs to have the leading and trailing "
characters stripped out. This can be done this way, assuming that there
would not be a field name beginning with or ending with " character:
For lngDelimCount = LBound(varColumn) To UBound(varColumn)
If Left(varColumn(lngDelimCount), 1) = Chr(34) And _
Right(varColumn(lngDelimCount), 1) = Chr(34) Then _
varColumn(lngDelimCount) = Mid(varColumn(lngDelimCount),
_
2, Len(varColumn(lngDelimCount)) - 2)
rs.Fields(varColumn(lngDelimCount)).Value =
varRow(lngDelimCount)
Next lngDelimCount
--
Ken Snell
<MS ACCESS MVP>
LF said:Yes the hover value is in the recordset table, but I noticed that the
hover
value is surrounded by double quotes> do those quotes need to be stripped
out
first?
I tried two seperate text files to rule out that the content was the
source
of the problem. I also tried converting the variant column to a string and
got the same error in both instances.
strCol = varColumn(lngDelimCount))
rs.Fields(strCol).Value = varRow(lngDelimCount)
--
LF
Ken Snell (MVP) said:When you get the error, hold cursor over the varColumn variable and note
what the value of that variable is. Is that field in the table that's the
basis of the recordset?
--
Ken Snell
<MS ACCESS MVP>
Almost there, although I can see in the VB Locals pane that it returns
the
expected varColumn, varRow, & lngDelimCount variables. it errors "Item
not
found in this collection" at the rs.Fields Value property in step 8.
One of the fields datatype is Memo; does that matter?
--
LF
:
Sorry -- I overlooked that you're bringing the values of strFilePath
and
strTbl to the sub. Here is corrected code:
Sub btnTest_Click (strFilePath as String, strTbl as String)
Dim strColumns as String, strRow as String
Dim varColumn as Variant, varRow as Variant
Dim lngDelimCount as Long, i as Integer, intF as Integer
Dim db as DAO.database, rs as DAO.recordset, strSQL as String
Const strDelim As String = ","
intF = FreeFile()
'__1) Open the text file.
Open strFilePath For Input As #intF
'__2) Get column names from the first text file paragraph.
Line Input #intF, strColumns
'__3) Parse column names into an array of field names.
varColumn = Split (strColumns, strDelim)
'__4) Open a recordset to the table that will receive the data.
Set db = CurrentDb
strSQL = "SELECT * FROM [" & srtTbl & "];"
Set rs = db.OpenRecordset (strSQL, dbOpenDynaset, dbAppendOnly)
'__5) Read each text file paragraph until the end of file is reached.
Do While EOF(intF) = False
Line Input #intF, strRow
'__6) Parse rows into an array of field values.
varRow = Split (strRow, strDelim)
'__7) Add a new record to the recordset.
rs.AddNew
'__8) Write row field values to the recordset.
For lngDelimCount = LBound(varColumn) To UBound(varColumn)
rs.Fields(varColumn(lngDelimCount)).Value =
varRow(lngDelimCount)
Next lngDelimCount
'__9) End the second loop.
'__10) Update the recordset.
rs.Update
'__11) End the first loop
Loop
'__12) Close the recordset.
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
'__13) Close the text file.
Close #intF
End Sub