Solved

I need help writing to a file in my VB6 application

Posted on 2013-12-24
7
260 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 46

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 20

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 64
Delphi: barcode reading on android platform 1 30
Datagridview column when exported to Excel changes vb.net 1 13
write xml in vb.net 2 24
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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…

810 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