Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write error messages to text file 3

Status
Not open for further replies.

ds2728

Programmer
Jul 18, 2001
50
US
Hi,

I have a process that runs, I am tring to log all problems I encounter to better resolve.

I am writing alot of info for each error. I use on_error and when I am done I resume Label: I had this info displayed via debug.print but it appears that I am overloading and losing the first items written.

Any help on how to write this information to a text file instead......

thanks,

dave
 
why not use a table

errortime (date/time) error(memo)
 
Create a table called tblErrors
ErrorID, Autonumber
ErrDate, Date
ErrNum, Number
ErrDesc, Text
What,Text


On_Err goto Err_Hand

Err_Hand:
Call Err_Log(Err.Num,Err.Desc,Me)

Public Sub Err_Log(ErrNum as Interger, ErrDesc as string,What as Object)

Dim DBS as database
Dim rst as rst]
Set DBS = currentdb()
Set rst = dbs.openrecodset("tblErrors")
With rst
.append
!Date = now()
!ErrNum = .ErrNum
!Desc = .desc
!What = .object
.update
End With
Set rst = nothing
Set dbs = nothing
End Sub

 
I would like to save the values in a bunch of variables to better identify any problems. I could setup a table and make a field for every value I would like to view later, i just thought there was a way to write the info I would like to a flat file as I do in other programs. I just could not seem to find the process to do that in VBA.
 
Well, 'databaseguy' is still on the 'right track'. In the table, Just create two more fields. [VarName] & [VarVal]. Whenever you log an error, you can 'add' as many records as desired, so keep the 'generic' info the same, but place the name of the variable in VarName] and it's Value in VarVal. Var name should be a "String" (Of course) - BUT [VARVAL]MUST ALSO be a "String". For non-String variables, Use CStr(NonStrVar).

