Solved

I need help writing to a file in my VB6 application

Posted on 2013-12-24
7
273 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
[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
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 48

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 21

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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…
Six Sigma Control Plans
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

623 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