Solved

VB Riddle

Posted on 2000-04-01
60
263 Views
Last Modified: 2010-05-02
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
Comment
Question by:ameba
  • 18
  • 13
  • 11
  • +6
60 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2675861
Hi Ameba, I haven't recieved an error.
0
 
LVL 15

Author Comment

by:ameba
ID: 2675868
Hi, Marine
The goal is to receive an error in the 'b' line, and explain situation when 'a' works and 'b' gives error.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2676211
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2676219
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
 
LVL 15

Author Comment

by:ameba
ID: 2676232
Hi, wsh2,
The form has a Caption, and Caption property is read/write.

>its a riddle, huh?
Yes :)
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2676241
<laughing and stating silently unto himself>.. "!@#$%^&*()_+|"
0
 
LVL 6

Expert Comment

by:Marine
ID: 2676248
Yes i just checked it too. Ameba can you tell me the answer i am curiouse to find out.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2676264
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2676271
Nope.. datz not it.. LOL
0
 
LVL 15

Author Comment

by:ameba
ID: 2676302
>can you tell me the answer
I hope someone will solve it. If not, I'll post the answer here.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2676398
1. of april ????
0
 
LVL 6

Expert Comment

by:Marine
ID: 2676401
haha this might just be it.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2676407
Marine:
You never know, and look at the text in statement b: for the msgbox :-)
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2676415
AngelIII:
ROFLMAO!!!!! Well done.. <smile>
0
 
LVL 15

Author Comment

by:ameba
ID: 2676419
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2676425
wsh2: (whisper) can you tell me what your expression ROFLMAO means. My English knowledge does not cover this...
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2676457
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
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2676515
angelIII: ROFLMAO = Rolled On Floor Laughing My <expletive deleted--alternative word for person's bottom> Off. <G>
0
 
LVL 27

Expert Comment

by:Ark
ID: 2677404
ameba, thats's great!
Unfortunately missing this question. Tomorrow I'll try to solve the puzzle.
Thanks
0
 
LVL 15

Author Comment

by:ameba
ID: 2677513
Edited text of question.
0
 
LVL 15

Author Comment

by:ameba
ID: 2677529
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
 
LVL 7

Expert Comment

by:BarryTice
ID: 2678545
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
 
LVL 27

Expert Comment

by:Ark
ID: 2678584
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
 
LVL 15

Author Comment

by:ameba
ID: 2678804
There is only one form in the project - no other modules.
0
 
LVL 27

Expert Comment

by:Ark
ID: 2678848
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
 
LVL 15

Author Comment

by:ameba
ID: 2678909
First statement will set Caption to 1.
>you see MsgBox
No
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2678921
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
 
LVL 27

Expert Comment

by:Ark
ID: 2678925
I was sure on this with 99.99%. Now I know the way, but still can not find solution :-(
0
 
LVL 27

Expert Comment

by:Ark
ID: 2678930
Is this depend on OS? I thing no. IMHO, MsgBox isn't Message Box.
0
 
LVL 15

Author Comment

by:ameba
ID: 2678956
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 27

Expert Comment

by:Ark
ID: 2678984
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
 
LVL 27

Expert Comment

by:Ark
ID: 2678989
Oops...
Another one

Option Explicit

Private Sub Form_Click()
    Dim X

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


0
 
LVL 15

Author Comment

by:ameba
ID: 2679099
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2679118
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
 
LVL 27

Expert Comment

by:Ark
ID: 2679146
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2679166
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
 
LVL 15

Author Comment

by:ameba
ID: 2679219
I didn't add any reference or component.
I didn't write any additional project or dll.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2679262
Dang.... I thought of:

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

But that gives a syntax error, not a run time error...
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2679270
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2679279
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
 
LVL 15

Author Comment

by:ameba
ID: 2679299
Ark is not far from the solution...
0
 
LVL 15

Author Comment

by:ameba
ID: 2679344
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
 
LVL 9

Expert Comment

by:antrat
ID: 2679378
Ctrl+Break ???
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2679389
antrat: Error????
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2679407
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2679420
C?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2679422
I still don't SEE it?
0
 
LVL 27

Expert Comment

by:Ark
ID: 2679423
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2679429
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
 
LVL 27

Expert Comment

by:Ark
ID: 2679451
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
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 150 total points
ID: 2679503
!!!!!!!! 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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2679530
Well done, sherlock!
0
 
LVL 15

Author Comment

by:ameba
ID: 2679534
Adjusted points from 50 to 150
0
 
LVL 15

Author Comment

by:ameba
ID: 2679535
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
 
LVL 15

Author Comment

by:ameba
ID: 2679554
>5) set the default property to (vbOkOnly)
small correction...
TextBox property Text is "1"
   Hello(0).Text = "1"

Thank to all!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2679555
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
 
LVL 15

Author Comment

by:ameba
ID: 2679576
I also posted it to 2 another places, also get some ugly comments. It seems EE has the most patient and smartest experts. :-)
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2679587
Where else did you post it?
0
 
LVL 15

Author Comment

by:ameba
ID: 2679870
It is now solved on the devx NG also.
0
 
LVL 27

Expert Comment

by:Ark
ID: 2681836
AngelIII,
WELL DONE!!!
Unfortunatelly, Form was a last "Control" I tried (though must be first :-))
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now