Solved Count Up Timer (1 Viewer)

murray83

Games Collector
Local time
Today, 15:46
Joined
Mar 31, 2017
Messages
728
Here is my code which counts down from 20 seconds and then makes the question for my quiz change

Code:
Private Sub Form_Load()

'this timer bit is for the count down on how long have left to answer the dam question
mintTimer = 20 ' 20 seconds
TimeLeft.Value = mintTimer ' show the time in a textbox
TimerInterval = 1000 ' 1 second


End Sub

Private Sub Form_Timer()

'for the coundt down for question
mintTimer = mintTimer - 1 ' count down
TimeLeft.Value = mintTimer ' show the time in a textbox
If mintTimer = 0 Then
  RememberQuestionNo = RememberQuestionNo + 1
  Call GetQuestion
  mintTimer = 21
End If

End Sub

But im trying to do the reveser and i thought would of been as easy as just flip the

Code:
mintTimer = mintTimer - 1 ' count down

from a minus to a plus and of course give it a new name so i did this

Code:
Private Sub Form_Load()

'this timer bit is for the count down on how long have left to answer the dam question
mintTimer = 20 ' 20 seconds
TimeLeft.Value = mintTimer ' show the time in a textbox
TimerInterval = 1000 ' 1 second


'this timer bit is for the count on how long you have taken for the whole quiz
mintTimer2 = 0 ' 0 seconds
txtYouHaveTaken.Value = mintTimer2 ' show the time in a textbox
TimerInterval = 1000 ' 1 second

End Sub

Private Sub Form_Timer()

'for how long taken
mintTimer2 = mintTimer2 + 1 ' count up
txtYouHaveTaken.Value = mintTimer2 ' show the time in a textbox

'for the coundt down for question
mintTimer = mintTimer - 1 ' count down
TimeLeft.Value = mintTimer ' show the time in a textbox
If mintTimer = 0 Then
  RememberQuestionNo = RememberQuestionNo + 1
  Call GetQuestion
  mintTimer = 21
End If



End Sub

but all it does is got from 0 to 1 and then bugger all, please help, cheers
 

plog

Banishment Pending
Local time
Today, 10:46
Joined
May 11, 2011
Messages
11,613
Scope: https://en.wikipedia.org/wiki/Scope_(computer_science)

That's the term for the context of how variables exist. You can't just use the name of a variable in multiple places and expect the computer to know you mean a variable somewhere else. I can yell "Charlie" in my house and my dog comes running. At the office, I yell "Charlie" and the janitor tells me to calm down and stop shouting at him. I yell "Charlie" at my friend's house, they all look at me weird because they have no idea what I'm doing.

Code:
Private Sub Form_Timer()

'for how long taken
mintTimer2 = mintTimer2 + 1 ' count up
txtYouHaveTaken.Value = mintTimer2 ' show the time in a textbox

'for the coundt down for question
mintTimer = mintTimer - 1 ' count down
TimeLeft.Value = mintTimer ' show the time in a textbox
If mintTimer = 0 Then
  RememberQuestionNo = RememberQuestionNo + 1
  Call GetQuestion
  mintTimer = 21
End If

End Sub

Same thing with variables. For both mintTimer2 and mintTimer you use them both before you assign them a value (actually you declare and assign at the same time, which is just as bad). The first time your Sub knows anything of mintTimer2 is when you try to assign it the value of itself plus 1. But it can't add 1 to something it just learned about.

Here's where you say that you did assign it a value in Sub Form_Load(). But those are 2 different mintTimer2 variables. They know nothing of each other. Scope. Now, what you need to do is either pass the value (https://docs.microsoft.com/en-us/do...s/passing-arguments-by-value-and-by-reference) of mintTimer2 from Form_Load() to Form_Timer() or assign the value just in mintTimer2, or (the least desirable option) make mintTimer2 a global variable.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:46
Joined
May 7, 2009
Messages
19,175
did you Declare your variables?
Code:
Dim mintTimer As Integer
Dim mintTimer2 As Integer
Dim RememberQuestionNo As Integer

Private Sub Form_Load()

'this timer bit is for the count down on how long have left to answer the dam question
mintTimer = 20 ' 20 seconds
[TimeLeft].Value = mintTimer ' show the time in a textbox
TimerInterval = 1000 ' 1 second


'this timer bit is for the count on how long you have taken for the whole quiz
mintTimer2 = 0 ' 0 seconds
[txtYouHaveTaken].Value = mintTimer2 ' show the time in a textbox
'no need for this, only set it once
'TimerInterval = 1000 ' 1 second
RememberQuestionNo = 0
End Sub

Private Sub Form_Timer()
'you need to Kill the timer first
Me.TimerInterval = 0
'for how long taken
mintTimer2 = mintTimer2 + 1 ' count up
txtYouHaveTaken.Value = mintTimer2 ' show the time in a textbox

'for the coundt down for question
mintTimer = mintTimer - 1 ' count down
TimeLeft.Value = mintTimer ' show the time in a textbox
If mintTimer = 0 Then
  RememberQuestionNo = RememberQuestionNo + 1
  Call GetQuestion
  mintTimer = 20
End If
'replace 20 below with the number of Questions you have
If RememberQuestionNo <= 20 Then
    Me.TimerInterval = 1000
End If
End Sub
also, you add a Button on the form to Advance to next question
(thereby resetting the Timer) if he already finished the question fast (are you going to bore
him to wait till 20 sec has elapsed before he will be presented
with new question).
 

murray83

Games Collector
Local time
Today, 15:46
Joined
Mar 31, 2017
Messages
728
cheers guys for the pointers will look at them in a mo, and to answer your question arnelgp, no if he finishes the question before 20 sec is up no need to wait.

the 20 sec countdown in that respect is a form of anti cheating so cant go and google the question in the 20 sec you have to read and type it in
 

Users who are viewing this thread

Top Bottom