Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to write a string to a text file

Posted on 2001-06-25
10
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 143

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 
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 143

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 28

Accepted Solution

by:
Ark earned 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

610 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