Excel VBA Simulate a Command's _Click

Josh Hazel
05-15-2008, 08:20 AM
I have two command buttons on a form, A & B
Press A and A_Click () code runs
Press B and B_Click () code runs

I have a value in B_Click () that says if x = y then A_Click (to run sub A_Click code)

When x =y it seems to start working and goes to the A_Click sub, but the code within A_Click doesnt seem to run..? However, when I manually point my mouse and click on A_Click, the code within A_Click sub will run.

What could I be doing wrong here?

Cas
05-15-2008, 08:56 AM
The way you describe should work fine. Can you post your code?

Josh Hazel
05-16-2008, 12:50 AM
The way you describe should work fine. Can you post your code?

I will try and post the relevant subs... Sorry about the length, there is a lot of if/then style statements.

This is the start button... when clicked it should disable key items, such as frames/text boxes/combo boxes...

Private Sub cmdStartTime_Click()

If Len(txtDOBMonth.Text) <> 2 Or Len(txtDOBDay.Text) <> 2 Or Len(txtDOBYear.Text) <> 4 Or Len(txtEEID.Text) <> 6 Then
chkAutoLogTimes.Value = False
End If

If optStopWatchSelect.Value = True Or optTimerSelect.Value = True Then
StopManual = False
TestD8Value = Format(Range("c8").Value, "hh:mm:ss")
If txtTimerStopWatch.Text = "00:00:00" And TimerOrStopWatch = "Timer" Then
Dim TimerValueZero
TimerValueZero = MsgBox("Timer value cannot be equal to 0." & vbNewLine & "Enter valid time", vbOKOnly, "Invalid Time")
Exit Sub

Else
txtTimerStopWatch.Enabled = False

If TimerOrStopWatch = "Timer" Then
stopped = False
Else
stopped = False
End If
End If

ElseIf optTimesheetSelect.Value = True Then
StartTestAllValues
Else
End If

frameTimesheet.Enabled = False
frameHoursWorked.Enabled = False
frameTimerQuickTimeSelect.Enabled = False

cmdStartTime.Enabled = False
cmdStopTimerStopWatch.Enabled = True

If optTimesheetSelect.Value = True Or optTimerSelect.Value = True Then
cmdResetTimerStopwatch.Enabled = False
Else
cmdResetTimerStopwatch.Enabled = True
End If

cmdResetTimerStopwatch.Enabled = False

If optStopWatchSelect.Value = True Or optTimerSelect.Value = True Then StartTimerStopWatchTestValue

frameTimerStopWatchSelect.Enabled = False


End Sub

Part of the Start button command tests certain values (FYI, selected value is Timesheet, not Timer or StopWatch), such as if two text boxes contain the same date, or less than - it would trigger the stop button to reset everything.

Private Sub cmdStopTimerStopWatch_Click()
stopped = True
StopManual = True
CompClockRun = False
If optTimerSelect.Value = True Or optStopWatchSelect.Value = True Then
txtTimerStopWatch.Enabled = True
txtTimerStopWatch.BackColor = &H8000000F
cmdStartTime.Enabled = True
End If

If optTimesheetSelect.Value = True Then
frameTimesheet.Enabled = True
'chkLogTime1.Enabled = True
'chkLogTime2.Enabled = True
'chkLogTime3.Enabled = True
'chkLogTime4.Enabled = True
Else
End If

cmdStopTimerStopWatch.Enabled = False

If TimerOrStopWatch = "StopWatch" Then
cmdResetTimerStopwatch.Enabled = True
End If

frameTimerStopWatchSelect.Enabled = True

If optTimerSelect.Value = True Then frameTimerQuickTimeSelect.Enabled = True

cmdStartTime.Enabled = True

frameTimerStopWatchSelect.Enabled = True

If optTimesheetSelect.Value = True Then frameHoursWorked.Enabled = True

End Sub

Josh Hazel
05-16-2008, 12:51 AM
The following code runs from the Start button (u should see references)

