Results 1 to 7 of 7

Thread: Problems with Writing to a log file, Please help!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Ireland
    Posts
    224

    Question

    Hi guys,
    What I'm trying to do write to a log file. The code here is part of my application that allows a user to change their password. The problem that I have is that the when the user changes their password for the first time the initial part of the code works and Prints some text to the log file, but that is the only thing that gets Printed. Could someone please tell me how go about putting some code in that will let me know in the log file when each stage of the code is executed. I need help really badly,

    Thanks a lot
    JK

    Code:
    Sub ComOK_Click()
    On Error GoTo ChangePW_ComOK_Click_Err
        'Creates a log file called DebugJK.log
        'If you want to open the file from the CURRENT directory:
        'added general function called AddSlash
        filenum = FreeFile
        Open AddSlash(CurDir) & "DebugJK.log" For Append As #filenum
        Print #filenum, "____Purpose of Logfile____: To determine any errors in the CHANGEPW.FRM "
        Print #filenum, "Last modified: "; Now
        Print #filenum, "By: "; who.Name
       
            If TextOldPassword.Text <> "" Then
    25:     UserSS.FindFirst ("[User Name] = '" + who.Name + "'")
            enc$ = encode(CStr(who.Name), CStr(TextOldPassword.Text))
            If (enc$ <> UserSS!Password) Or (who.Name <> UserSS![User Name]) Then Error 99
                    
                        Print #filenum, "CheckPoint1: The oldPasswordCheck has been run successfully"
                        Close #filenum
                        Open AddSlash(CurDir) & "DebugJK.log" For Append As #filenum
                        
            If Len(TextNewPassword.Text) < 6 Then
                Text = messageList(30) ' Password must have at least 6 characters
                        
                         Print #filenum, "Checkpoint 2: If the Password Typed is under 6 characters, this will appear in the logfile"
                         Close #filenum
                         Open AddSlash(CurDir) & "DebugJK.log" For Append As #filenum  'Reopens the Debug log
                       
                MsgBox Text, MB_ICONEXCLAMATION, "Password Change"
                TextNewPassword.Text = ""
                TextConfirm.Text = ""
                TextNewPassword.SetFocus
                
                        Print #filenum, "Checkpoint 3: If the Password Change Message box has appeared, this will appear in the logfile"
                        Close #filenum
                Exit Sub
            End If
            
            Else
                
                Open AddSlash(CurDir) & "DebugJK.log" For Append As #filenum  'Reopens the Debug log
                UserSS.Edit
                UserSS!Password = encode(CStr(who.Name), CStr(TextNewPassword.Text))
                Print #filenum, "The old Password has been Opened for edit and the Username has been associated with the new password"
                UserSS![Expiry Date] = Format$(Date + PasswordExpiry, "dd/mm/yyyy")
                Print #filenum, "The Expiry Date been set"
                UserSS.Update
                Print #filenum, "The new User Password has been Updated."
                Close #filenum
                
                
                If AUDITING = True Then
                Open AddSlash(CurDir) & "DebugJK.log" For Append As #filenum  'Reopens the Debug log
                Print #filenum, "This Confirms that the auditing Process has been started"
      
                    UserAudit.AddNew
                    'The Auditing Process has been started
                    UserAudit![User Name] = who.Name
                        Print #filenum, " The user name has been retrieved and put into the Variable [User Name]"
                    UserAudit!Password = "Changed"
                        Print #filenum, "The Password has been Changed"
                    UserAudit![Expiry Date] = Format$(Date + PasswordExpiry, "dd/mm/yyyy")
                        Print #filenum, "The Expiry Date has Been set"
                    UserAudit!By = who.Name
                        Print #filenum, "User Name, Who is Changing their password:   "; who.Name
                    UserAudit!Date = Format$(Date, "dd/mm/yyyy")
                        Print #filenum, "Date,  "; Format$(Date, "dd/mm/yyyy")
                    UserAudit!Time = Format$(Time, "Long Time")
                        Print #filenum, "Time?"; Format$(Time, "Long Time")
                    UserAudit.Update
                        Print #filenum, "The User Audit Has Successfully passed through all states."
                    Close #filenum
    
                End If
          
                Open AddSlash(CurDir) & "DebugJK.log" For Append As #filenum  'Reopens the Debug log
        
                MsgBox "Your password has been changed!", MB_ICONINFORMATION
           
                Print #filenum, "The Message Box to display that the Password has Successfully changed has been displayed"
                Close #flienum
            
            End If
        Else
            MsgBox "You must enter the old password!", MB_ICONEXCLAMATION
            TextOldPassword.Text = ""
            TextNewPassword.Text = ""
            TextConfirm.Text = ""
            TextOldPassword.SetFocus
            Exit Sub
        End If
        ChangePW.Hide
    
    ChangePW_ComOK_Click_Res:
        Exit Sub
    
    ChangePW_ComOK_Click_Err:
        If Erl = 25 Then
            Text = "Password change failed! Try again!"
            MsgBox Text, MB_ICONEXCLAMATION, "Password Change"
            TextOldPassword.Text = ""
            TextNewPassword.Text = ""
            TextConfirm.Text = ""
            TextOldPassword.SetFocus
            Resume ChangePW_ComOK_Click_Res
        Else
            MsgBox " Error " + CStr(Err) + " " + Error$(Err)
            z% = LogError("COMOK Password Change", " Error " + CStr(Err) + " " + Error$(Err))
            End
        End If
    
    End Sub

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    You seem to know bascially what you are doing, so i am not going to spoon feed you the code.

    Bascially what you need is a Sub that writes a string to a log file. You can pass it the Name of the file, and the String to place in the file. This sub should go in a module.

    Code:
    'basics for you
    Private Sub logToFile(strLogFileName As String, strToPrint As String)
        Dim iFreeFileNum As Integer
        
        iFreeFileNum = FreeFile
        Open strLogFileName For Append As iFreeFileNum
        
        Print #iFreeFileNum, strToPrint
        
        Close #iFreeFileNum
        
    End Sub
    Then as each bit of code completes succesfully, you can log that to the file.
    Code:
    Private Sub logOn()
        'code to log on
        
        logToFile "log.txt", "Logged user on succesfully"
    End Sub
    This keeps all of the access to the log file in one place, which is tidier, safer, and of coures mean you will have a lot less code repition.
    Iain, thats with an i by the way!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Ireland
    Posts
    224
    Thanks a lot for that, It really put me on the right track.
    I have encountered a small problem. Is it possible to do this??
    At the top of the code I did this:
    Code:
    Dim DebugLog As String
    DebugLog = AddSlash(CurDir) & "DebugJK.log"
    Because Addslash is a function I didn't konw if I could call a function from within a function.
    So I tried this
    Code:
    logToFile (DebugLog, "The Editing of the UserName has Successfully Started")
    But it keeps giving me a complie error, expected =.
    Can anyone help???Please

  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    remove the brackets from the call.

    Code:
    logToFile DebugLog, "The Editing of the UserName has Successfully Started"
    or use the "Call" statement

    Code:
    Call logToFile (DebugLog, "The Editing of the UserName has Successfully Started")
    Sub calls do not work if you place brackets around the arguments.

    Functions that are returning a value reqiuire them.
    Iain, thats with an i by the way!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Ireland
    Posts
    224
    Thanks a lot
    You've been a great help,
    JK

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Instead of doing all this have a look at the LogEvent method of the App object in MSDN library.

  7. #7
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Yes well good luck with what Mr Joacim Andersson said. Unless you know the quirks of that method, and its related properties.

    LogPath Property

    Returns the path and filename of the file used to capture output from the LogEvent method. Not available at design-time; read-only at run time.
    See that. Not available at design time, read only at run time. How is he supposed to set that property. Have you ever used the LogEvent Method, do you even know what you are talking about ??


    So to complete the information, you will need to use the "App.StartLogging" method. With this you can specify the log path, and the log mode.

    Code:
    App.StartLogging "c:\test\log event\test.txt", vbLogToFile
    Another hint by the way. This does not work in the IDE, only the compiled executable. So it makes debugging a bit of a pain. The method also places a lot of usless information in your log file, that makes it harder to find what you are looking for. Personally i would stick to the method you were already using.

    *walks off shaking head*
    Iain, thats with an i by the way!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width