Solved

Log Files - VB6

Posted on 2003-11-14
14
923 Views
Last Modified: 2010-05-01
I want to be able to log events from my application, such as what information was typed into various text boxes. Is there a way I can do this? If so, where can I get the info on how it all works?
0
Comment
Question by:JFrye
14 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9750483
Which  "events" are you looking to log?  

Every time you move your mouse over a text box, hundreds of MouseMoved events fire.  Every time you press a key, the KeyDown, KeyUp, KeyPress events fire.  Get the idea?   =)

Are you actually asking how do you store the values of a text box in a file?

Idle_Mind
0
 

Author Comment

by:JFrye
ID: 9750637
Yeah, say I have 4 text boxes for instance. The user click a button, and stuff happens. Can I make it so that when the button is clicked, the data in those 4 tboxes also appends to a log file?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 30 total points
ID: 9750800
Sure, in the simplest of cases you would do something like this:

Private Sub Command1_Click()
    Open "c:\log.txt" For Append As #1
    Print #1, Text1.Text
    Print #1, Text2.Text
    Print #1, Text3.Text
    Print #1, Text4.Text
    Close #1
End Sub
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 9751283
another way:
Private sub command1_click
    Dim oContr As Control
    Dim Control As Control
    Open "c:\log.txt" For Append As #1 '< copied from Idle_Mind's comment
    For Each Control In Me.Controls
        Set oContr = Control
        On Error Resume Next
        If InStr(1, oContr.Name, "Text") > 0 Then
             Print #1, oContr.name, ",", oContr.text
        End If
        Err.Clear
        On Error GoTo 0
    Next Control
    Close #1
    Set oContr = Nothing
    Set Control = Nothing
End Sub

The only real diff between mine and Idle_Mind is you don't have to know how many textboxes are in the form, it also prints the name of the control so you can debug and you can add to it for other options.

HTH
dragontooth

0
 

Author Comment

by:JFrye
ID: 9751562
What about creating a new log file for each day? Is there some code to create a new file and name it the current date? Extra 40 pts for this!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9751595
Private Sub Form_Load()
    Dim fileName As String
   
    fileName = App.Path & "\" & Format(Now(), "ddmmmyyyy") & ".txt"
    Debug.Print fileName
    Open fileName For Append As #1
    Print #1, Text1.Text
    Print #1, Text2.Text
    Print #1, Text3.Text
    Print #1, Text4.Text
    Close #1
End Sub
0
 

Author Comment

by:JFrye
ID: 9751645
Run-time error '52':

Bad file name or number

What now?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9751765
Works great on my system.  Can you post it as you used it on your code?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 9753268
Mate,
Do one thing. Write a generalise routine with two parameters
for example

public function WriteToLog(controlName as string, sValue as string)as boolean
 'open the log file and write the detaills like this
on error goto errhandler
dim fn as integer
fn=freefile()
open app.path & "\log.txt" for append as fn
    Print #fn, controlName & Vbtab & "=" & vbtab & svalue
close #fn
WriteToLog = true
ErrEx:
exit function
ErrHandler:
WriteToLog=false
resume errex
End function

'Call this function in a button click or some other event. Loop through the control's collection of the form and call this subroutine to store the values of the needed controls in your form.

With regars
Team Accent
0
 

Author Comment

by:JFrye
ID: 9753870
That just lost me... I'm still new to the game, so I might get lost easily...
0
 

Author Comment

by:JFrye
ID: 9753918
Private Function AddToLog()

    Dim fileName As String
   
    fileName = App.Path & "G:\COMMON\DCS Manual\Northlite SuperCenter\Jonathan's Briefcase\Log Files\" & Format(Now(), "ddmmmyyyy") & ".txt"
    Debug.Print fileName
    Open fileName For Append As #1
    Print #1, Date & " @ " & Time & ":"
    Print #1, "SAM:  " & txtSAM2M.Text & ",  SA1:  " & txtSA1.Text & ",  SA2:  " & txtSA2.Text
    Print #1, "UP1:  " & txtUP1.Text & ",  UP2:  " & txtUP2.Text & ",  PG: " & txtPG.Text
    Print #1, "D768: " & txtDSLM768.Text & ",  D256: " & txtDSLM256.Text & ",  DU23: " & txtDialUp2.Text & ",  DU19: " & txtDialUp1.Text
    Print #1, "PP:   " & txtPC.Text & ",  PG$:  " & txtPGMoney.Text & ",  AC:   " & txtAC.Text & ",  Aux:  " & txtAux.Text
    Print #1,
    Close #1
End Function
0
 

Author Comment

by:JFrye
ID: 9753922
This is what I have... Before we did the 'name the log file as the current date' thing, it wrote properly to the single log file.
0
 

Author Comment

by:JFrye
ID: 9753944
Wow... I feel stupid now... I just realized the "AppPath & " in there... Took that out and it worked fine...
0
 

Author Comment

by:JFrye
ID: 9754013
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 add a minute to the date time value 8 61
bit defender blocks good applications 2 82
Access query that references subform 5 43
MsgBox 4 46
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now