Argument not optional for getTickCount

Udaman

New Member
Joined
Feb 17, 2016
Messages
43
So I wanted to make a start/stop timer in Access 2016. I found this website with the code and instructions:

https://support.microsoft.com/en-us/kb/325238

My first issue was needing to convert the declaration to 64 bit. So I replaced:

Code:
[COLOR=inherit][FONT=Menlo]Private Declare Function GetTickCount Lib "kernel32" () As Long[/FONT][/COLOR]

with:

Code:
Private Declare PtrSafe Function getTickCount Lib "kernel32" Alias "QueryPerformanceCounter" (cyTickCount As Currency) As Long

That fixed that problem. But then I started to get a compile error "Argument not Optional" for the StartStop click event, and highlighted was "getTickCount ()" shown in bold in this code:

Code:
Private Sub btnStartStop_Click()If Me.TimerInterval = 0 Then
      StartTickCount = [B]getTickCount()[/B]
      Me.TimerInterval = 15
      Me!btnStartStop.Caption = "Stop"
      Me!btnReset.Enabled = False
   Else
      TotalElapsedMilliSec = TotalElapsedMilliSec + _
         (getTickCount() - StartTickCount)
      Me.TimerInterval = 0
      Me!btnStartStop.Caption = "Start"
      Me!btnReset.Enabled = True
   End If
End Sub

I am not savy enough with VBA to figure out what is not optional and how to make it so. Any suggestions?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You are not calling the GetTickCount API function. You are wrongly calling the QueryPerformanceCounter API which takes an argument

Your api declaration should be as follows :
Code:
#If VBA7 Then
    #If Win64 Then
        Declare PtrSafe Function GetTickCount Lib "kernel32" Alias "GetTickCount64" () As LongPtr
    #Else
        Declare PtrSafe Function GetTickCount Lib "kernel32" () As LongPtr
    #End If
#Else
    Declare Function GetTickCount Lib "kernel32" () As Long
#End If
and if you are declaring your variables then the variable StartTickCount should be declared as LongPtr

If you need a really high resolution timer (Micro seconds) , you should use
QueryPerformanceCounter instead of GetTickCount

Here is an example :

Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
    Private Declare PtrSafe Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Private Declare Function QueryPerformanceCounter Lib "Kernel32" (lpPerformanceCount As Currency) As Boolean
    Private Declare Function QueryPerformanceFrequency Lib "Kernel32" (lpFrequency As Currency) As Boolean
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub Test()
     Dim crStartTime As Currency
     Dim crEndTime As Currency
     Dim crFrequency As Currency
     
     If QueryPerformanceFrequency(crFrequency) Then
        QueryPerformanceCounter crStartTime
        Sleep 1000
        QueryPerformanceCounter crEndTime
        MsgBox "Elapsed time: " & vbNewLine & ((crEndTime - crStartTime) * 1000) / crFrequency & " ms"
     End If
End Sub
 
Last edited:
Upvote 0
Thank you for a quick response, but when I say I am not savy with VBA, I meant I fumble around aimlessly most of the time. Can you help show me where to put the code your suggesting? I've tried it myself and am still getting the same error.

Here is all my code:

Code:
Option ExplicitDim TotalElapsedMilliSec As Long
Dim StartTickCount As Long
Private Declare PtrSafe Function getTickCount Lib "kernel32" Alias "QueryPerformanceCounter" (cyTickCount As Currency) As Long


Private Sub btnReset_Click()
TotalElapsedMilliSec = 0
   Me!ElapsedTime = "00:00:00:00"
End Sub


Private Sub btnStartStop_Click()
If Me.TimerInterval = 0 Then
      StartTickCount = getTickCount()
      Me.TimerInterval = 15
      Me!btnStartStop.Caption = "Stop"
      Me!btnReset.Enabled = False
   Else
      TotalElapsedMilliSec = TotalElapsedMilliSec + _
         (getTickCount() - StartTickCount)
      Me.TimerInterval = 0
      Me!btnStartStop.Caption = "Start"
      Me!btnReset.Enabled = True
   End If
End Sub


Private Sub Form_Timer()


 Dim Hours As String
   Dim Minutes As String
   Dim Seconds As String
   Dim MilliSec As String
   Dim Msg As String
   Dim ElapsedMilliSec As Long


   ElapsedMilliSec = (getTickCount() - StartTickCount) + _
      TotalElapsedMilliSec


   Hours = Format((ElapsedMilliSec \ 3600000), "00")
   Minutes = Format((ElapsedMilliSec \ 60000) Mod 60, "00")
   Seconds = Format((ElapsedMilliSec \ 1000) Mod 60, "00")
   MilliSec = Format((ElapsedMilliSec Mod 1000) \ 10, "00")


   Me!ElapsedTime = Hours & ":" & Minutes & ":" & Seconds & ":" _
      & MilliSec
      
End Subb
 
Upvote 0
I don't know why everybody wants to use API for this task. Is there a problem with vba timer?
What I usually use for this purpose is:
Code:
dim timStart as variant
timStart = vba.DateTime.Timer*1000
......
totalTime = vba.DateTime.Timer*1000 - timStart 'this is in ms
If anyone can say: is it better to use an API call than VBA?
 
Upvote 0
I don't need anything real fancy. All I am trying to accomplish is to allow people to start and stop a task, while having a timer visible as a reminder that they are indeed still on task. In the end it's the start and stop time that I care about most. I have been successful in creating this on my own, but it's just this darn timer that I am having issues with.
 
Upvote 0

Forum statistics

Threads
1,221,813
Messages
6,162,117
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top