Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

quick - why won't this work???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Susan - 18 Oct 2006 18:32 GMT
Public Sub cmdClose_Click()

MsgBox "Are you sure you want to quit?", vbQuestion + vbYesNo
If vbNo Then
Exit Sub

Else
Unload Me

End If
End Sub

xxxxxxxxxxxxxxxxxxxx
just keeps giving me the first option (vbNo) even when vbYes is
chosen.....
it's probably something stupid but I can't get it.
thanks!
susan
Tony Jollans - 18 Oct 2006 19:25 GMT
You are not comparing the response from the Msgbox.

   If vbNo Then

is a shorthand for ...

   If vbNo = true Then

What actually happens is that vbNo (a number) is coerced to a boolean value
so that the comparison can be made. Zero is coerced to False and all other
numbers are coerced to True. vbNo (7) is thus considered always equal to
True.

You should be doing something like ..

   If MsgBox("Are you sure you want to quit?", vbQuestion + vbYesNo) = vbNo
then

--
Enjoy,
Tony
Jean-Guy Marcil - 18 Oct 2006 19:27 GMT
Susan was telling us:
Susan nous racontait que :

> Public Sub cmdClose_Click()
>
[quoted text clipped - 13 lines]
> it's probably something stupid but I can't get it.
> thanks!

Because you re coding in a "lazy" manner.

Not that I am trying to blame you or anything like that... Even Microsoft
often posts "lazy" code as examples on their web sites... It is
unfortunately all too common.

vbNo is a constant that equals 7
vbYes equals 6

When you write
   If vbNo Then
you are assuming that the compiler will read the user's thoughts and infer
that the vbNo in that statement is somehow connected to the previous MsgBox
statement... What if you had 2 more MsgBox in the code? Or other code that
also uses vbNo as a constant. What would the compiler infer then?
So, since the compiler will never make such assumptions, the line
   If vbNo Then
is in fact compiled like this:
   If vbNo is True Then
and since vbNo is always equal to 7, the line is always true.

What you want is something like this:

Dim intResponse As Integer

intResponse = MsgBox("Are you sure you want to quit?", vbQuestion + vbYesNo)
   If intResponse = vbNo Then
   Exit Sub
Else
   Unload Me
End If

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Jean-Guy Marcil - 18 Oct 2006 19:31 GMT
Jean-Guy Marcil was telling us:
Jean-Guy Marcil nous racontait que :

> Susan was telling us:
> Susan nous racontait que :
[quoted text clipped - 18 lines]
>
> Because you re coding in a "lazy" manner.

;-)
Just had a thought... if you want to do it the "lazy" way, try this:

If MsgBox("Are you sure you want to quit?", _
   vbQuestion + vbYesNo) = vbNo Then
   Exit Sub
Else
   Unload Me
End If

No need to decalre a variable!

HTH
:-)

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Susan - 18 Oct 2006 20:04 GMT
aha!  i KNEW it wasn't the "if-then" construct, because i've got tons
of them & they all work.
but i could not get this yes-no thing to work.
thank you very much for explaining it, both of you.
(i haven't decided if i'm going to be "lazy" or not - ha ha)
susan
Susan - 18 Oct 2006 20:09 GMT
a thought.............
if i make it an

if msgbox=vb no then
whatever
end if

will that pertain ONLY to that one particular message box line of code?
jean-guy makes a good point - i do have other msgboxes in this code.
perhaps it would be better to declare EACH one's variable

dim intAnswer1
dim intAnswer2
etc.
susan
Greg Maxey - 18 Oct 2006 20:16 GMT
Sub Test()
If MsgBox("Do you want to see another message?", vbYesNo, "Message 1")
= vbNo Then
 Exit Sub
Else
 If MsgBox("Do you want to see another message?", vbYesNo, "Message
2") = vbYes Then
   If MsgBox("Do you want to see another message?", vbYesNo, "Message
3") = vbNo Then
     Exit Sub
   Else
     MsgBox "I think it only applies to the message in question.",
vbOKOnly, "Message 4"
   End If
 Else
  Exit Sub
 End If
End If
End Sub

> a thought.............
> if i make it an
[quoted text clipped - 11 lines]
> etc.
> susan
Susan - 18 Oct 2006 20:21 GMT
that's funny.
i get the point  :D
a) that it works and
b) that i should have TRIED it myself before asking.
:P
susan
Greg Maxey - 18 Oct 2006 20:43 GMT
;-)

> that's funny.
> i get the point  :D
> a) that it works and
> b) that i should have TRIED it myself before asking.
> :P
> susan
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.