Solved

How to write a string to a text file

Posted on 2001-06-25
10
371 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

688 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