Solved

Recording the time a button is pressed

Posted on 2003-11-10
12
343 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 63 total points
Comment Utility

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 62 total points
Comment Utility
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
Comment Utility
on button click event
public sub button_click()
dim note as date
note = time
end sub
0
 

Expert Comment

by:KCHiggs
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
seriesUp challenge 7 106
wordsWithout 49 77
What is Python programming? 3 62
PHP question(s) about order of output 9 39
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

10 Experts available now in Live!

Get 1:1 Help Now