• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

VB Riddle

I simplified the puzzle:

When I click on my form, first statement runs OK (it will set caption to "1"), but second statement gives run-time error.

****  Q: How is this possible?  ****

' Here is complete code (there is no other code
'   in the form or elsewhere)
Option Explicit

Private Sub Form_Click()
    Caption = MsgBox("Hello", vbOKOnly)
    Caption = MsgBox("Hehe", vbOKOnly)  ' <<< ERROR!
End Sub

--
Have fun!
0
ameba
Asked:
ameba
  • 18
  • 13
  • 11
  • +6
1 Solution
 
MarineCommented:
Hi Ameba, I haven't recieved an error.
0
 
amebaAuthor Commented:
Hi, Marine
The goal is to receive an error in the 'b' line, and explain situation when 'a' works and 'b' gives error.
0
 
wsh2Commented:
The problem is not in the styles.. but rather, you are putting the results of the MsgBox Dialog into your Form caption. MsgBox returns the folowing values..

vbOK(1) OK
vbCancel(2) Cancel
vbAbort(3) Abort
vbRetry(4) Retry
vbIgnore(5) Ignore
vbYes(6) Yes
vbNo(7) No

What I'm guessing you should be doing is the following..

<----- Code Begin ----->

  Dim Response as Integer
  Dim style As VbMsgBoxStyle
     
  style = vbExclamation Or vbOKOnly
  Response = MsgBox("Hello", style)
  If Response = vbOk _
  Then
    Form1.Caption ="HELLO!"
  End If

' -or-

  style = vbExclamation Or vbOKCancel
  Response = MsgBox("HeHe", style)
  Select Case Response
    Case vbOk
      Form1.Caption ="HeHe!"
    Case vbCancel
      Form1.Caption = "CANCEL!"
  End Select

<----- Code End ----->








0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
wsh2Commented:
Ooops.. its a riddle, huh? <lol>. The one way this could fail, is if the Form was defined with no Caption.. ergo.. the Caption property is read only.
0
 
amebaAuthor Commented:
Hi, wsh2,
The form has a Caption, and Caption property is read/write.

>its a riddle, huh?
Yes :)
0
 
wsh2Commented:
<laughing and stating silently unto himself>.. "!@#$%^&*()_+|"
0
 
MarineCommented:
Yes i just checked it too. Ameba can you tell me the answer i am curiouse to find out.
0
 
wsh2Commented:
If b: does NOT have a:

  Dim style As VbMsgBoxStyle

statement in front of it you will error out because the command1.style property will only handle numbers up to 48.

vbexclamation or vbokcancel = 49
     
   
0
 
wsh2Commented:
Nope.. datz not it.. LOL
0
 
amebaAuthor Commented:
>can you tell me the answer
I hope someone will solve it. If not, I'll post the answer here.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
1. of april ????
0
 
MarineCommented:
haha this might just be it.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Marine:
You never know, and look at the text in statement b: for the msgbox :-)
0
 
wsh2Commented:
AngelIII:
ROFLMAO!!!!! Well done.. <smile>
0
 
amebaAuthor Commented:
Yes, today is 1. of april
It is a tricky question, but it is 100% valid.

