Solved

Recording the time a button is pressed

Posted on 2003-11-10
12
360 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:sue33
  • 3
  • 2
  • 2
  • +2
12 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9715139
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
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9715203
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

0
 

Author Comment

by:sue33
ID: 9715750
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.

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 63 total points
ID: 9715818

Private tStart As Date

Private Sub Form_Load()
    tStart = Now
End Sub

Private Sub Command1_Click()
    Dim tCurrent As Date
    tCurrent = Now
   
    txtTimeInterval.Text = DateDiff("S",tCurrent,tStart)

' or
'   txtTimeInterval = Second(tCurrent - tStart)

End Sub
 
AW
 
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 62 total points
ID: 9715866
Add listbox List1 to the form and run the corrected version (previous was wrong):

Option Explicit

Private Declare Function GetTickCount Lib "kernel32" () As Long


Private nStart As Long

Private Sub Form_Load()
    nStart = GetTickCount()
End Sub

Private Sub Command1_Click()
    Dim n As Long
   
    n = GetTickCount() - nStart
   
    List1.AddItem CStr(CInt(n / 1000))
End Sub

If you want to show the time in the text box, write something like this:

Text1.Caption = CStr(CInt(n / 1000))
0
 

Expert Comment

by:khubpk
ID: 9715883
on button click event
public sub button_click()
dim note as date
note = time
end sub
0
 

Expert Comment

by:KCHiggs
ID: 9723275
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
0
 

Expert Comment

by:KCHiggs
ID: 9724910
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
0
 

Expert Comment

by:KCHiggs
ID: 10335317
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  
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

856 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