avoid double initiation of form when form variable referenced from Module

Gregg Noud 0 Reputation points
2024-08-01T18:18:57.8666667+00:00

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"


Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,511 questions
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,974 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,001 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.