Sub StartTimerStopWatchTestValue()

Dim strTSWTV As String
strTSWTV = txtTimerStopWatch

Dim intTimerHours As Integer
Dim intTimerMinutes As Integer
Dim intTimerSeconds As Integer

Dim intTimerHoursLocStart As Integer
Dim intTimerMinutesLocStart As Integer
Dim intTimerSecondsLocStart As Integer

intTimerHoursLocStart = 1
intTimerMinutesLocStart = InStr(1, strTSWTV, ":") + 1
intTimerSecondsLocStart = InStr(1, (Mid(strTSWTV, intTimerMinutesLocStart, 20)), ":") + intTimerMinutesLocStart

Dim intTimerHoursLocEnd As Integer
Dim intTimerMinutesLocEnd As Integer
Dim intTimerSecondsLocEnd As Integer

intTimerHoursLocEnd = InStr(1, strTSWTV, ":") - 1
intTimerMinutesLocEnd = InStr(1, Mid(strTSWTV, intTimerHoursLocEnd, 20), ":") + intTimerMinutesLocStart - 1
intTimerSecondsLocEnd = Len(strTSWTV)

Dim intHoursDiff As Integer
Dim intMinutesDiff As Integer
Dim intSecondsDiff As Integer

intHoursDiff = intTimerHoursLocEnd - intTimerHoursLocStart + 1
intMinutesDiff = intTimerMinutesLocEnd - intTimerMinutesLocStart + 1
intSecondsDiff = intTimerSecondsLocEnd - intTimerSecondsLocStart + 1

Dim strTimerHourVal As String
Dim strTimerMinuteVal As String
Dim strTimerSecondVal As String

strTimerHourVal = Mid(strTSWTV, intTimerHoursLocStart, intHoursDiff)
strTimerMinuteVal = Mid(strTSWTV, intTimerMinutesLocStart, intMinutesDiff)
strTimerSecondVal = Mid(strTSWTV, intTimerSecondsLocStart, intSecondsDiff)

Dim msgHourError
Dim msgMinuteError
Dim msgSecondError

If Not Len(strTimerHourVal) = 2 Then
msgHourError = MsgBox("Hours must be 2 numbers only (ie 00)", vbOKOnly, "Invalid Hour")
cmdStopTimerStopWatch
ElseIf Not Len(strTimerMinuteVal) = 2 Then
msgHourError = MsgBox("Minutes must be 2 numbers only (ie 00)", vbOKOnly, "Invalid Minute")
cmdStopTimerStopWatch_Click
ElseIf Not Len(strTimerSecondVal) = 2 Then
msgHourError = MsgBox("Minutes must be 2 numbers only (ie 00)", vbOKOnly, "Invalid Minute")
cmdStopTimerStopWatch_Click
Else
End If





End Sub
Sub StartTestAllValues()

