avoid double initiation of form when form variable referenced from Module
With Respect To: UserForm_Initialize() is automatically called when a form variable is accessed in other module
I saw this article/topic on this forum and thought I might get some help on a related issue.
Using Excel spreadsheet to track performance, ie average shot time. Created userform Timer which behaves as a stopwatch...On User from, Click Start Timer starts and displays time ticking in seconds, Click Reset Timer records elapsed seconds, resets timer to zero, and timer continues to count elapsed seconds. When form is executed using F5 from the form, all code executes as desired. When I put a button on the spreadsheet and open the form using a macro called Timer, the code executes undesirably, ie first mention of form variable in ModTimer causes reinitializing of form (see italicized text below), instead of executing next statement. How do I stop this reinitializing of the form?
ModMain (Code)
Option Explicit
Dim Interval As Date, elapsedtime As Date, datShotTime As Date
Public Sub Timer()
'Stop
Dim frmInstance As frmMatch
Set frmInstance = New frmMatch
frmInstance.Show
Set frmInstance = Nothing
```End Sub
**frmMatch (Code)**
Private Sub butStartTimer_Click()
Call StartTimer
Private Sub butStopTimer_Click()
Call StopTimer
Private Sub butResetTimer_Click()
Call ResetTimer
Private Sub butClose_Click()
Unload Me
End
Private Sub spinMatch_Change()
datMatch.Value = spinMatch.Value
Private Sub spinGame_Change()
datGame.Value = spinGame.Value
Private Sub spinInning_Change()
datInning.Value = spinInning.Value
Private Sub UserForm_Initialize()
'Create a new id
Call CreateNewID
'Initialize the controls
Call InitializeControls
Private Sub CreateNewID()
datID.Value = GetNewID
Private Sub InitializeControls()
Me.datMatch.Value = "1"
Me.datGame.Value = "1"
Me.datInning.Value = "0"
Me.datTopInning.Value = True
Me.datShotTime.Value = "00:00:00"
**ModTimer(Code)**
Public Sub StartTimer()
Interval = Now + TimeValue("00:00:01")
elapsedtime = elapsedtime + TimeValue("00:00:01")
'Debug.Print elapsedtime, frmMatch.datShotTime.Value
datShotTime.Value = Format(elapsedtime, "hh:mm:ss") (problem occurs here, using F5 to execute, this line executes and moves to next line...calling Macro Timer, this line sends execution to UserForm_Initialize ()
Application.OnTime Interval, "StartTimer"
Public Sub ResetTimer()
'Write Data to Worksheet controls
Call WriteDataToSheet
Call RefreshPivotTables
elapsedtime = TimeValue("00:00:00")
'frmMatch.datShotTime.Value = TimeValue("00:00:00")
frmMatch.datID.Value = GetNewID
Public Sub StopTimer()
' Bug in code...if Reset is selected followed "quickly" by Stop Timer,
' programs errors out at OnTime Method of Application below
' First attempt to fix is IF-EndIF below
'If Interval <= Now Then
' Interval = Now + TimeValue("00:00:01")
'End If
Application.OnTime Interval, "StartTimer", , "False"