Hi Dan,
The function below takes the filespec of a textfile and returns its
contents. So if you have a table with one field containing the names or
filespecs and a memo field ready for the contents, you can simply use a
query to update the memo field to
FileContents([XXX])
where XXX is the name of the filespec field.
The FileContents function is intended for small-to-medium text files
only and I've never tested it with a file bigger than 4 megabytes,
though I expect it will work with much larger ones.
I've read through a dozen different ideas on how to attack importing
straight text files into a memo field without breaking it up in dozens
of records. Basically, I have a few thousand text files to import
into a database (I know it will grow large) so search and replace
doesn't work.
Any help on this would be greatly appreciated, this is one of the few
problems I've found that I couldn't answer by just searching deja.
Public Function FileContents( _
ByVal FileSpec As Variant, _
Optional ByVal ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return a error value.
' Alternatively, you can retrieve the error
' code in the ErrCode argument
Dim lngFN As Long
On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContents = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContents = Input(LOF(lngFN), #lngFN)
End If
ErrCode = 0
GoTo Exit_FileContents
Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContents = CVErr(Err.Number)
Else
FileContents = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function