Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I need help writing to a file in my VB6 application

Posted on 2013-12-24
7
Medium Priority
?
280 Views
Last Modified: 2014-01-08
Hi Experts,
I tried the code below to create and write to a file in my VB6 application, but I keep getting the following error whenever the file tries to open: "File already open"

Dim strTest As String 

Open "c:\sample.txt" For Output As #1 
strTest = "This is a test" 
Print #1, strTest

Close #1 

Open in new window


How can I fix this?

Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
7 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39738282
You got the file open by another application as word or Notepad??
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39738287
Put a line that just says Close after line 1.

Dim strTest As String 
On Error GoTo ErrorRoutine
Open "c:\sample.txt" For Output As #1 
strTest = "This is a test" 
Print #1, strTest
Close #1
Exit Sub
ErrorRoutine:
Close #1
Resume
 

Open in new window

0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39738290
Look at this code
Option Explicit
'Set a reference to "Microsoft Scripting Runtime"
Private Sub Command1_Click()
  'declare and initiate required objects
    Dim fs As FileSystemObject
    Dim ts As TextStream
    Set fs = New FileSystemObject
       'To write
    Set ts = fs.OpenTextFile("C:\mytestfile.txt", ForWriting, True)
    ts.WriteLine "I Love"
    ts.WriteLine "VB Forums"
    ts.Close
     
      'To Read
    If fs.FileExists("C:\mytestfile.txt") Then
        Set ts = fs.OpenTextFile("C:\mytestfile.txt")
        
        Do While Not ts.AtEndOfStream
            MsgBox ts.ReadLine
        Loop
        ts.Close
    End If

      'clear memory used by FSO objects
    Set ts = Nothing
    Set fs = Nothing
 End Sub

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Expert Comment

by:CompProbSolv
ID: 39738583
If you are running on Win 7 or 8, you may have problems writing a file to C:\.  Have you tried creating a folder and writing there?
0
 
LVL 2

Accepted Solution

by:
DiscGolfDad earned 2000 total points
ID: 39739352
Dim strTest As String 
Dim intFileCh As Integer

intFileCh = Freefile
Open "c:\sample.txt" For Output As intFileCh
strTest = "This is a test" 
Print #intFileCh, strTest

Close intFileCh

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 39741820
The message indicates that a prior invocation of the code did not succeed.  It should probably be fixed with a reboot.

As already mentioned, you are likely to get push-back from the security system if you try an write to the root directory of the C: drive.
0
 

Author Closing Comment

by:mainrotor
ID: 39765900
The following code fixed the problem,

intFileCh = Freefile
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

963 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