MS Office Forum / Word / Programming / October 2006
boolean values
|
|
Thread rating:  |
Susan - 06 Oct 2006 19:29 GMT russ was helping me with this issue in a recent post. i have this short little code in a trial macro word document. in the GlobalMod module, i have:
Option Explicit
Public inumber As Integer Public svariable As String Public blnswitch As Boolean
xxxxxxxxxxxxxxxxxxxxxxxxxxxx in the UserForm1 code, i have:
Option Explicit
Private Sub cmdClose_Click() Unload Me End Sub
Public Sub userform1_Initialize()
blnswitch = True txtnumber.Value = "" txtadjective.Value = "" txtnumber.SetFocus
End Sub
Public Sub cmdRun_Click()
With ActiveDocument .FormFields("inumber").Result = txtnumber .FormFields("sadjective").Result = txtadjective
If blnswitch = True Then .FormFields("blnswitch").Result = "YES" blnswitch = False Else .FormFields("blnswitch").Result = "no" End If
.Fields.Update
Unload Me End With End Sub
xxxxxxxxxxxxxxxxxxx the way i understood it from russ, i have set the boolean value to TRUE in the initialization. therefore, the code above should produce the result "YES", correct? because it is only AFTER it reads it as TRUE that i switch it to FALSE. then why does it produce "no"???????? arrrrrrgggggg!!!!!!!!! i may have to go get a sledgehammer!!!!
:) just kidding TIA for any help! susan
Tony Jollans - 06 Oct 2006 19:41 GMT Hi Susan,
Every time you initialise the userform you set your flag to true. Then when you click your Run button you check it for true, which it is, and set it to false and then you unload your userform. At this point your flag is false. Now if you wat to use the userform again you will have to initialise it again which sets your flag to true again.
What are you trying to do?
-- Enjoy, Tony
> russ was helping me with this issue in a recent post. > i have this short little code in a trial macro word document. [quoted text clipped - 55 lines] > TIA for any help! > susan Susan - 06 Oct 2006 20:02 GMT tony - this is just a tiny part of a much larger project, which is going very well EXCEPT for this boolean thing.
in the code i had posted before, yes, i'm initializing it to true. so when i run this macro, why am i not getting the value "YES", which i'm supposed to get if it is true? the value "no" is supposed to be *IF* the boolean is false. which it is not, when it "hits" the testing "if" statement.
(i realize i could just compromise & change the initialization to "blnswitch=false", but that would just fix the symptom, & i wouldn't figure out what i was doing wrong....). thanks susan
Tony Jollans - 06 Oct 2006 20:15 GMT Aha! I missed what you were saying.
The Initailization procedure must be called UserForm_Initialize and not UserForm*1*_Initialize
-- Enjoy, Tony
> tony - > this is just a tiny part of a much larger project, which is going very [quoted text clipped - 11 lines] > thanks > susan Susan - 06 Oct 2006 20:24 GMT aaaaaaahhhhhhhhhhhhh. thank you very very much! <bow> susan
Russ - 07 Oct 2006 08:56 GMT Susan, With the cursor in the beginning subroutine, if you had single-stepped your short example code in the VBA Editor by using the menu Debug>"Step Into" and by hitting the F8 function key to move one line at a time, you would have seen which subroutines were being used, in what order, as the code is running. Also while single stepping, it is often useful to pause and hover over different parts of the code with the mouse cursor, because the VBA Editor will produce a 'tooltip' showing what the *current* value is of the syntax you are hovering over. Other useful debugging tools include the immediate window, for injecting ad hoc code changes while in the run mode; the watch and locals window, for viewing (watching) variables and range.text, etc. as the values change.
P.S. You probably didn't have to change all your userform subroutines to Public as they usually do work OK under the default creation scope of Private.
> aaaaaaahhhhhhhhhhhhh. > thank you very very much! > <bow> > susan
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 07 Oct 2006 09:37 GMT Susan, I just read this answer from Jean-Guy to another posting. It is certainly an appropriate debugging method that applied to your problem, too.
---------- clipped text ----------------------------- Then, this means that there is a name error.
Just to test this name, create a new module in your template project, then create a Sub like this: Type: Sub TestUserForm and hit Enter. You should now have
Sub TestUserForm()
End Sub
Place the cursor between the Sub and End Sub statement and type:
UserForm1
and now type a period "." right after UserForm1.
Automatically, you should have a list of available methods/properties that are available to UserForm1. This is called the VBE IntelliSense.
If you do not have a list that pops ups, then it means there is name error and your userform is not called UserForm1 as you think it is. Double check the Userform name form the Project browser pane (Top left part of the VBE window).
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP
--------------- end of clipped text -------------
> Susan, > With the cursor in the beginning subroutine, if you had single-stepped your [quoted text clipped - 17 lines] >> <bow> >> susan
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 07 Oct 2006 18:28 GMT Susan, Another method used in debugging, is the judicious use of Breakpoints created by clicking in the left margin of the VBA Editor code window or through the Debug menu. If you suspect a line of code is not being read you can put a Breakpoint there to see if, in fact, the program does stop at the Breakpoint as it should. Or to avoid the tedious effort of Single-Stepping through a long program, you put Breakpoints just before the "points of interests" and can alternate between Single-Stepping and the fast, normal Run mode. While stopped (Broke?) at a "point of interest" in the code and still Paused in the Run mode, the Tooltips appear over parts of the code where the mouse cursor is hovering and you can query Word through the Immediate window or see what the current values are, in what you may have set up, in the Watch, Locals, or Stack windows. You can also pause a running macro by clicking on the Pause button in the VBA Editor window or by pressing the Shift + Pause/Break (on the top row) keys of the keyboard. Finally, if Word doesn't interact like you think it should, it may be because Word is Paused in Run mode. Try clicking on the square Stop button in the VBA Editor window to bring it out of Run mode.
> Susan, > I just read this answer from Jean-Guy to another posting. It is certainly an [quoted text clipped - 57 lines] > Enjoy, > Tony
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Susan - 07 Oct 2006 22:01 GMT thank you all for your direction. i knew about stepping thru but i didn't think of it for this issue - i guess i figured it was so short of a macro, i didn't "need" it (lesson learned!). actually, once the incorrect name was pointed out, i went back to my REAL macro & tried to figure out why THAT boolean value was not working. there were no spelling errors there, but it still wouldn't catch that blnFirst value. when stepping thru, it went to "if" & then straight to "endif". after wracking my very tired friday-afternoon brain, i finally thought of the fact that the "if" statement that contained the trial was connected to the userform using "with" & "end with". however, the blnFirst value was NOT connected to the userform. so i went to the userform, and on the same page as the chkbox that was the other part of the "if" statement, i added a chkbox named "blnFirst". i made it not visible, no tab stop enabled, and the value set as checked ("true"). voila!!! it worked. :D it actually now triggers the "if" statement the first time, and then ignores it the second time, since blnFirst is set to false in the "if" statement. i don't know if this is the CORRECT way to do it, but since it was friday @ 4pm i didn't really care!!! (dance around waving arms wildly & grinning like a banshee!) actually during this process i also coincidentally discovered that mouse-over hovering that you guys described - i didn't know that was there before, but it is handy! i greatly appreciate all your help with this very ambitious macro project for a relative-newbie! gratefully, susan
Shauna Kelly - 08 Oct 2006 02:34 GMT Hi Susan
If you haven't yet discovered it, you might also find it useful to check out the "Locals" window. To see it, do View > Locals Window.
Now, when you step through your code using F8, you can actually see values of variables change as you execute the code step by step.
Hope this helps.
Shauna Kelly. Microsoft MVP. http://www.shaunakelly.com/word
> thank you all for your direction. i knew about stepping thru but i > didn't think of it for this issue - i guess i figured it was so short [quoted text clipped - 23 lines] > gratefully, > susan Russ - 09 Oct 2006 01:18 GMT Susan,
> thank you all for your direction. i knew about stepping thru but i > didn't think of it for this issue - i guess i figured it was so short [quoted text clipped - 3 lines] > spelling errors there, but it still wouldn't catch that blnFirst value. > when stepping thru, it went to "if" & then straight to "endif". The computer does what you tell it to do, not necessarily what you wanted it to do. ;-) Remember, too, that if your 'if statements' are made into compound 'if statements' with the 'And' logical operator, then you are telling the computer not to run the subsequent section of code unless ** all ** parts of the compound statement are True. You can make very elaborate compound 'if statements' by mixing 'Andy's, 'Or's, 'Not's, and 'Xor's, etc. (boolean logic)
> after wracking my very tired friday-afternoon brain, i finally thought > of the fact that the "if" statement that contained the trial was [quoted text clipped - 16 lines] > gratefully, > susan
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
|
|
|