If somebody is very impatient, I can send solution via e-mail (my e-mail address is in my profile)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
wsh2: (whisper) can you tell me what your expression ROFLMAO means. My English knowledge does not cover this...
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
One thing is clear: after this question, i discovered some VB options. I have been looking into the deepest options, settings, stuff...
I assume that the error occurs during runtime (i couldn't produce the error), meaning that there must be some side effect due to some compilation options, but to try all the combinations....
0
 
pjknibbsCommented:
angelIII: ROFLMAO = Rolled On Floor Laughing My <expletive deleted--alternative word for person's bottom> Off. <G>
0
 
ArkCommented:
ameba, thats's great!
Unfortunately missing this question. Tomorrow I'll try to solve the puzzle.
Thanks
0
 
amebaAuthor Commented:
Edited text of question.
0
 
amebaAuthor Commented:
I simplified the puzzle. The original text of question is in this Lounge question:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lounge&qid=10321083 
0
 
BarryTiceCommented:
I still don't know the answer, but I'm still posting here so I can eventually find out what it is. So there.
0
 
ArkCommented:
dmn!
Alredy done using bas module with 4 strings of code (including Option Explict and function - end function) - ie only one string need. But if you say "no more code"...
0
 
amebaAuthor Commented:
There is only one form in the project - no other modules.
0
 
ArkCommented:
Ameba,
BTW <First statement runs OK> it means it works in "standard way" (you see MsgBox) or only that you don't receive Error?
Cheers
0
 
amebaAuthor Commented:
First statement will set Caption to 1.
>you see MsgBox
No
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This night, i had this thought:
* Click on Form (Opens Message Box)
* Try to Shut Down NT
* Click OK (of First MSGBOX)
While the process now tries to open the 2nd Msgbox, this will be proscecuted because the system is shutting down
0
 
ArkCommented:
I was sure on this with 99.99%. Now I know the way, but still can not find solution :-(
0
 
ArkCommented:
Is this depend on OS? I thing no. IMHO, MsgBox isn't Message Box.
0
 
amebaAuthor Commented:
angelIII,
I didn't try shutdown, but I think you cannot do it at the right moment (after the first line, but before the second). Nice try, but this is not it.

Ark,
No, you won't see Message Box dialog.
Code works the same in VB4, VB5 and VB6, Win95, NT...
0
 
ArkCommented:
Just a joke (thiugh today is not a 1 of April)
PASTE this code and try to run
Option Explicit

Private Sub Form_Click()
    Dim X

    Caption = MsgBox("Hello", vbOKOnly)
    Caption = M¾gBox("Hehe", vbOKOnly)  ' <<< ERROR!
End Sub

But this isn't a solution - only joke. Now I'm digging through a bulk of controls and their defoult properties/methods
Cheers
0
 
ArkCommented:
Oops...
Another one

Option Explicit

Private Sub Form_Click()
    Dim X

    Caption = msgbox("Hello", vbOKOnly)
    Càption = msgbox("Hehe", vbOKOnly)  ' <<< ERROR!
End Sub


0
 
amebaAuthor Commented:
AngelIII,
I tested your shutdown solution.
If I try shutdown when second Message Box is displayed, I receive error:
"Cannot quit at this time" - in VB IDE, or
"Cannot quit" - when compiled.

This is not trappable run-time error (it doesn't have error number), but I'll award points to you if nobody finds real solution.

It is possible that Ark will find it.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Other thougt:
If in references, you have a dll that contains also a function msgbox, and this reference is moved up before any VB reference, this function will be called. if this dll is written by you, you may raise an error whenever you want, for example when first parameter is "hehe", or when function is called 2nd time. Or it may return an object, which in first call has a default property (with 1 as value), and the second time no default property, a NULL value or something similar that will raise an error when trying to affect this to the caption
0
 
ArkCommented:
angelIII,
I don't thing so. Using own ActiveX is the same as using bas or cls modules - it means more code than only at form. It seems this is standard control named MsgBox. Now I stay at PictureBox(Image) or ListBox(Combo) - IMHO no other fit this puzzle (BTW, I don't check OLE yet). As ameba said this code works at VB4, so I didn't check CommonControls.
Cheers
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Ark: as i said, it is just a thought, which may be based on the sentence of ameba:
There is no other code in the form and no other module in the project.
So the problem must be somewhere else?!
0
 
amebaAuthor Commented:
I didn't add any reference or component.
I didn't write any additional project or dll.
0
 
caraf_gCommented:
Dang.... I thought of:

    Caption = MsgBox(''Hehe'', vbOKOnly)  ' <<< ERROR!

But that gives a syntax error, not a run time error...
0
 
caraf_gCommented:
Or even:

    Caption = MsgBox("Hehe", vbOKOnIy)  ' <<< ERROR!

.... You cannot compile this, but if you hit F5 you get a "run time" error "Variable not defined"

?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
OK, but where is the typing error then, when you paste the code in the question, there is not O/0 or 1/l or similar...
0
 
amebaAuthor Commented:
Ark is not far from the solution...
0
 
amebaAuthor Commented:
Sorry, my connection is very slow at this time of the day.
For now, Ark and AngelIII mentioned some good parts.
I'll check later...
0
 
antratCommented:
Ctrl+Break ???
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
antrat: Error????
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
After this one, have to go to optician, as i was staring too long at these 2 lines of code........................
I won't even be able to SEE you in front of a judge, being condamed for this question................
Seems i'm close to it and can't SEE it however....................
0
 
caraf_gCommented:
C?
0
 
caraf_gCommented:
I still don't SEE it?
0
 
ArkCommented:
Option Explicit

Private Sub Form_Click()
    Caption = msgbox("Hello", vbOKOnly)
'typing with invisible ink
   SendKeys "^{BREAK}"
'change ink to visible
    Càption = msgbox("Hehe", vbOKOnly)  ' <<< ERROR!
End Sub

Cheers :-)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Ark: no other code (even not invisible) code is allowed here, i assume

BTW, i have problems with SendKeys, it seems not to work?...
0
 
ArkCommented:
I didn't check syntax, just type. It's a joke. Have to stop experiment, I'll continue tomorrow.
AngelIII - if you'll continue, it seems it's a picturebox.
Place picturebox on your form, name it MsgBox. Good luck
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
!!!!!!!! Found it !!!!!!!!

The hint of Ark showed me:

1) Name your form msgbox
2) Add a control to your form
3) name the control Hello
4) give the control the index 0
5) set the default property to (vbOkOnly), control maybe label, textbox...

Error is:
730: Control "Hehe" not found



0
 
caraf_gCommented:
Well done, sherlock!
0
 
amebaAuthor Commented:
Adjusted points from 50 to 150
0
 
amebaAuthor Commented:
Bravo!!!

It uses default property of the Form and default property of TextBox or Label:

Caption = MsgBox.Controls("Hello", 0).Text

angelIII, you can also answer question in the Lounge.
0
 
amebaAuthor Commented:
>5) set the default property to (vbOkOnly)
small correction...
TextBox property Text is "1"
   Hello(0).Text = "1"

Thank to all!
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Ark: i suggested ameba to assign you the points in the Lounge (as your comment unclicked the solution). So post a comment there.

ameba: Great and challenging this riddle....
Have another one?
0
 
amebaAuthor Commented:
I also posted it to 2 another places, also get some ugly comments. It seems EE has the most patient and smartest experts. :-)
0
 
caraf_gCommented:
Where else did you post it?
0
 
amebaAuthor Commented:
It is now solved on the devx NG also.
0
 
ArkCommented:
AngelIII,
WELL DONE!!!
Unfortunatelly, Form was a last "Control" I tried (though must be first :-))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 18
  • 13
  • 11
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now