File System Object error

T

td

I am using the code below which was lifted from the Code Librarian in Access
2000. Unfortunately the code generates an error "Type Mismatch" when the file
does not exist in the line:

Set f = fso.CreateTextFile(gSettings.LogFile, False)

(gSettings.LogFile is a user defined variable with .LogFile defined as
string and appears to contain a legitmate path as a string)
Could you please explain why there is a mismatch?
TIA
td
================================
Public Function WriteLogFile(Msg as string) as boolean
Dim fso As FileSystemObject
Dim f As File
Dim txtStream As TextStream

Set fso = New FileSystemObject

On Error Resume Next
' See if file already exists.
Set f = fso.GetFile(gSettings.LogFile)
' If not, then create it.
If Err <> 0 Then
Set f = fso.CreateTextFile(gSettings.LogFile, False)
If Err Then
'Could not create the file
WriteLogFile = False
Exit Function
End If
End If
On Error GoTo 0

' Open file as text stream for reading.
Set txtStream = f.OpenAsTextStream(ForAppending)
' Write error information and close.
With txtStream
.WriteLine Format(Date, "Short Date") & Msg
.Close
End With
End Function
 
C

Cindy M.

Hi =?Utf-8?B?dGQ=?=,
I am using the code below which was lifted from the Code Librarian in Access
2000. Unfortunately the code generates an error "Type Mismatch" when the file
does not exist in the line:

Set f = fso.CreateTextFile(gSettings.LogFile, False)

(gSettings.LogFile is a user defined variable with .LogFile defined as
string and appears to contain a legitmate path as a string)
Could you please explain why there is a mismatch?
Careful reading of the Help for CreateTextFile reveals that it returns a
TEXTSTREAM, not a file. So
dim f as TextStream

================================
Public Function WriteLogFile(Msg as string) as boolean
Dim fso As FileSystemObject
Dim f As File
Dim txtStream As TextStream

Set fso = New FileSystemObject

On Error Resume Next
' See if file already exists.
Set f = fso.GetFile(gSettings.LogFile)
' If not, then create it.
If Err <> 0 Then
Set f = fso.CreateTextFile(gSettings.LogFile, False)
If Err Then
'Could not create the file
WriteLogFile = False
Exit Function
End If
End If
On Error GoTo 0

' Open file as text stream for reading.
Set txtStream = f.OpenAsTextStream(ForAppending)
' Write error information and close.
With txtStream
.WriteLine Format(Date, "Short Date") & Msg
.Close
End With
End Function

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
T

td

Thanks Cindy,
I did try that but then the line
Set txtStream = f.OpenAsTextStream(ForAppending)
Does not make sense.
td
 
T

td

You were correct Cindy
I have followed that through & re-written part of the code & it now works
quite nicely. The original code as supplied by MS was a tiny bit off the mark
which makes it difficult when you are using it as a learning tool.
Thanks Cindy
td
 
C

Cindy M.

Hi td,
I have followed that through & re-written part of the code & it now works
quite nicely. The original code as supplied by MS was a tiny bit off the mark
which makes it difficult when you are using it as a learning tool.
Yeah. I especially dislike that none of the code samples in Help explicitly
declare the variables used in the code samples, by type. Glad you're up and
running :)

Cindy Meister
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top