Solved

VBS Create CSV Log file

Posted on 2007-11-20
3
2,264 Views
Last Modified: 2012-08-13
Hi,

I need a function who write a csv log file.
I want to call my function like this:
WriteLog(DateTime,Value1,Value2,Value3)
Every time I call WriteLog, it should append the data to c:\temp\LogFile.csv

Thanks!
HPvH
0
Comment
Question by:HPvH
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 20326226
Try this code below, it give options on whether overwrite a file or append to a file:

Public Sub WriteFileText(ByVal FileName As String, ByVal Source As String, Optional noCrlf As Boolean = False, Optional Append As Boolean = False)
    On Error GoTo EHandler
    Dim Handle As Integer
    Handle = FreeFile
    If Append Then
        Open FileName For Append As #Handle
            If noCrlf Then
                Print #Handle, Source;
            Else
                Print #Handle, Source
            End If
            On Error Resume Next
        Close #Handle
    Else
        Open FileName For Output As #Handle
            If noCrlf Then
                Print #Handle, Source;
            Else
                Print #Handle, Source
            End If
            On Error Resume Next
        Close #Handle
    End If
    Exit Sub
EHandler:
    'ShowErrMsg
    On Error Resume Next
    Close #Handle
End Sub
0
 
LVL 6

Expert Comment

by:MorDrakka
ID: 20326229
Hi,

Try the following!

You can set the objFSO in the main script. You can set them in the function too but then he opens and closes them each time you enter the function.
DateTime, Value1, Value2, Value3 are all expected to be variables you get on your own :P

Regards,

M
SET objFSO = CreateObject("Scripting.FileSystemObject")

SET objOutputFile = objFSO.CreateTextFile("log.txt", True)
 
 

Function Writelog(DateTime, Value1, Value2, Value3)

objOutputfile.writeline DateTime & "," & Value1 & "," & Value2 & "," & Value3

End Function

Open in new window

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 20326230
A small quick example for your requirements
public sub WriteLog(byval dt as DateTime,byval Value1 as string,byval Value2 as string,byval Value3 as string)

On Error GoTo EHandler

   Dim Handle As Integer

   Handle = FreeFile

   Open "c:\temp\LogFile.csv" For Append As #Handle

   Print #Handle, dt & " " & value1 & " " & value2 & " " & value3

   Close #Handle

   Exit Sub

EHandler:

    'ShowErrMsg

    On Error Resume Next

    Close #Handle

end sub

Open in new window

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

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 65
vba find the last empty column 10 88
Crystal reports - Formula Field code need assistance with code 17 52
Advice in Xamarin 21 57
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 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

15 Experts available now in Live!

Get 1:1 Help Now