Software Technology Tips

The following code snippet would allow one to write text to a file in VB 6.0. This can help if some one wants to log any kind of information to a log file.

The function basically accepts 3 parameters
1. The text/content of the line that needs to be written.
2. The path name where the file is existing / is to be created eventually.
3. The name of the file.
Private Function WriteToFile(strLineToWrite As String, _
                                     strPathName As String, _
                                     strFileName As String) As Boolean
    Dim FileN As Integer
    Dim FileExists As String
    Dim DirExists As String
    FileN= FreeFile
'--Check for the directory existance
    DirExists = Dir(strPathName, vbDirectory)
    If DirExists = "" Then
        MkDir (strPathName)
    End If
    FileExists = Dir(strPathName & "\" & strFileName, vbNormal)
'--Check for the file existance
'--If it's not there create it and write the line of text into it
    If FileExists = "" Then
         Open strPathName & "\" & strFileName For Output As FileN
            Print #FileN, strLineToWrite
        Close FileN
'--If it is there then append data to end of file
        Open strPathName & "\" & strFileName For Append As FileN
            Print #FileN, strLineToWrite
        Close FileN
    End If
    WriteToFile= True
End Function

Related Tags:

Visual Basic, Visual Basic 6

Author: Ansuman Sahu

Related Pages

  • VB Desktop Development
  • Windows Database Projects

Visual Basic

Let us Connect!

Awards and Achievements

Red Herringcolor DeloitteFast50 DB ZInnov1   Nascome  DB Stpi

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login