One small (or not so small?) caveat. Tables filled with this info will grow MUCH faster than you are planning (I DON"T care if you are planning on it being HUGE - You didn't plan for it to be HUGE enough!). That is why I did it with a text file, the Text file is slower, but it does not cuse your db to GROW - and therefor need frequent deleting of all old records and compacting.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
MichaelRed

you said thats why you did it with a text file. That is what I am trying to find out how to do. I would rather just dump the info to a flat file, can you shed some light on how to do it to a text file. Speed is not an issue for my application it is more for debug purposes and I would rather do it this way. will resort to db it I have to.

Thanks,

dave
 
You just open the file for append on each occurance of logging an event. Print to the file, what ever you choose (I just Print "'XXXXX' = YYYYETR; " for each item). Close the file.

Actually, I set yp a seperate Module to handle the messy details in ONE place, and simply called it with a UDT filled with the info to log. Let the ONE stop routine do all of the open, print, close stuff. Not going to happen this PM, but if you want to see it, i'll try to retrieve it in the AM and post it for You.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Thank you for the reply, and yes I would like to see the code because on this one I am lost on how to get a text file written using VBA.

Thanks
 
Declaration of the UDT Array to hold the items to be logged. Note the array SIZE is NOT declared here. It is set in the CALL to the Main routine.

Code:
    Type TraceInfoType
        VarName As String * 30
        VarLoc As String * 50
        VarVal As Variant
        VarIndex As Integer
    End Type
    Global TraceInfo() As TraceInfoType 'Holder(s) for Trace Routine Information
'__________________________________________________________



Main Routine. This does the actual printing to the text file. there are a few issues to note here.

1.[tab] There are a LOT of snippets "If (Trace???) Then ... End If. the ??? represent classes which are 'public' in my app (e.g. Bks, Crs, ...) the booleans Trace??? are set in a form set up to monitor / control the debugging process. I used a set of checkboxes to set / clear these class trace booleans. They are (obviously?) quite unnecessary for your purposes, but I left them to illustrate a possabile way to optionally always log a few (globally available) parameters.
Removal of these blocks of code will dramatically reduce the size of the main function, but will have little impact on the overall performance.

2.[tab]The call to this routine (Info) is the number of items to enter into the tracelog file. It is SET for EVERY CALL to this routine as a Zero Based number of Element of TraceInfo (see Above).

Code:
Public Sub TraceLog(Info As Integer)

    'This Routine actually places the Records in the Text File.  It is called from _
     the various Modules/Functions/Subroutines.  A _typical_ call would Be:

    '   If (TraceOpt) Then
    '      Call TraceLog("AdoptBks.GetDcsInfo Entry")
    '   End If

    'The (Global) Var [TraceOpt] is set |cleared in the Maintenance Form.  When it _
     is Set, it brings up another form to check off the Data Sets to Report on. _
     For each data set checked, a "Key" set of parameters is reported.  These are _
     shown below in the Code.  Optional additional parameters are also reported. _
     Currently, these Optional Additional parameters are "Hard Coded" immediatly _
     below the standard data set parameters.  Hopefully, in the near future, we _
     can elaborate the process/system to permit more flexability in the Optional _
     parameters.

    Dim Idx As Integer

    Dim varTmp As Variant
    Dim MyAuthor As String
    Dim MyTitle As String
    Dim MyStId As String
    Dim MyTrmCde As String
    Dim MyCrsNum As String
    Dim MyOdt As String
    Dim MyRFN As String
    Dim MyPno As String
    Dim MyCrsSec As String
    Dim MyDptNum As String
    Dim MyAuthr8 As String
    Dim MyBookNo As String
    Dim MyPubCd8 As String
    Dim MyLRD As String
    Dim MyListNo As String
    Dim MyDateRtn As String
    Dim MyDateReq As String
    Dim MyChrgBack As String
    Dim MyRDt As String
    Dim MyRFd As String
    Dim MySeq As String
    Dim MyCrsBk As String
    Dim MyLoc As String
    Dim MyChr As String * 1

    Static LogOpen As Boolean
    Static MyFil As Integer
    Static MyCount As Long

    If (Not LogOpen) Then
        MyFil = FreeFile
        Open "TraceLog.txt" For Output As #MyFil
        LogOpen = True
        MyCount = 1
        ReDim TraceInfo(0)
        Print #MyFil, "Tracing: "; CurrEnvir.DataDir
    End If

    MyLoc = RepNullWSpace(TraceInfo(0).VarLoc)

            
    Print #MyFil, "@" & Trim(MyLoc) & " " & "[" & MyCount & "]"       '& Now() & " Incident Count =
    MyCount = MyCount + 1

    If (TraceBks) Then

        MyAuthor = RepNullWSpace(Trim(BKS.AUTHOR))
        MyTitle = RepNullWSpace(Trim(BKS.TITLE))

        Print #MyFil, "Books", BKS.ISBN, MyAuthor, BKS.BookNo
        Print #MyFil, "Books - Title", MyTitle
    End If

    If (TraceCbk) Then

        MyStId = RepNullWSpace(Trim(CBK.StId))
        MyTrmCde = RepNullWSpace(Trim(CBK.TRMCDE))
        MyDptNum = RepNullWSpace(Trim(CBK.DptsNm))
        MyCrsNum = RepNullWSpace(Trim(CBK.CrsNum))
        MyCrsSec = RepNullWSpace(Trim(CBK.CrsSEc))
        MyCrsBk = RepNullWSpace(Trim(CBK.BookNo))

        Print #MyFil, "Crs/Bks", MyStId, MyTrmCde, MyDptNum, _
                      MyCrsNum, MyCrsSec, MyCrsBk
    End If

    If (TraceCrs) Then

        MyStId = RepNullWSpace(Trim(Crs.StId))
        MyTrmCde = RepNullWSpace(Trim(Crs.TRMCDE))
        MyDptNum = RepNullWSpace(Trim(Crs.DptsNm))
        MyCrsNum = RepNullWSpace(Trim(Crs.CrsNum))
        MyCrsSec = RepNullWSpace(Trim(Crs.CrsSEc))

        Print #MyFil, "Course", MyStId, MyTrmCde, MyDptNum, _
                      MyCrsNum, MyCrsSec
    End If

    If (TraceDcs) Then

        MyStId = RepNullWSpace(Trim(DCS.StId))
        MyTrmCde = RepNullWSpace(Trim(DCS.TRMCDE))
        MyBookNo = RepNullWSpace(Trim(DCS.BookNo))
        MyAuthr8 = RepNullWSpace(Trim(DCS.Authr8))
        MyPubCd8 = RepNullWSpace(Trim(DCS.PUBCD8))

        Print #MyFil, "Decision", MyStId, MyTrmCde, MyBookNo, _
                      MyAuthr8, Trim(MyPubCd8)
    End If

    If (TracePoHdr) Then

        MyStId = RepNullWSpace(Trim(POH.StId))
        MyTrmCde = RepNullWSpace(Trim(POH.TRMCDE))
        MyPno = RepNullWSpace(Trim(POH.PNo))
        MyOdt = RepNullWSpace(Trim(POH.ODt))

        Print #MyFil, "P.O. Header", MyStId, MyTrmCde, Trim(MyPno), _
                      Format(MyOdt, "Short Date")
    End If

    If (TracePoDetail) Then

        MyStId = RepNullWSpace(Trim(POD.StId))
        MyTrmCde = RepNullWSpace(Trim(POD.TRMCDE))
        MyBookNo = RepNullWSpace(Trim(POD.BookNo))
        MyAuthr8 = RepNullWSpace(Trim(POD.Authr8))
        MyPno = RepNullWSpace(Trim(POD.PNo))

        Print #MyFil, "P.O. Detail", MyStId, Trim(MyTrmCde), MyBookNo, _
                      Trim(MyAuthr8), Trim(MyPno)
    End If

    If (TraceRcvHdr) Then

        MyStId = RepNullWSpace(Trim(RVH.StId))
        MyPubCd8 = RepNullWSpace(Trim(RVH.PUBCD8))
        MyRFN = RepNullWSpace(Trim(RVH.RFN))
        MySeq = RepNullWSpace(Trim(RVH.Seq))
        MyRFd = RepNullWSpace(Trim(RVH.RFd))

        Print #MyFil, "Rvc Hdr", MyStId, Trim(MyPubCd8), Trim(MyRFN), _
                      MySeq, Format(MyRFd, "Short Date")
    End If

    If (TraceRcvDetail) Then

        MyStId = RepNullWSpace(Trim(RVD.StId))
        MyTrmCde = RepNullWSpace(Trim(RVD.TRMCDE))
        MyPno = RepNullWSpace(Trim(RVD.PNo))
        MySeq = RepNullWSpace(Trim(RVD.Seq))
        MyBookNo = RepNullWSpace(Trim(RVD.BookNo))
        MyAuthr8 = RepNullWSpace(Trim(RVD.Authr8))
        MyRDt = RepNullWSpace(RVD.RDt)

        Print #MyFil, "Rcv Detail", MyStId, Trim(MyTrmCde), Trim(MyPno), _
                      MySeq, MyBookNo, MyAuthr8, Format(MyRDt, "Short Date")
    End If

    If (TraceRtnHdr) Then

        MyStId = RepNullWSpace(Trim(RTH.StId))
        MyPubCd8 = RepNullWSpace(Trim(RTH.PUBCD8))
        MyChrgBack = RepNullWSpace(Trim(RTH.ChrgBack))
        MyDateReq = RepNullWSpace(Trim(RTH.DateReq))
        MyDateRtn = RepNullWSpace(Trim(RTH.DateRtn))
        MyListNo = RepNullWSpace(Trim(RTH.ListNo))

        Print #MyFil, "Rtn Hdr", MyStId, MyPubCd8, MyChrgBack, _
                      Format(MyDateReq, "short date"), _
                      Format(MyDateRtn, "short date"), MyListNo
    End If

    If (TraceRtnDetail) Then

        MyStId = RepNullWSpace(Trim(RTD.StId))
        MyPubCd8 = RepNullWSpace(Trim(RTD.PUBCD8))
        MyListNo = RepNullWSpace(Trim(RTD.ListNo))
        MyAuthr8 = RepNullWSpace(Trim(RTD.Authr8))
        MyBookNo = RepNullWSpace(Trim(RTD.BookNo))
        MySeq = RepNullWSpace(Trim(RTD.Seq))
        MyLRD = RepNullWSpace(RTD.LRD)
        MyRFN = RepNullWSpace(RTD.RFN)

        Print #MyFil, "Rtn Detail", MyStId, MyPubCd8, MyListNo, MyAuthr8, _
                      MyBookNo, MySeq, MyLRD, MyRFN
    End If

    Print #MyFil, ""
    Print #MyFil, "Special Items: "
    Print #MyFil, ""

    'Add/modify the below to "Print" desired information to MyFil
    For Idx = 0 To Info
        If (Len(TraceInfo(Idx).VarName) > 0) Then
            If (Asc(Left(TraceInfo(Idx).VarName, 1)) > 32) Then        'MustHave a Char
                Print #MyFil, Space(7); Trim(TraceInfo(Idx).VarName) & " = "; TraceInfo(Idx).VarVal
            End If
        End If
    Next Idx

'    Print #MyFil, Space(10); "AdoptBks.cmdAddCbk.Caption = "; Trim(AdoptBks.cmdAddcbk.Caption)
'    Print #MyFil, Space(10); "AdoptBks.cmdAddCbk.Enabled = "; AdoptBks.cmdAddcbk.Enabled
'
'    Print #MyFil, ""

End Sub
'__________________________________________________________


Sample Call.

This is copied from an active module. This entire Block can be inserted as is - simply change the assignment statements for [TraceName] and [TraceMeth] to your module name and Procedure name at each Entry point.

Note that the call to [TraceLog] is a conditional, based on the Global Boolean "TraceOpt". I set/cleared this in the same form used to set/clear the Class module variables. You could just set / clear it manually in an immediate window.

Note that the First action in adding an element to the tracelog is to 'clear' the UDT array by setting it's dimension to zero - WITOUT the benefit of Preserve.

This call only has the single element (entry to the procedure). If you wanted to "LOg" additional information, just:

ReDim Preserve TraceInfo(UBound(TraceInfo) + 1) and fill in the next set of variables with the desired information.


Dim TraceName As String
Dim TraceMeth As String

TraceName = "ADoptBks"
TraceMeth = "Adoption"
If (TraceOpt) Then
ReDim TraceInfo(0)
TraceInfo(0).VarLoc = Me.Name & "." & TraceMeth & ": Entry"
Call TraceLog(UBound(TraceInfo))
End If
'__________________________________________________________



Dump/Print Routine.

This just dumps a minimally formatted version of the tracelog.txt file to the printer. I RAPIDLY found that reviewing this on screen was not in my planned lifestyle.

Code:
Public Sub basPrntBriefTRace()

    Dim MyLine As String
    Dim MyInFil As Integer
    Dim MyOutFil As Integer

    MyInFil = FreeFile
    Open "C:\MultiTrack\MTrakWin\TraceLog.Txt" For Input As MyInFil

    MyOutFil = FreeFile
    Open "C:\MultiTrack\MTrakWin\TraceLog2.Txt" For Output As MyOutFil


    While (Not EOF(MyInFil))
        Line Input #MyInFil, MyLine
        If (Left(MyLine, 1) = "@") Then
            Print #MyOutFil, Trim(MyLine)
        End If
        If (Left(MyLine, 7) = Space(7)) Then
            Print #MyOutFil, Space(5) & "Item: " & Trim(MyLine)
        End If
    Wend

    Close #MyInFil
    Close #MyOutFil

End Sub




'__________________________________________________________

Multiple line call
The following shows acall where four items are logged from the same point in the code. Note it is a SINGLE call to the main routine - with the ZERO BASED upper bound to the UDT array as the argument.

If (TraceOpt) Then
ReDim TraceInfo(3)
TraceInfo(0).VarName = "chkDcs.Value"
TraceInfo(0).VarVal = chkDcsIpo.Value
TraceInfo(1).VarName = "Dcs.DcsIpo"
TraceInfo(1).VarVal = DCS.DcsIpo
TraceInfo(2).VarName = "Dcs.DcsCut"
TraceInfo(2).VarVal = DCS.DcsCut
TraceInfo(3).VarName = "Dcs.DcwNqo"
TraceInfo(3).VarVal = DCS.DcwNqo
TraceInfo(0).VarLoc = Me.Name & "." & TraceMeth & ": Before chkDcsIpo"
Call TraceLog(UBound(TraceInfo))
End If


MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
On last post, make sure you trap error 0 in a select case. It's always generated. It means, ironically, no error....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top