|
-
Aug 2nd, 2017, 09:31 PM
#1
Thread Starter
Member
Write To Log File
I'm trying to create a sub that writes to a log file. If the file exists, append to it, otherwise create it:
Code:
Option Compare Database
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Function LogMessage(message As String)
Dim logFileName As String
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim file As Object
logFileName = CurrentProject.Path & "\MyLog.txt"
Dim filePath As String
filePath = ""
filePath = Dir(logFileName)
If filePath = "" Then
Set file = fso.CreateTextFile(logFileName, True, True)
Else
Set file = fso.OpenTextFile(logFileName, ForAppending)
End If
file.WriteLine Now & ": " & message
file.Close
Set fso = Nothing
Set file = Nothing
End Function
The CreateTextFile code works fine. But the OpenTextFile writes out the text in Chinese. What am I doing wrong here?
Thanks
-
Aug 2nd, 2017, 10:03 PM
#2
New Member
Re: Write To Log File
It looks like it's because it's outputting to a unicode file.
Change this line:
Set file = fso.CreateTextFile(logFileName, True, True)
to this:
Set file = fso.CreateTextFile(logFileName, True, False)
which will make it an ASCII file instead. The saving works correctly after this change.
According to MSDN, OpenTextFile has a parameter to say which format the file should be opened in. I changed the value on my side and it didn't seem to work. Changing the parameter for CreateTextFile does work for me.
https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx
-
Aug 2nd, 2017, 10:32 PM
#3
Re: Write To Log File
Not sure why you would see the results you are seeing.
I would suggest not using FSO for this though
Also no need to check to see if the file need to be created or not simply use the build in VB method
Code:
FileNumber=FreeFile
Open Filename for Append as #FileNumber
This method will create the file if it does not exist and append to it if it does.
To write a line to the file you would use the Print # method
Code:
Print #FileNumber Now & ": " & message
Close #FileNumber
-
Aug 3rd, 2017, 12:23 AM
#4
Re: Write To Log File
This smells like VBA, not true VB. You have posted in the wrong forum.
It is also being misused or you (a.) would not have to define those constants, and (b.) through IntelliSense you probably would have discovered your error.
If you had set a reference to Microsoft Scripting Runtime you'd be in less of a mess. As it stands this looks like something largely copy/pasted from a musty old VBScript example.
The upshot is that your CreateTextFile() call is explicitly creating a Unicode file but your OpenTextFile() call omits its Format argument and implicitly opens as ASCII (not even ANSI). Your solution lies in reading the fine manual: OpenTextFile Method.
There is a lot of other messy stuff there, all you need is:
Code:
Option Explicit
'Requires a reference to: Microsoft Scripting Runtime.
Function LogMessage(ByRef Message As String)
Dim FSO As Object
Dim LogFileName As String
Dim File As Object
Set FSO = New Scripting.FileSystemObject
#If VBA Then
LogFileName = CurrentProject.Path & "\MyLog.txt"
#Else
LogFileName = App.Path & "\MyLog.txt"
#End If
If FSO.FileExists(LogFileName) Then
Set File = FSO.OpenTextFile(LogFileName, ForAppending, False, TristateTrue)
Else
Set File = FSO.CreateTextFile(LogFileName, True, True)
End If
File.WriteLine CStr(Now) & ": " & Message
File.Close
End Function
-
Aug 3rd, 2017, 12:51 AM
#5
Re: Write To Log File
This dump shows where you went wrong and got what you thought were "Chinese characters" in the file:
Code:
0000 ff fe 38 00 2f 00 33 00 2f 00 32 00 30 00 31 00 ..8./.3./.2.0.1.
0010 37 00 20 00 31 00 3a 00 34 00 39 00 3a 00 35 00 7. .1.:.4.9.:.5.
0020 31 00 20 00 41 00 4d 00 3a 00 20 00 46 00 69 00 1. .A.M.:. .F.i.
0030 72 00 73 00 74 00 20 00 65 00 6e 00 74 00 72 00 r.s.t. .e.n.t.r.
0040 79 00 0d 00 0a 00 38 2f 33 2f 32 30 31 37 20 31 y.....8/3/2017 1
0050 3a 34 39 3a 35 31 20 41 4d 3a 20 53 65 63 6f 6e :49:51 AM: Secon
0060 64 20 65 6e 74 72 79 0d 0a d entry..
-
Aug 3rd, 2017, 08:56 AM
#6
Re: Write To Log File
 Originally Posted by dilettante
This smells like VBA, not true VB. You have posted in the wrong forum.
Yep now that I look at it again it looks like Access VBA
-
Aug 3rd, 2017, 05:17 PM
#7
Thread Starter
Member
Re: Write To Log File
Your code worked great. Thank you!
"This smells like VBA, not true VB. You have posted in the wrong forum."
Yes, it's VBA. I'm working in MS Access, and this is the Office Dev forum.
-
Aug 4th, 2017, 03:15 AM
#8
Re: Write To Log File
and this is the Office Dev forum.
yes, but originally it was posted in the vb6 forum
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|