If chkLogIn.Value = True And txtLogIn.Enabled = True And IsDate(txtLogIn) = False Then GoTo StartTestAllValuesError
If chkBreak1Out.Value = True And txtBreak1Out.Enabled = True And IsDate(txtBreak1Out) = False Then GoTo StartTestAllValuesError
If chkBreak1In.Value = True And txtBreak1In.Enabled = True And IsDate(txtBreak1In) = False Then GoTo StartTestAllValuesError
If chkLunch1Out.Value = True And txtLunch1Out.Enabled = True And IsDate(txtLunch1Out) = False Then GoTo StartTestAllValuesError
If chkLunch1In.Value = True And txtLunch1In.Enabled = True And IsDate(txtLunch1In) = False Then GoTo StartTestAllValuesError
If chkBreak2Out.Value = True And txtBreak2Out.Enabled = True And IsDate(txtBreak2Out) = False Then GoTo StartTestAllValuesError
If chkBreak2In.Value = True And txtBreak2In.Enabled = True And IsDate(txtBreak2In) = False Then GoTo StartTestAllValuesError
If chkLogOut.Value = True And txtLogOut.Enabled = True And IsDate(txtLogOut) = False Then GoTo StartTestAllValuesError
If chkBreak3Out.Value = True And txtBreak3Out.Enabled = True And IsDate(txtBreak3Out) = False Then GoTo StartTestAllValuesError
If chkBreak3In.Value = True And txtBreak3In.Enabled = True And IsDate(txtBreak3In) = False Then GoTo StartTestAllValuesError
If chkLunch2Out.Value = True And txtLunch2Out.Enabled = True And IsDate(txtLunch2Out) = False Then GoTo StartTestAllValuesError
If chkLunch2In.Value = True And txtLunch2In.Enabled = True And IsDate(txtLunch2In) = False Then GoTo StartTestAllValuesError
If chkLogTime1.Value = True And txtLogTime1.Enabled = True And IsDate(txtLogTime1) = False Then GoTo StartTestAllValuesError
If chkLogTime2.Value = True And txtLogTime2.Enabled = True And IsDate(txtLogTime2) = False Then GoTo StartTestAllValuesError
If chkLogTime3.Value = True And txtLogTime3.Enabled = True And IsDate(txtLogTime3) = False Then GoTo StartTestAllValuesError
If chkLogTime4.Value = True And txtLogTime4.Enabled = True And IsDate(txtLogTime4) = False Then GoTo StartTestAllValuesError

GoTo SkipStartTestAllValues

StartTestAllValuesError:
Dim strStartTestAllValuesError As String
strStartTestAllValuesError = MsgBox("Check values entered in timesheet, all times " & vbNewLine & _
"must be entered in 00:00 AM/PM (hh:mm AM/PM) format.", vbOKOnly, "Invalid Value")
cmdStopTimerStopWatch_Click
Exit Sub

SkipStartTestAllValues:

If optTimesheetSelect.Value = True Then TimsheetTestTimesheetOrderStep1

End Sub
Sub TimsheetTestTimesheetOrderStep1()

Dim tMilLogIn, tMilBreak1Out, tMilBreak1In, tMilLunch1Out, tMilLunch1In, tMilBreak2Out, tMilBreak2In, tMilBreak3Out, tMilBreak3In, tMilLunch2Out, tMilLunch2In, tMilLogTime1, tMilLogTime2, tMilLogTime3, tMilLogTime4, tMilLogOut As Date

If chkLogIn = True Then tMilLogIn = CDate(txtLogIn) ', "h:mm")
If chkBreak1Out = True Then tMilBreak1Out = CDate(txtBreak1Out) ', "h:mm")
If chkBreak1In = True Then tMilBreak1In = CDate(txtBreak1In) ', "h:mm")
If chkLunch1Out = True Then tMilLunch1Out = CDate(txtLunch1Out) ', "h:mm")
If chkLunch1In = True Then tMilLunch1In = CDate(txtLunch1In) ', "h:mm")
If chkBreak2Out = True Then tMilBreak2Out = CDate(txtBreak2Out) ', "h:mm")
If chkBreak2In = True Then tMilBreak2In = CDate(txtBreak2In) ', "h:mm")
If chkBreak3Out = True Then If chkBreak3Out = True Then tMilBreak3Out = CDate(txtBreak3Out) ', "h:mm")
If chkBreak3In = True Then tMilBreak3In = CDate(txtBreak3In) ', "h:mm")
If chkLunch2Out = True Then tMilLunch2Out = CDate(txtLunch2Out) ', "h:mm")
If chkLunch2In = True Then tMilLunch2In = CDate(txtLunch2In) ', "h:mm")
If chkLogTime1 = True Then tMilLogTime1 = CDate(txtLogTime1) ', "h:mm")
If chkLogTime2 = True Then tMilLogTime2 = CDate(txtLogTime2) ', "h:mm")
If chkLogTime3 = True Then tMilLogTime3 = CDate(txtLogTime3) ', "h:mm")
If chkLogTime4 = True Then tMilLogTime4 = CDate(txtLogTime4) ', "h:mm")
If chkLogOut = True Then tMilLogOut = CDate(txtLogOut) ', "h:mm")

