set focus requires open VBE

  • Thread starter Örjan Skoglösa
  • Start date
Ö

Örjan Skoglösa

In a userform I do have the follwing:

If förstaGången Then
With comboInled
.SetFocus
.SelStart = 0
.SelLength = 100
End With
Else
With comboPatNr
.SetFocus
.SelStart = 0
.SelLength = 10
End With
End If

(comboInled and comboPatnr are dropdowns on the form)

The first time the routines runs, "förstaGången" is True and
"comboInled" is selected. Works perfectly.

From the next time the routine runs "förstaGången" is False but
"comboPatNr" is not selected no matter how may times the routine runs.

But then I press Alt+F11 and then Alt+F11 again. After that I run once
more without desired result, but from the second run it works ok.

I do not need to touch anything inside the VBE, it just automatically
shows the userform code and then I toggle back and start the macro.

To be honest I have not really started to search for any error, as it
is not so important, but perhaps someone could suggest something to
look out for.

TIA
Örjan Skoglösa

PS. I use Word97 on Win2K.
 
J

Jonathan West

Hi Örjan

The problem might be that VBA is going too fast for the screen to keep
itself updated. try putting a few DoEvents commands (it often requires more
then one) into the routine immediately after the second End With statement.
DoEvents tells Windows to stop running the macro briefly and go and check
the Windows message queue (mostly for commands to update the ecreen)
 
Ö

Örjan Skoglösa

Hi Jonathan,

(Sorry for the delayed reply, but I have been "offline" for some
days.)

I guess this is a stupid question, but what kind of event should I
use?

If have tried to insert empty "Do -> Loop" statements, but that does
not show any effect.

TIA
Örjan
 
H

Helmut Weber

Hi Örjan,
doevents is nothing specific, just see help for "doevents".
I guess, Jonathan means something like:
for i = 1 to x
doevents
next
You may also try to combine "doevents" with "sleep".
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds
As Long).
Beware of possible linebreaks.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
Ö

Örjan Skoglösa

Hi Helmut,

Thanks for your explanation. I thought the command was "DoEvent" and
tried to find that. Now I get it.

Sadly, it does not help. Nor does your complementary advice, at least
if I got it right (see below*).

Perhaps I did not explain correctly.

If I start Word, open a new document on the template with the macro
and then run the macro, the selection in the else clause will never be
executed.

If I start Word, open a new document on the template with the macro
but then (just) open the macro editor before I run the macro, the
selection in the else clause will not be executed the first time it
should, but after that as it is intended.

If I then close the editor again, the selection will be executed as it
should one more time, but after that not any more.

==============

I think I will let this case rest for now and perhaps next weekend try
to more systematically trace the flow in the macro and hopefully come
back then.

TIA

Örjan

*
If förstaGången Then
With comboInled
.SetFocus
.SelStart = 0
.SelLength = 100
End With
Else
With comboPatNr
.SetFocus
.SelStart = 0
.SelLength = 10
End With
Dim x
x = 1000
For i = 1 To x
DoEvents
Sleep 1000
Next

Btw, does the macro execution wait for the "sleep" to be finished? I
have the feeling it does not.
 
H

Helmut Weber

Hi Örjan,
to let a problem rest for a while, is a good idea.
Helped me a lot.
Besides of some lines, which I don't understand:
x = 1000
For i = 1 To x
DoEvents
Sleep 1000
Next
would cause a timeout of 1000 seconds!
x = 1000, sleep 1000 (= 1000 milliseconds = 1 second)
= 1000 seconds
---
BTW, where are you from?
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
Ö

Örjan Skoglösa

Oops, I did not calculate, I just started with a number that surely
would be enough and then added "the sleep" in the same way.

But it also is interesting to find out that it did not cause any real
timeout. The only effect I realized was slowness.

Until then.

BR,
Örjan

PS. I´m swedish and live in the south of Sweden but was raised in
Hamburg.
 
H

Helmut Weber

Hi Örjan,
it seems, "timeout" wasn't clear enough,
or just the wrong word.
What I meant was "pause".
Bye.
 
Ö

Örjan Skoglösa

Just FYI

I discovered that I hid the userform between runs.

Thus the routine "activate" was not executed. Or to be more precise,
it was not executed when the VBE was closed. Now I use unload instead
of hide and the problem has disappeared.

Just out of curiosity I made a little test and could confirm that with
my constellation (Word97 on Win2K) the built-in routine
UserForm_Activate only is run (after the userform has been hidden) if
the editor is open.

Best regards,
Örjan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top