Link to home
Start Free TrialLog in
Avatar of sue33
sue33

asked on

Recording the time a button is pressed

I'm a complete beginner to VB6 and have a question. I want to record the time when a button is pressed - so each time the user presses some button named 'enter', I want  to record what time it is relative to the time the program was loaded. The idea is to record the rate at which information is being entered into a text box. I then want to store this information somewhere permanent. Is this possible?
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

you can capture the Time when a ButtonClick event is triggered using the Now Function in VB 6, which reads the current system Date AND Time (to the nearest second).  How you store these values is then entirely up to you.



Private Sub btnEnter_Click()
   Dim dtTime as Date
   dtTime = Now
' now save this value in some 'permanent' location - possibly in a record in a Database?

End Sub

AW
Avatar of AlexFM
AlexFM

This small program (Form with Command1 button) shows number of seconds from the start when user clicks on the button:

Option Explicit

Private tStart As Date

Private Sub Form_Load()
    tStart = Now
End Sub

Private Sub Command1_Click()
    Dim tCurrent As Date
    tCurrent = Now
   
    MsgBox Second(tCurrent - tStart)    ' number of seconds relative to program load time
End Sub

Avatar of sue33

ASKER

Thanks.

But how would I display the time in a text box rather than a message box? I want to build up a list of the times.

ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
on button click event
public sub button_click()
dim note as date
note = time
end sub
Hello sue33,

I understand what its like to be a complete beginner, I too have been experimenting with different load times.  the 'Now' function will give you both the date and time depending on how its used, but if you use it as I have below, you will get the time with seconds and AM or PM.

e.g. 10:20:05 AM
 
for this example you will need 2 textboxes, a command button and a listbox.

you may want to try:

Option Explicit

Public ltime As Long                'makes it easier to use loadtime in command1's event

Private Sub Form_Load()
Dim mins2 As Long, secs2 As Long

Text1.Text = Format$(Now, "long time")      'short time can be substituted within ""
mins2 = Minute(Time)
secs2 = Second(Time)

ltime = mins2 & secs2
End Sub


Then I would place 2nd textbox to display the time after the user clicks your commandbuton.

e.g.

Private Sub Command1_Click()
Dim ctime As Long         'current time
Dim etime As Long         'elapsed time in seconds
Dim mins As Long, secs As Long                    'mins and seconds used separately

Text2.Text = Format$(Now, "long time")     'use built in now function to get the time_
mins = Minute(Time)                                  'place a 3nd textbox on form to store ctime
secs = Second(Time)                                 'get the mins and secs separately


ctime = mins & secs                                      'get the mins and secs from current time    
etime = ctime - ltime                                     'subtract from load time

List1.AddItem Format$(etime, "00.00")          'use listbox control to store the list of elapsed
                                                                  'secs.
End Sub


This should work and give you the elapsed time in seconds... ofcourse there are other ways of doing this. I myself, am not aware of how you might be able to store a list in a textbox. I have always used listbox for lists.  I hope this helps,

-KC
I think I may have misread your question, if you meant to store a list of the  different times and not the difference in times  then you will use one textbox along with the listbox and command button.
use the listbox to store the different times. Something as simple as this is really all you need.

Option Explicit

Private Sub Form_Load()
   Text1.Text = Format$(Now, "long time")  
End Sub

Private Sub Command1_Click()
   List1.AddItem Format$(Now, "long time")
End Sub
 
as for storing the values somewhere permanent, there are many ways to do this. For e.g., if you wanted to store your list in a Word Document, you would need to go to Project from your VB menu, select references and choose the MS Word Object Lib. and use Automation or some other method to move the items in the list box to the doc. I saw some sample code at the following address:
http://www.developerfusion.com/show/1724/

just to give you some ideas. Is any of this helpful to you?

-KC
Sue33,

I think that the solution from my last post would've helped. I used something similar to  it myself in a program. I don't know if you had a chance to review my posts, or if you came about the solution in a different manner, but if you do decide to give points, and if I what I said was helpful, I would still like to be considered.

thanks,
- KC