Discussion:
Win8 & VB6
(too old to reply)
GS
2014-08-28 21:55:08 UTC
Permalink
I have a situation where the following code for a Form raises an error
(#5: Invalid procedure call or argument) running under Win8, but on XP
to Win7 it works fine...

Private Sub Form_Load()
With Me
.Caption = gsAPP_NAME ': .Icon = fAbout.Icon
.btnRegisterApp(LicenseReg.Validate).Enabled = _
(Me.txtLicenseKey <> "")
On Error Resume Next: .txtLicenseKey.SetFocus
End With
End Sub

..where changing as follows works without error...

Private Sub Form_Load()
With Me
.Caption = gsAPP_NAME ': .Icon = fAbout.Icon
.btnRegisterApp(LicenseReg.Validate).Enabled = _
(Me.txtLicenseKey <> "")
On Error Resume Next: Me.txtLicenseKey.SetFocus
End With
End Sub

Does anyone have any idea why \win8 throws an error?
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
John K.Eason
2014-08-28 22:58:00 UTC
Permalink
*Date:* Thu, 28 Aug 2014 17:55:08 -0400
I have a situation where the following code for a Form raises an
error (#5: Invalid procedure call or argument) running under Win8,
but on XP to Win7 it works fine...
Private Sub Form_Load()
With Me
.Caption = gsAPP_NAME ': .Icon = fAbout.Icon
.btnRegisterApp(LicenseReg.Validate).Enabled = _
(Me.txtLicenseKey <> "")
On Error Resume Next: .txtLicenseKey.SetFocus
End With
End Sub
..where changing as follows works without error...
Private Sub Form_Load()
With Me
.Caption = gsAPP_NAME ': .Icon = fAbout.Icon
.btnRegisterApp(LicenseReg.Validate).Enabled = _
(Me.txtLicenseKey <> "")
On Error Resume Next: Me.txtLicenseKey.SetFocus
End With
End Sub
Does anyone have any idea why \win8 throws an error?
I'm quite surprised it works at all TBH. Trying to SetFocus to a control in
Form_Load causes an error because the control isn't fully loaded until it's visible.
If I have to SetFocus to a control when the form is loaded, I do it in the
Form_Activate event.
Have a look at the "Form_Load vs. Form_Activate" setion of this web page:
http://www.vb6.us/tutorials/understanding-forms-vb6-tutorial

Regards
John (***@jeasonNoSpam.cix.co.uk) Remove the obvious to reply...
GS
2014-08-29 00:46:27 UTC
Permalink
Post by John K.Eason
I'm quite surprised it works at all TBH. Trying to SetFocus to a control in
Form_Load causes an error because the control isn't fully loaded until it's visible.
If I have to SetFocus to a control when the form is loaded, I do it in the
Form_Activate event.
http://www.vb6.us/tutorials/understanding-forms-vb6-tutorial
Regards
Hi John,
That makes sense! I'll look at switching that around so I can remove
the OERN. The only reason this code is 'as is' was to make it portabe
between VB6 and VBA. Using the _Activate event may upset the focus set
to other controls based on user interaction. It might be better to move
this into the caller right after .Show, then!

I appreciate your input...
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2014-08-29 00:49:07 UTC
Permalink
Oops.., typo!
Post by GS
That makes sense! I'll look at switching that around so I can remove
the OERN. The only reason this code is 'as is' was to make it
portable
Post by GS
between VB6 and VBA. Using the _Activate event may upset the focus
set to other controls based on user interaction. It might be better
to move this into the caller right after .Show, then!
Also, VBA uses an _Initialize event and SetFocus works just fine!
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2014-08-29 00:58:31 UTC
Permalink
Bingo! Form_Activate gets it done much better!
Big thanks...
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
John K.Eason
2014-08-29 11:16:00 UTC
Permalink
Post by GS
Bingo! Form_Activate gets it done much better!
Big thanks...
As both the article and obiwan say, don't forget a 'form loading' semaphore or you
could end up with unexpected results!

Regards
John (***@jeasonNoSpam.cix.co.uk) Remove the obvious to reply...
GS
2014-08-29 11:42:56 UTC
Permalink
Post by John K.Eason
Post by GS
Bingo! Form_Activate gets it done much better!
Big thanks...
As both the article and obiwan say, don't forget a 'form loading'
semaphore or you could end up with unexpected results!
Regards
Yes.., I agree! Thanks again for contributing further to my learning
process.
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
ObiWan
2014-08-29 07:53:19 UTC
Permalink
:: On Thu, 28 Aug 2014 17:55:08 -0400
:: (comp.lang.basic.visual.misc,microsoft.public.vb.general.discussion)
Post by GS
Does anyone have any idea why \win8 throws an error?
when the load event fires, you aren't guaranteed that all the controls
on the form have already been drawn and in some cases the setfocus
throws an error so it's common practice moving the "setfocus" into the
"Activate" event; to avoid repeating the setfocus over and over just
add a static boolean variable and use it as a semaphore; for example

Sub Form_Activate()
Static bFirstTime As Boolean

If Not bFirstTime Then
bFirstTime = True
txtLicenseKey.SetFocus
End If
End Sub
GS
2014-08-29 09:37:10 UTC
Permalink
Post by ObiWan
Post by GS
Post by ObiWan
On Thu, 28 Aug 2014 17:55:08 -0400
(comp.lang.basic.visual.misc,microsoft.public.vb.general.discussion)
Does anyone have any idea why \win8 throws an error?
when the load event fires, you aren't guaranteed that all the
controls on the form have already been drawn and in some cases the
setfocus throws an error so it's common practice moving the
"setfocus" into the "Activate" event; to avoid repeating the setfocus
over and over just add a static boolean variable and use it as a
semaphore; for example
Sub Form_Activate()
Static bFirstTime As Boolean
If Not bFirstTime Then
bFirstTime = True
txtLicenseKey.SetFocus
End If
End Sub
Thank you.., that's a great idea! As it happens, the code is about 10
years old but the Form has been revised from a multi-use component to a
dedicated one. That means my concern for repeating SetFocus in this
case is now mute.

When the Form was multi-use I was managing 'pages' using frame controls
and a 'ShowPage' routine. Each 'page' was used in a specific 'AppMode'
state which was checked each time the form was loaded. (No
multi-instancing)

I use the same code and form design in my VBA projects and so is why
the OERN statement persisted. VBA uses an _Initialize event (instead of
_Load) and so behaves differently in that SetFocus works without error.
Now that I have recently made identical frm components for both VB6/VBA
projects, I can update the component code specific to each. Both still
use the same .bas component and so are named the same in either project
environment.

Initially, both VB6/VBA projects used the same component for app
startup where my VBA projects use a VB6.exe 'frontloader' to load the
VBA project into an automated instance of MSO Excel. In the VB6 apps
the user could always enter a LicenseKey at runtime (when LicenseStatus
= TrialVer) as well as during startup. The VBA apps (inconveniently)
could only do this during startup (via the frontloader) until recently
when I added a MSO Userform duplicate of the VB6 Form. Both use the
presence of a value stored in the 'app.ini' file to determine license
'state'; a blank value sets LicenseStatus to 0 (TrialVer), and a
non-blank value sets to 1 (Licensed). The ini file gets read at startup
in InitGlobals(), and so this now happens twice with VBA projects. Both
can reset this during runtime so next startup knows how to configure
the workspace to hide the 'Register...' menuitem used to display the
frm component. The VBA project does not check if it's licensed or not
because the only way it runs is if its frontloader determines a valid
LicenseStatus (ie: NOT 'Expired')
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS
2014-08-29 09:44:17 UTC
Permalink
I posted incorrect values for the LicenseStatus...

Expired=0
TrialVer=1
Licensed=2
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
ObiWan
2014-08-29 10:59:48 UTC
Permalink
:: On Fri, 29 Aug 2014 05:37:10 -0400
:: (comp.lang.basic.visual.misc,microsoft.public.vb.general.discussion)
Post by GS
Post by ObiWan
it as a semaphore; for example
Sub Form_Activate()
Static bFirstTime As Boolean
If Not bFirstTime Then
bFirstTime = True
txtLicenseKey.SetFocus
End If
End Sub
Thank you.., that's a great idea! As it happens, the code is about 10
years old but the Form has been revised from a multi-use component to
just in case, change the code this way

Sub Form_Activate()
Static bInitFlag As Boolean

' enter only once
If bInitFlag Then Exit Sub
bInitFlag = True

' add here your one time code
txtLicenseKey.SetFocus
'....more code....
End Sub


not that the other code is wrong, but I think the above is cleaner and
avoids wrapping the "one time" code inside the "if" block :)
GS
2014-08-29 11:55:15 UTC
Permalink
Post by ObiWan
Post by GS
Post by ObiWan
On Fri, 29 Aug 2014 05:37:10 -0400
(comp.lang.basic.visual.misc,microsoft.public.vb.general.discussion)
it as a semaphore; for example
Sub Form_Activate()
Static bFirstTime As Boolean
If Not bFirstTime Then
bFirstTime = True
txtLicenseKey.SetFocus
End If
End Sub
Thank you.., that's a great idea! As it happens, the code is about
10 years old but the Form has been revised from a multi-use
component to
just in case, change the code this way
Sub Form_Activate()
Static bInitFlag As Boolean
' enter only once
If bInitFlag Then Exit Sub
bInitFlag = True
' add here your one time code
txtLicenseKey.SetFocus
'....more code....
End Sub
not that the other code is wrong, but I think the above is cleaner
and avoids wrapping the "one time" code inside the "if" block :)
Either example still evals an If...Then so I'm going to say that
'cleaner' to me means the 1st example because it more concisely does
the job!<g>... This example is better (IMO) where the code that follows
the check is lengthy, in which case I prefer to exit early.
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Loading...