PDA

View Full Version : Solved: Word - Run time error running a form



tallen
11-24-2010, 07:35 AM
I have created a Word template and in VBA created two forms with text boxes, check boxes and a drop down list in UserForm1, I am calling Userform2 which contains a dropdown list box. When I run UserForm1 and fill in all of the boxes when clicking on command button to call userform2 I get error "run time error 28, out of stack space. I would be really grateful if someone would look at my attempts and tell me where I have gone wrong. Many thanks for taking the time to read this.

Paul_Hossler
11-24-2010, 09:14 AM
A document with the macros and user forms would be helpful

Otherwise, I've always found that a Stack Space error occurs when I have unintended recursion:

ONE calls TWO calls THREE calls ONE which ....

Paul

tallen
11-24-2010, 09:31 AM
Sorry, am new to VBA and not sure if I am using the correct commands!! I attach the word document I am struggling with!! Thanks very much for taking a look at it for me.

Paul_Hossler
11-24-2010, 10:31 AM
Event driven programming is a little tricky



'load first, then show ----------------
Load UserForm5
UserForm5.Show
End Sub

Private Sub CommandButton2_Click()
'hide first, then unload ----------------------------
' no End statement either
UserForm1.Hide
Unload UserForm1
End Sub


I'm not sure about your logic, but the changes I made do not cause a stack error (always a good thing)


Paul

fumei
11-24-2010, 10:49 AM
I am not sure about logic either.

Load UserForm5
UserForm5.Show


If you are using Show right after Load, this is redundant instruction. Show be default starts with a Load. You do NOT need a load if you are immediately doing a Show.

The same thing with Hide and Unload. There is absolutely no point in doing a Hide followed immediatly by an Unload. Just an Unload is needed.

tallen
11-24-2010, 11:30 AM
Thank you so much. Out of interest, where do you think my logic is odd!! May I also ask one more thing.....is it possible to have two dropdown boxes on one form? I really appreciate your help - thanks again. T

tallen
11-24-2010, 11:33 AM
Ah, now I understand, only self taught, think I need a good book!1 Many thanks

fumei
11-24-2010, 12:42 PM
You can have as many controls as you like. The only criteria is useability - too many controls makes things crowded and confused. If you have a LOT, consider using Multi-Pages.

Re: logic.

You have:

Unload UserForm1
UserForm1.Hide

You can not Hide something AFTER you have unloaded it.

You are using Selection a lot.

Select Case ComboBox1
Case 0
Selection.Text = UserForm1.ComboBox1.Text
Case 1
Selection.Text = UserForm1.ComboBox1.Text
Case 2
Selection.Text = UserForm1.ComboBox1.Text
Case 3
Selection.Text = UserForm1.ComboBox1.Text
End Select
OK, so you have made Selection.Text something. But then you immediately:

Selection.GoTo what:=wdGoToBookmark, Name:="Disabled"

If CheckBox1.Value = True Then
Selection.TypeText Text:="Yes"
move the Selection and type something different from what you put into Selection.

Hint: if you you want to put text INTO a bookmark, and want to be able to keep the bookmark do not use Selection and Typetext.

Paul_Hossler
11-24-2010, 01:31 PM
There is absolutely no point in doing a Hide followed immediatly by an Unload. Just an Unload is needed.


Gerry,

I'm sure you're correct, but ...

1. Does it hurt or waste cycles?

2. Since my style is to lead in with Load and Show, I feel (100% absolutely personal opinion) that for balence I like to leave with Hide and Unload (if I'm done with the form).

3. It's just one more of my quirky styles/habits, because a lot of times I'll leave a Userform in memory and .Show and.Hide it multiple times, before I'm totallty done with it and Unload and release its memory.

Paul

Paul_Hossler
11-24-2010, 01:34 PM
where do you think my logic is odd!!


Sorry - I wasn't implying that your logic was odd, only that I did not bother to trace through it.

I was only looking at the Shows and Hides, not what you were doing with the data or any thing else

Paul

fumei
11-24-2010, 03:21 PM
Your #3 is completely correct. If you are still using it, of course use Hide and Show.

But

Hide immediately followed by Unload is not multiple uses.

While it does not use extraneous cycles, it IS an extraneous instruction. And therefore it is an extraneous parsing of the code. My position is that if you do not need an instruction, do not use the instruction.

Selection.MoveEnd Unit:=wdCharacter, Count:=1
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Selection.MoveEnd Unit:=wdCharacter, Count:=1


is, technically (in terms of result) the same as:

Selection.MoveEnd Unit:=wdCharacter, Count:=7


Is the first wrong? No, not really. Does it "hurt", or waste cycles? No, not really, although - again - each one DOES have to go through the parser.

tallen
12-09-2010, 08:44 AM
Just wanted to say thank you to everyone for their expertise, I have now been able to resolve my problem now I understand about calling and hiding Userforms.