Dim blnMilLogIn, blnMilBreak1Oubln, blnMilBreak1In, blnMilLunch1Oubln, blnMilLunch1In, blnMilBreak2Oubln, blnMilBreak2In, blnMilBreak3Oubln, blnMilBreak3In, blnMilLunch2Oubln, blnMilLunch2In, blnMilLogblnime1, blnMilLogblnime2, blnMilLogblnime3, blnMilLogblnime4, blnMilLogOut As Boolean

blnMilLogIn = chkLogIn
blnMilBreak1out = chkBreak1Out
blnMilBreak1In = chkBreak1In
blnMilLunch1out = chkLunch1Out
blnMilLunch1In = chkLunch1In
blnMilBreak2Out = chkBreak2Out
blnMilBreak2In = chkBreak2In
blnMilBreak3out = chkBreak3Out
blnMilBreak3In = chkBreak3In
blnMilLunch2out = chkLunch2Out
blnMilLunch2In = chkLunch2In
blnMilLogTime1 = chkLogTime1
blnMilLogTime2 = chkLogTime2
blnMilLogTime3 = chkLogTime3
blnMilLogTime4 = chkLogTime4
blnMilLogOut = chkLogOut

Cas
05-16-2008, 01:19 AM
If Not Len(strTimerHourVal) = 2 Then
msgHourError = MsgBox("Hours must be 2 numbers only (ie 00)", vbOKOnly, "Invalid Hour")
cmdStopTimerStopWatch
ElseIf Not Len(strTimerMinuteVal) = 2 Then
msgHourError = MsgBox("Minutes must be 2 numbers only (ie 00)", vbOKOnly, "Invalid Minute")
cmdStopTimerStopWatch_Click
ElseIf Not Len(strTimerSecondVal) = 2 Then
msgHourError = MsgBox("Minutes must be 2 numbers only (ie 00)", vbOKOnly, "Invalid Minute")
cmdStopTimerStopWatch_Click
Else
End If

There's a "_Click" missing in the first Then-block, could it be as simple as that?

Josh Hazel
05-16-2008, 07:55 AM
And the last bit...

Dim mLogIn, mBreak1Out, mBreak1In, mLunch1Out, mLunch1In, mBreak2Out, mBreak2In, mBreak3Out, mBreak3In, mLunch2Out, mLunch2In, mLogmime1, mLogmime2, mLogmime3, mLogmime4, mLogOut As String

