Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VBS Create CSV Log file

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to set the sa password in a vb6 code for sql connection 9 60
Hide vba in gp 7 104
Spell Check in VB6 13 126
VB6 - Cannot type when doing an extract from MSHFlexgrid to Excel 6 18
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
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…

839 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