Solved

How to write a string to a text file

Posted on 2001-06-25
10
364 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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