By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,293 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,293 IT Pros & Developers. It's quick & easy.

text file reading in Access using VBA

P: n/a
Hi everyone,

I have a question about text file reading with VBA. I want to read he
whole contents of the file in one string variable.

I have been able to successfully read lines using:

Line Input #inputFile, inputLine ' read line into inputLine string

But is there any input command that lets me read the whole file into a
string variable without worrying about delimiters and all that.

Thanks a lot for any help you might give me,

Anja

Oct 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Anja" <an*******@googlemail.comwrote in news:1160842037.079468.300920
@b28g2000cwb.googlegroups.com:
Hi everyone,

I have a question about text file reading with VBA. I want to read he
whole contents of the file in one string variable.

I have been able to successfully read lines using:

Line Input #inputFile, inputLine ' read line into inputLine string

But is there any input command that lets me read the whole file into a
string variable without worrying about delimiters and all that.

Thanks a lot for any help you might give me,

Anja
Air Code Function

Public Function ReadFile$(byval fileName$)
Dim fileNumber%
fileNumber = FreeFile()
Open fileName For Binary As #fileNumber
ReadFile = String(LOF(fileNumber), vbNullChar)
Get #fileNumber, , ReadFile
Close #fileNumber
End Function

--
Lyle Fairfield
Oct 14 '06 #2

P: n/a

Lyle Fairfield wrote:
"Anja" <an*******@googlemail.comwrote in news:1160842037.079468.300920
@b28g2000cwb.googlegroups.com:
Hi everyone,

I have a question about text file reading with VBA. I want to read he
whole contents of the file in one string variable.

I have been able to successfully read lines using:

Line Input #inputFile, inputLine ' read line into inputLine string

But is there any input command that lets me read the whole file into a
string variable without worrying about delimiters and all that.

Thanks a lot for any help you might give me,

Anja

Air Code Function

Public Function ReadFile$(byval fileName$)
Dim fileNumber%
fileNumber = FreeFile()
Open fileName For Binary As #fileNumber
ReadFile = String(LOF(fileNumber), vbNullChar)
Get #fileNumber, , ReadFile
Close #fileNumber
End Function

--
Lyle Fairfield
Hi there,

Thanks for replying.

The line:

Get #fileNumber, , ReadFile

returns an error "Variable uses an automation type not supported in
Visual Basic".

I am using VBA under Access.

Thanks,
Anja

Oct 14 '06 #3

P: n/a
"Anja" <an*******@googlemail.comwrote in
news:11**********************@f16g2000cwb.googlegr oups.com:
>
Lyle Fairfield wrote:
>"Anja" <an*******@googlemail.comwrote in
news:1160842037.079468.300920 @b28g2000cwb.googlegroups.com:
Hi everyone,

I have a question about text file reading with VBA. I want to read
he whole contents of the file in one string variable.

I have been able to successfully read lines using:

Line Input #inputFile, inputLine ' read line into inputLine string

But is there any input command that lets me read the whole file
into a string variable without worrying about delimiters and all
that.

Thanks a lot for any help you might give me,

Anja

Air Code Function

Public Function ReadFile$(byval fileName$)
Dim fileNumber%
fileNumber = FreeFile()
Open fileName For Binary As #fileNumber
ReadFile = String(LOF(fileNumber), vbNullChar)
Get #fileNumber, , ReadFile
Close #fileNumber
End Function

--
Lyle Fairfield

Hi there,

Thanks for replying.

The line:

Get #fileNumber, , ReadFile

returns an error "Variable uses an automation type not supported in
Visual Basic".

I am using VBA under Access.
Me too. This works here in Canada:

Debug.Print ReadFile("C:\Documents and Settings\Lyle Fairfield\My
Documents\GeocacheTextFiles\(GCNHHP) MULTI-AGREEMENT CONLEY TRACT by
BigLittleZee.txt")

(that's all one line)
--
Lyle Fairfield
Oct 14 '06 #4

P: n/a
Hmm, posted this just the other day.

Sub ImportData(FileName As String)
Dim intFF As Integer
Dim strLine As String
Dim varLines As Variant
Dim intLine As Integer

intFF = FreeFile

Open FileName For Binary Access Read Lock Read Write As intFF

strLine = Space(LOF(intFF))
Get intFF, , strLine
Close intFF

varLines = Split(strLine, vbCrLf)

' Now you can step through the varLines array
' and put the relevant data into the correct
' fields in your table.

End Sub
--

Terry Kreft
"Anja" <an*******@googlemail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi everyone,

I have a question about text file reading with VBA. I want to read he
whole contents of the file in one string variable.

I have been able to successfully read lines using:

Line Input #inputFile, inputLine ' read line into inputLine string

But is there any input command that lets me read the whole file into a
string variable without worrying about delimiters and all that.

Thanks a lot for any help you might give me,

Anja

Oct 15 '06 #5

P: n/a
"Anja" <an*******@googlemail.comwrote in
news:11**********************@b28g2000cwb.googlegr oups.com:
Hi everyone,

I have a question about text file reading with VBA. I want to read he
whole contents of the file in one string variable.

I have been able to successfully read lines using:

Line Input #inputFile, inputLine ' read line into inputLine string

But is there any input command that lets me read the whole file into a
string variable without worrying about delimiters and all that.

Thanks a lot for any help you might give me,

Anja
I tested Lyle's function on Win98/Office 8 with no problems.

Sub Test()
MsgBox (Len(ReadFile("C:\WINDOWS\Application Data\AVG7\Log\emc.log")))
End Sub

This worked too.

Function FileToOneString()
Dim MyReadNum As Integer
Dim theGuts As Variant
'--
Rem FileToOneString(FileIN$)
Dim FileIN$
FileIN$ = "C:\WINDOWS\Application Data\AVG7\Log\emc.log"
'--
MyReadNum = FreeFile
Open FileIN For Input As MyReadNum

theGuts = Input$(LOF(MyReadNum), MyReadNum)
Close MyReadNum

MsgBox "Length is " & Len(theGuts) & vbCrLf & Right(theGuts, 40)
FileToOneString = theGuts
End Function

My test file was about nine Megs. I'm not sure what the limit is.

Cheers,
Alan
Oct 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.