Solved

How to write a string to a text file

Posted on 2001-06-25
10
363 Views
Last Modified: 2007-11-27
Hello,

I need to take a variable and store it in a text file for retrieval after a reboot of a process. Below is a snip of the code I am using and the trouble is when it should write to the file I get an error code 54 Bad File Mode.

When I step thru the UstrProduct_ID shows the correct value, it just wont write it to the file that was created.


Dim FileSystemObject, TextStream As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.CreateTextFile("c:\catalog.txt", True)


Shell ("\\6183_CD_Server\test\initailcopy.exe"), vbMaximizedFocus


Repeat:
strProduct_ID = InputBox("Please Enter The Bundle System Catalog String. Use UPPER CASE Letters Only.", "6183 Install Process", "6183-")
UstrProduct_ID = UCase(strProduct_ID)

Set TextStream = FileSystemObject.OpenTextFile("c:\catalog.txt")
TextStream.Writeline (UstrProduct_ID)
TextStream.Close
0
Comment
Question by:bandu
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6224373
Try this additional Parameter:

Set TextStream = FileSystemObject.OpenTextFile("c:\catalog.txt", IOMode.ForAppending)

The default is ForReading, thus you cannot write to the file
0
 

Author Comment

by:bandu
ID: 6224409
Ok, I added the IOMode and now I get an error 424 Object Required when it passes the above line
Set TextStream = FileSystemObject.OpenTextFile("c:\catalog.txt", IOMode.ForAppending)
0
 
LVL 1

Expert Comment

by:mcbeth
ID: 6224433
try this

public Sub WriteToCatalog(pzText as string)
open "c:\catalog.txt" for append as #1
write #1, pzText
close #1
0
 

Expert Comment

by:Amitabh
ID: 6224473
hi bandu !!!
the following code is not using filesystem objects but the following two functions can be successfully used for writing and reading from the txt file. hope this would solve your purpose.

cheers!!!!!!!!!!
Amitabh

'To Write in the file
'=====================
Public sub generate_txtfile()
On Error GoTo fl_err:
Dim p_fileno As Integer
Dim p_str As String,p_filepath as string

p_str = "writing text to the file which will be read later"
p_filepath= "c:\bandu\nameofthefile.txt"

p_fileno = FreeFile()
Open p_filepath For Output As #p_fileno 'creating the file
Print #p_fileno, p_str  'this will write the string in file
Close #p_fileno
Exit Sub
fl_err:
MsgBox Err.Description, , "ERROR"
End sub

'To read from the file
'======================
Public sub read_txtfile()
On Error GoTo readfl_err:
Dim p_fileno As Integer
Dim p_str As String, p_filepath As String

p_filepath = "c:\winnt\profiles\dlsyfsap\desktop\nameofthefile.txt"

p_fileno = FreeFile()
Open p_filepath For Input As #p_fileno 'opening the file
Line Input #p_fileno, p_str  'this will read the line in variable
Close #p_fileno
MsgBox CStr(p_str)
Exit Sub
readfl_err:
MsgBox Err.Description, , "ERROR"
End sub
0
 

Author Comment

by:bandu
ID: 6224521
McBeth,

I tried your method and it writes with no trouble. The only problem that remains is that the output to the file is "6183-ZZZZZZZZZZZ" and I need the "" removed so that when I read it back it is only the actual string. I guess I could read back only the characters minus the " but I want this as clean as I can get it.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:mcbeth
ID: 6224552
bandu,
yes there are "" in the textfile, but if you read out
the string the quotes arn't read out :

public function GetFromCatalog() as string
Dim pzText As String
Open "C:\catalog.txt" For Input As #1
Input #1, pzText
Close #1
GetFromCatalog= pzText
exit function
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6224568
replace write by print...
0
 
LVL 1

Expert Comment

by:mcbeth
ID: 6224577
thanks angelIII that's the point i forgot...
0
 
LVL 27

Accepted Solution

by:
Ark earned 100 total points
ID: 6225770
Try to change IOMode.ForAppending exactly to its value(8).
I've already faced this problem - FSO doesn't understand some predefined const (bug?)

Cheers
0
 

Author Comment

by:bandu
ID: 6225779
This solved the trouble and works great. I must note that while the other answers worked, I wanted to stay with FileSystemStream because of other requirements in my program.
Thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now