If blnMilLogIn = False Or blnMilLogOut = False Or (tMilLogOut < tMilLogIn) Then
mLogIn = MsgBox("Check LogIn/LogOut time. " & vbNewLine & "Start time cannot be less than end time." & "LogIn and LogOut time must be turned on (checked).", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilBreak1out = True And (tMilBreak1Out <= tMilLogIn) Then
mBreak1Out = MsgBox("Break 1 Out time cannot be less than Log In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilBreak1In = True And (tMilBreak1In <= tMilBreak1Out) Then
mBreak1In = MsgBox("Break 1 In time cannot be less than Break 1 Out time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch.Enabled = True
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilLunch1out = True And (tMilLunch1Out <= tMilBreak1In) Then
mLunch1Out = MsgBox("Lunch 1 Out time cannot be less than Break 1 In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilLunch1In = True And (tMilLunch1In <= tMilLunch1Out) Then
mLunch1In = MsgBox("Lunch 1 In time cannot be less than Lunch 1 Out time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilBreak2Out = True And (tMilBreak2Out <= tMilLunch1In) Then
mBreak2Out = MsgBox("Break 2 Out time cannot be less than Lunch 1 In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilBreak2In = True And (tMilBreak2In <= tMilBreak2Out) Then
tMilBreak2In = MsgBox("Break 2 In time cannot be less than Break 2 Out time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If opt10Hour.Value = True Then
If tMilLogOut <= tMilBreak2In Then
mLogOut = MsgBox("Log Out time cannot be less than Break 2 In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
End If
If opt10Hour.Value = True Then
If blnMilBreak3out = True And (tMilBreak3Out <= tMilBreak2In) Then
mBreak3Out = MsgBox("Break 3 Out time cannot be less than Break 2 In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilBreak3In = True And (tMilBreak3In <= tMilBreak3Out) Then
mBreak3In = MsgBox("Break 3 In time cannot be less than Break 3 Out time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If tMilLogOut <= tMilBreak3In Then
mLogOut = MsgBox("Log Out time cannot be less than Break 3 In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
End If
If opt12Hour.Value = True Then
If blnMilLunch2out = True And (tMilLunch2Out <= tMilBreak2In) Then
mLunch2Out = MsgBox("Lunch 2 Out time cannot be less than Break 2 In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilLunch2In = True And (tMilLunch2In <= tMilLunch2Out) Then
mLunch2In = MsgBox("Lunch 2 In time cannot be less than Lunch 2 Out time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilBreak3out = True And (tMilBreak3Out <= tMilLunch2In) Then
mBreak3Out = MsgBox("Break 3 Out time cannot be less than Lunch 2 In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If blnMilBreak3In = True And (tMilBreak3In <= tMilBreak3Out) Then
mBreak3In = MsgBox("Break 3 In time cannot be less than Break 3 Out time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
If tMilLogOut <= tMilBreak3In Then
mLogOut = MsgBox("Log Out time cannot be less than Break 3 In time.", vbokayonly, "Invalid Timesheet Settings")
cmdStopTimerStopWatch_Click
Exit Sub
End If
End If


End Sub[/CODE]

Josh Hazel
05-16-2008, 07:58 AM
Good catch, I updated the _Click, unfortunately that wasnt the problem. Still not working, that part of the sub only runs when the optStopWatchSelect.value = true

In this case Im testing for when the optTimesheetSelect.value = true

Cas
05-16-2008, 08:07 AM
Okay, could you describe again exactly what happens when you step through the code? Does it highlight the _Click call, then the _Click header, then the line below the _Click call, or is the sequence different?

Josh Hazel
05-16-2008, 08:19 AM
Maybe this triggers a thought.

When running, I set the break 1 out and break 1 in value the same, so it should trigger the msg box to say it cannot be equal, then run the stop_click button.

That part works, the message pops up, (its a vbokonly msgbox) and then it DOES go to the stop_click button's sub routine.

So the very first line of the sub routine (in the stop button_click) i entered frameSelectTimerMode.enabled = true (btw, yes, i renamed the frame, since posting the code above, i changed all old frame names to this frame name)

However, once sub routine for stop_click button is complete, the frameSelectTimerMode is still disabled, along with many of things that should have been enabled.

Cas
05-16-2008, 08:57 AM
and then it DOES go to the stop_click button's sub routine [...] the very first line of the sub routine (in the stop button_click) i entered frameSelectTimerMode.enabled = true [...] However, once sub routine for stop_click button is complete, the frameSelectTimerMode is still disabled, along with many of things that should have been enabled.
So, what you're saying is that the problem isn't that the sub doesn't run but that the changes it should make to the controls' properties don't materialize, yes? If I understood you correctly, there are two basic possibilities - either somehow the changes aren't applied at all, or they are applied but then something happens that changes them right back.

Okay, here's what you do: Put in the new procedure below. Replace all lines that set the frame's .Enabled property by a call to this procedure (not just in the ones in the suspect _Click handler, all of them). The debug output should tell us what's going on.
Private Sub setFrameSelectTimerModeEnabled(whichState As Boolean)
Debug.Print "frameSelectTimerMode.Enabled = " & whichState
FrameSelectTimerMode.Enabled = whichState
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum