Solved

VBS Create CSV Log file

Posted on 2007-11-20
3
2,263 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

744 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

13 Experts available now in Live!

Get 1:1 Help Now