Writing to a text file

I want to write the contents of a field on a form to a text file when I hit the 'Save' button. This works O.K. the first time, but thereafter I get a "run Time error 70 - permission denied"! The code I am using is as follows, any suggestions?

Private Sub cmdSaveEntry_Click()
intFileNum = FreeFile
If Len(Me![txtField1].Text) = 0 Then
    MsgBox "There is nothing to save!"
Else
    Open "c:\savetext.txt" For Append As #intFileNum
    Write #intFileNum, Me![txtField1].Text
    Close #intFileNum
End If
End Sub

dkenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PlinioCommented:
why you don't try with the RichTextBox ActiveX control is more
ease with the SaveFile metod?

Regards
PSC
0
jaywalkCommented:
You might try changing Me![txtField1].Text to Me.txtField1.Text

Also Try declaring FileNum:
Dim FileNum as Long

But then you might not, too..

Frankly, I can't see anything wrong with it. I assume you're not messing with the permissions or anything...
0
EDDYKTCommented:
Also try this


Private Sub cmdSaveEntry_Click()
dim Tmp as string

intFileNum = FreeFile
Tmp = Me.txtField1.Text
If Len(Tmp) = 0 Then
    MsgBox "There is nothing to save!"
Else
    Open "c:\savetext.txt" For Append As #intFileNum
    Write #intFileNum, Tmp
    Close #intFileNum
End If
End Sub


0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

caraf_gCommented:
Nah, there's nothing wrong with your code. Tried it out and it works perfectly.

It's a mystery!

Your problem suggests that somehow the file does not get closed properly, but this is not through something you've done wrong in your code.

?
0
caraf_gCommented:
Plinio, that's no answer, by the way. VB file access it very straight forward and it surely should not be necessary to use a control just so you can write to a file.
0
dkenAuthor Commented:
Thanks, but none of those made any difference.

jaywalk - No I'm not messing with permissions.

dken
0
SLECommented:
Are you hitting the "Save" button several times with *short* intervals?

The OS might not have the time to close the file(handle). Insert a API Sleep(250) after the "Close #intFileNum" and try it out - had the a similar problem problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
caraf_gCommented:
Hm... SLE, are you saying that the closing of a file is somewhat asynchronous? That would indeed be a possible explanation.
0
rmichelsCommented:
Works fine on my PC as well (NT 4.0 SP4).

An obvious question, but one never knows.  Are you sure nothing else has opened up the file either within your program or an external program?
0
jmnolanCommented:
What line generates the Error 70? Is it the Open or the Write? Is it possible that the access is denied to the DB field?
0
SLECommented:
caraf_g,

Yes, that's what I saying - had this problem on NTWS4.0 - after adding the Sleep(), it worked fine...
0
caraf_gCommented:
Aha, the plot thickens...
0
deightonprogCommented:
Private Sub cmdSaveEntry_Click()
      static bRun as boolean

      if not brun then
                  
            brun = true            

                    intFileNum = FreeFile
                    If Len(Me![txtField1].Text) = 0 Then
                        MsgBox "There is nothing to save!"
                    Else
                  
                        Open "c:\savetext.txt" For Append As #intFileNum
                        Write #intFileNum, Me![txtField1].Text
                        Close #intFileNum
                    End If

            brun = false
      end if
      
End Sub
0
caraf_gCommented:
deighton, if the close is indeed asynchronous, that won't help...

Close #intFileNum
....OS starts closing the file

brun = false
....OS still closing the file

(next iteration)

if not brun then
....OS still closing the file

intFileNum = FreeFile
If Len(Me![txtField1].Text) = 0 Then
    MsgBox "There is nothing to save!"
Else
    ...OS still closing the file
    Open "c:\savetext.txt" For Append As #intFileNum
    ERROR.


Another solution.
'Try a couple of times
On Error Resume Next
Dim intTries As Integer
intTries = 0
Do While intTries < some reasonable number
    Open "c:\savetext.txt" ..etc
    If Err.Number = 0 Then
        'It worked!
        Exit Do
    End If
    DoEvents
    intTries = intTries + 1
Loop
0
caraf_gCommented:
'Oops forgot to finish it off properly

....
    DoEvents
    intTries = intTries + 1
Loop

'If there is an error here, save info
Dim intErrNum As Integer
intErrNum = Err.Number
'You could also save Err.Description and other info here if you so wish.

'Revert back to original error handling
'e.g.
On Error Goto 0
'or
On Error Goto OriginalErrorHandler

'Raise original error
If intErrNum <> 0 Then
    Err.Raise intErrNum 'See above
End If
0
fibdevCommented:
The only thing I suggest is that you try assigning the text file to a veriable before you open it.  I've never heard of a problem like this before, what version of vb are you using?  If 5.0 do you have Service Pack 3?
0
prem_kumar_25Commented:
Try This

Private Sub cmdSaveEntry_Click()
intFileNum = FreeFile
Open "c:\savetext.txt" For Append As #intFileNum          
If Len(Me![txtField1].Text) = 0 Then
           MsgBox "There is nothing to save!"
Else
         Write #intFileNum, Me![txtField1].Text
          Close #intFileNum
End If
End Sub
0
caraf_gCommented:
prem_kumar_25

What are you playing at?

If you propose an answer, you must be pretty confident that it is actually going to work.

Your code is not at all helpful; what's more, it is blatantly WRONG.

If this code is executed and the text field is empty, the file is left open.

A beginner could have spotted that.
0
EDDYKTCommented:
I believe Caraf_g should deserve the points.

However, caraf_g, I don't see anything wrong on prem_kumar_25's code.

If I remember correctly, text1.text should return "" instead of NULL if the field is empty.

8->

Correct me if I am wrong.
0
caraf_gCommented:
prem_kumar_25's code:


Private Sub cmdSaveEntry_Click()

'Free the file number
intFileNum = FreeFile

'Open the file for append
Open "c:\savetext.txt" For Append As #intFileNum            

'File is now open
If Len(Me![txtField1].Text) = 0 Then
    MsgBox "There is nothing to save!"
    'End of processing. File is still open. File will not be closed.
Else
    'Write data to the file
    Write #intFileNum, Me![txtField1].Text
    'Close the file. This is OK.
    Close #intFileNum
End If

'If the code enters the True part of the if statement (In other words, when the text box is empty) the file is NOT CLOSED.

End Sub

'EDDYKT, see it now?

'PS - what is this NULL you're talking about? The code checks Len of the text, not the text itself. Len("") = 0
0
caraf_gCommented:
"I believe caraf_g should deserve the points."

Not at all... If you read the thread, SLE is the only person who has so far come up with a plausible cause for dken's problem. If SLE's assumption is right, the solution proposed by SLE should work, and SLE should get the points.
0
EDDYKTCommented:
8->
0
caraf_gCommented:
<g>
0
fibdevCommented:
;-)
0
dkenAuthor Commented:
Thanks SLE, the 'sleep' idea seems to do the trick.
How do I give these points to you?
Thanks everyone for trying to help.
dken
0
caraf_gCommented:
Look at the "title bar" thingy above SLE's comment. On the right hand side you'll see a hyperlink for "Accept comment as an answer"
0
dkenAuthor Commented:
caraf_g, thanks.
dken
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.