Results 1 to 6 of 6
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095

    Maintaining a log file in chronological order

    I get the impression that there's a VBA function, 'ReadFile", that will read an entire text file into memory. Perhaps I need an added Reference for a general module, most of what I've found online seems to focus on Excel. Anyway, I just want to read an existing log file into memory, Open a text log file for Output in my app and let it log its pertinent actions and then append the memory contents. The results is then a chronological log. Is ReadFile actually what I need? The log files are about 50 records or so.



    Chronological is the wrong word to use. The logging is on a app session by session. What I want is each set to be LIFO. With the small sets, there is a simple way of accomplishing what I want, but the notion of ReadFile caught my attention and would really like to know more about it in the context of Access, not Excel.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Access can import/link to as well as export text files.

    Can also use text file Read/Write automation. This would be a VBA user defined function or sub, call it ReadFile if you want.

    Why not log data to a table then export table?

    What have you found/tried?

    "my app" is an Access file? What are you logging?

    I have worked with laboratory test equipment that generate log file and have utility to print the file. You want something like that?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Not a built function but its easily done using file system object code.
    You can also write to a text file
    Place the code below in a standard module

    Code:
    Option Compare Database
    Option Explicit
    
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    
    Dim objFso As Object
    Dim logStream As Object
    Dim fs As Object
    
    '######################################
    '#  Use this to create and append a text file with log entries
    '#
    '#  Parameters
    '#      strLogEntry text to write eg "Hello world!"
    '#  Usage
    '#      LogEntry " Hello World!"
    '#
    '#  This will create or add to a file with..
    '#  25/04/2008 - 10:56:40 : Hello World!
    '#
    '######################################
    Sub LogEntry(strLogEntry As String)
        WriteToFile strCurrentDBDir & "LogFile.txt", strLogEntry
        'Debug.Print strLogEntry
    End Sub
    
    
    '######################################
    '#  Use this function to return the current db's path
    '#
    '######################################
    Function strCurrentDBDir() As String
        Dim strDBPath As String
        Dim strDBFile As String
        
        strDBPath = CurrentDb.Name
        strDBFile = Dir(strDBPath)
        strCurrentDBDir = left$(strDBPath, Len(strDBPath) - Len(strDBFile))
    End Function
    
    
    '######################################
    '#  Use this to create and append a text file with log entries
    '#
    '#  Parameters
    '#      strLogFile file to write to eg. "c:\mylog.txt"
    '#      strLogEntry text to write eg "Hello world!"
    '#  Usage
    '#      WriteToFile "c:\mylog.txt", "Hello World!"
    '#
    '#  This will create or add to a file with..
    '#  25/04/2008 - 10:56:40 : Hello World!
    '#
    '######################################
    Sub WriteToFile(strLogFile As String, strLogEntry As String)
    
        On Error Resume Next
        
        Set objFso = CreateObject("Scripting.FileSystemObject")
        Set logStream = objFso.OpenTextFile(strLogFile, ForAppending, True) 'Open log file
        If strLogEntry <> "" Then
            logStream.WriteLine Date & " - " & Time() & ":  " & strLogEntry
        Else
            logStream.WriteLine strLogEntry
        End If
        logStream.Close
    End Sub
    
    Sub AddTextToFile(strLogFile As String, strLogEntry As String) 'CR v5253
    
    'Identical to WriteToFile but without the date/time
        On Error Resume Next
        
        Set objFso = CreateObject("Scripting.FileSystemObject")
        Set logStream = objFso.OpenTextFile(strLogFile, ForAppending, True) 'Open log file
        
        logStream.WriteLine strLogEntry
        
        logStream.Close
    End Sub
    
    Function ReadTextFile(strFilename As String) As String
    
      Dim ret As Long
      
      Set objFso = CreateObject("Scripting.FileSystemObject")
      
      If objFso.FileExists(strFilename) Then
         Set logStream = objFso.OpenTextFile(strFilename)
         
         ReadTextFile = logStream.ReadAll
        ' Debug.Print Mid(ReadTextFile, InStr(ReadTextFile, "Update to version") + 18, 4)
         logStream.Close
         Set logStream = Nothing
       Else
         ret = MsgBox("File Not Found", vbCritical)
       End If
       
       Set objFso = Nothing
    End Function
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  4. #4
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    you can also create a Log history using table.

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,095
    Hi Colin,
    I think both you and June are on the right track. I'll put together the code that imports the existing log and trim to 50 records. Open a new log file for APPEND, let the task log its events as needed. Then at task end, export the imported and trimmed old log to the new log and close. I'll read-up on Import/Export plus DoCmd.TtansferText as appropriate to my issue.
    Thanks,
    Bill

  6. #6
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    Import/Export nor TransferText has nothing to do with your manual
    writing/reading to and from the textfile.

    those two are related to Tables/Query, which I am suggesting to you.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 11-01-2019, 01:05 AM
  2. Import from a Text File while maintaining database relation
    By robbeh in forum Import/Export Data
    Replies: 3
    Last Post: 03-12-2015, 03:07 PM
  3. Log off Command Button redirect to Log In Form
    By dsemclaughlin in forum Programming
    Replies: 1
    Last Post: 09-09-2013, 12:55 PM
  4. Sort a Text Field Chronological
    By Juan4412 in forum Queries
    Replies: 2
    Last Post: 09-07-2012, 12:20 PM
  5. Chronological Query/Report
    By Benjamin Breeg in forum Queries
    Replies: 6
    Last Post: 09-23-2011, 06:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums