Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


I need help writing to a file in my VB6 application

Posted on 2013-12-24
Medium Priority
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,
Question by:mainrotor
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
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39738282
You got the file open by another application as word or Notepad??
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
Close #1

Open in new window

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"
      'To Read
    If fs.FileExists("C:\mytestfile.txt") Then
        Set ts = fs.OpenTextFile("C:\mytestfile.txt")
        Do While Not ts.AtEndOfStream
            MsgBox ts.ReadLine
    End If

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

Open in new window

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!

LVL 22

Expert Comment

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?

Accepted Solution

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

LVL 46

Expert Comment

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.

Author Closing Comment

ID: 39765900
The following code fixed the problem,

intFileCh = Freefile

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that 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…
Suggested Courses

719 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