Solved

I need help writing to a file in my VB6 application

Posted on 2013-12-24
7
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now