Solved

VBS Create CSV Log file

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access 2003, my txt file export is running backwards 4 51
Excel Vlookup to move data back to source. 4 90
Put text in a picture ASP.NET C# 2 56
Spell Check in VB6 13 112
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

816 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

8 Experts available now in Live!

Get 1:1 Help Now