Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 12-28-2008, 11:18 PM   #1
jal
Newly Registered User
 
Join Date: Mar 2007
Posts: 1,709
Thanks: 0
Thanked 3 Times in 3 Posts
jal will become famous soon enough jal will become famous soon enough
Reading a text file with FileSystemObject

I'm doing a VBscript. I have had some success so far, with populating recordsets and moving files.

The next thing I need to do is read a textfiile. The code I am using seems to be a textbook example but I can't get it to work.


Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso.OpenTextFile(pathToTextFile, 1) '1 means ForReading
Dim Ln
Do Until stream.AtEndOfStream
Ln = stream.ReadLine
Loop



I purchased a VBscript editor which has a debugger. When I step through the code, I verified that "PathToTextFile" has the correct value and I verified that this textfile does indeed contain a lot of text.

The code isn't throwing any errors. However, the variable "Ln" remains an empty string. It's not reading the text. It's just a regular plain text file editable in Wordpad or Notepad.

jal is offline   Reply With Quote
Old 12-28-2008, 11:22 PM   #2
jal
Newly Registered User
 
Join Date: Mar 2007
Posts: 1,709
Thanks: 0
Thanked 3 Times in 3 Posts
jal will become famous soon enough jal will become famous soon enough
Re: Reading a text file with FileSystemObject

Here's a sample I found in a tutorial - it's the same code as mine but mine isn't working:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\scripts\servers and services.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
'more code
Loop

From here:
http://www.activexperts.com/activmon...her/textfiles/

I also stepped through my code to make sure that the loop is iterating. The ReadLine is executing time and again in the loop but for some reason the Ln variable in my code isn't populating.
jal is offline   Reply With Quote
Old 12-28-2008, 11:45 PM   #3
jal
Newly Registered User
 
Join Date: Mar 2007
Posts: 1,709
Thanks: 0
Thanked 3 Times in 3 Posts
jal will become famous soon enough jal will become famous soon enough
Re: Reading a text file with FileSystemObject

Seems to be working now, can't say I understand it.

Apparently I had to set the "Format" value (the third param in OpenTextFile). I set it to negative 2 (means TristateUseDefault) and it worked.

jal is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with FileSystemObject and text files P Zero Modules & VBA 14 02-08-2007 09:33 AM
[SOLVED] Importing a text file into access table SuneeshSNair Queries 7 06-13-2006 09:57 AM
Import Text File into Access Then Delete the Text File tiomin Macros 3 10-04-2004 05:53 AM
text file extension - transfer text datacontrol General 3 02-09-2004 10:48 AM
Convert Excel file into Text file polina Modules & VBA 1 02-07-2003 09:44 AM




All times are GMT -8. The time now is 05:49 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World