
Dreaded vbscript INPUTBOX |
Post Reply
|
Page <12 |
| Author | |||
rasimmer
Frequent Contributor
Joined: 30 Jan 2009 Location: United States Online Status: Offline Posts: 344 |
Quote Reply
Posted: 25 Jan 2010 at 08:53 |
||
|
Wow...didn't expect to spark a response like that. This is typically what I would do in this situation:
return = InputBox("Enter data","My Input")
MsgBox "VarType: " & VarType(Return)
If IsEmpty(Return) Then MsgBox "Cancel clicked" ElseIf Return = "" Then MsgBox "Empty string" Else MsgBox "String returned: " & Return End If If I click Cancel, it returns a 0, or VBEmpty, or VarType(0). How is using IsEmpty or looking for varType(0) different? VarType(0) is vbEmpty.
|
|||
![]() |
|||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6809 |
Quote Reply
Posted: 25 Jan 2010 at 09:09 |
||
|
IsEmpty will retutn true in both cases whereas VarType(myvar) = vbEmpty will only return true when the Cancel button is clicked. It is the ONLY way to detect the cancel button. Anything else is ambiguous. This is teh exact issue that theposter and you are mixed up about. YOu are only looking at one narrow aspect of usage so everything you do to test your understanding misses the point just enough to pervent you from seeing what I am trying to point out.
ONLY VarEmpty(myvar) = vbEmpty detects the cancel button. An empty var of any type will alway evaluate to true when tested against an empty string. This is because VBScript uses a form of "type coersion" which tries to convert any variables to compatible type in a left to right order. If you test an empty var against a string it willbe converted to an empty string. Because of tthis both buttons will evaluate identicall if you just test for an empty string. You must check vartype against vbEmpty. It is teh only time it is 0 (zero) If OK is clicked and teh string is empty then vartype will evaluate to vbString or 8 and NOT to 0.
Stop and think about it for a bit. I think you will see it now.
|
|||
![]() |
|||
raster_gfx
I'm new here
Joined: 23 Jan 2010 Online Status: Offline Posts: 32 |
Quote Reply
Posted: 25 Jan 2010 at 21:05 |
||
|
The OK can be detected (with no data entered) with a VarType(project) = 8 AND len(project) = 0 test... here is another variation of my original script... in reality... ALL aspects of the test... OK with no data, Cancel, NOT the option 1 or 2, Invalid data - x or a space... all are accounted for with a 2 line if elseif test....
Which can (without message boxes other than those we want) be condensed down to this.....
So... basically... my original code from the 1st post... technically is still sound and valid.... you may have done a different do while yet that would have entailed further error-checking (I think)..... anyways... I'm sure this thread (if someone out there was wondering about the inputbox like I was) will find this post useful in some manner... Going to examine those links you provided... for further research... ;) Hmmm... after contemplating... it actually could all be accomplished with this... isn't this similar to what you posted earlier J?
Nah that doesn't cover all the bases... anyways... :) Edited by raster_gfx - 25 Jan 2010 at 21:46 |
|||
![]() |
|||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6809 |
Quote Reply
Posted: 25 Jan 2010 at 22:48 |
||
|
raster - you still miss the point. It's not that your code is wrong opnly that you are not seeing how the INputBox was designed to work. Because of this you think you need to do too,amy things.
There are only two outcomes. Either OK is clicked or Cancel was clicked. If VarType is 0 then Cancel else OK. Only one test is needed to dtermine which button is clicked. Once you can understand th e importance of this and how it is differnt from what you are saying you will undoubtedly have a revelation. New worlds will open and you will see magical possibilities in your coding.
One test one kill - isn't that the motto?
As for other error checking. No! No other checking is ever necessary. Wht would you think that. Only two buttons. Only one test. If, as an alternative to the default you want to check the string it can be done as I have posted above and it will cover EVERY possibility.
What you have done will work and it can be used without any issues. I only post this because your explanation of why what you are doing is necessary shows that you don't understand how InputBox is designed.
Your final example is getting closer but you are still testing more things than you have to test. The exxmple I posted is all that is required.
This:
Do
return = InputBox("Enter data","My Input") Loop While VarType(return) <> vbEmpty And return = "" Will always force either a Cancel or a non-empty string whenthe loop is exited. Just substitute your message and title and you will see that it works. It works in all cases. (The order of the test is required.) A logical AND will always fail if the first element is false and it will never evaluate the second element. Do it the other way around and it won't work as expected. The return variable will always evaluate to FALSE if the string is not empty and the Vartype will never get tested. Because of this you will never be able to detect the Cancel button. This is one condition of programming logic that befuddles non-programmers. It is part of the design of nearly all languages because it provides for conformance to the rules of logic. It also is one of the biggest causes of program failures because the programmer didn't understand this element of the language design.
Hey. We have been using this in VB for almost 20 years. It just works like this.
ANyway - your method will work so, if you prefer to use it then do by all means. In admin programming whatever gets the job done...
Edited by jvierra - 25 Jan 2010 at 23:07 |
|||
![]() |
|||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6809 |
Quote Reply
Posted: 25 Jan 2010 at 23:12 |
||
|
raster - one last note:
You attempt to clarify this was a good one. Unfortunately you stumbled on one of my pet peaves so I though it a good chance to clear up some issue about InputBox. Don't let that discourage you from other demos as ther eare hundreds of bits to this scripting stuff crying out for an explanation.
Also, I wasn't trying to rain on your parade but thouught you might benefit from aa rethink of you investigation. I am betting that this will be of some value going forward.
Good luck.
|
|||
![]() |
|||
rasimmer
Frequent Contributor
Joined: 30 Jan 2009 Location: United States Online Status: Offline Posts: 344 |
Quote Reply
Posted: 26 Jan 2010 at 06:16 |
||
|
Can I ask a stupid VB design question? In this scenario
return = MsgBox("Do you want to do this?", vbOkCancel + vbQuestion, "Question")
If Return = vbCancel Then
MsgBox "You clicked Cancel" Else MsgBox "You clicked OK" End If You have a constant for Cancel, but InputBox requires all of this "special handling".
|
|||
![]() |
|||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6809 |
Quote Reply
Posted: 26 Jan 2010 at 06:54 |
||
|
rasimmer - again this is exactly the point I havebeen trying to make.
In order to design the INputBox so that it will behave well in a programatic environment it was necessary to give it certainbehaviors that may seem odd compared to MsgBox behaviors.
Consider that MsgBox only returns an Integer. That Integer can easily encode multiple buttons but it is always an Integer. You may need to test for two items witj MsgBox because of scenarios like Yes/No/Cancel. If the message box has only two buttions (Yes/No) then only one test is necessary.
InputBox on teh other hand returns strings EXCEPT when the Cancel button is clicked. It then returns a vbEmpty variant which means that the contents of the variant are neither Integer nor String nor anything else.
Since VB languages guarantee that unassigned variables will never throw errors when tested the return variant will always evaluate to an empty string or the Integer zero when tested in the normal way like:
if return = "" if return - 0
If the Cancel button is clicked both of these will always be true so it will be impossible to determine if an empty string means Caancel unless we define that it does mean Cancel. It we wan an empty strin to be valid it is necessary to detect vbEmpty to determine that the user DID NOT click Cancel. Remember Cancel usually means NO SELECTION and not an empty string.
These issues seem subtle but are at the very heart of program design. Not be aware of this can lead to errors in logic that are very hard to find especially when you are not aware of the symantics in use.
Probably a simper explanation is...
Why would an InPutBox need more than two buttons and, if it did, how would you encode the return to reflect this is a way that is easy to determine programatically. Reverse engineer the design process of the InputBox and you will understand why the designers chose the solution they used.
|
|||
![]() |
|||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6809 |
Quote Reply
Posted: 26 Jan 2010 at 07:05 |
||
|
Note that in VB.NET Microsft has chosen to ignore the cancel button in favor of a more confusing method.
In NET we need to provide a default which is cleared when the Cancel button is chosen. Of course if the user clears the box and clicks OK tthen it will appear to be a CAncel.
Microsoft would like to get rid on InputBox becasue it is a royal pain to get people to understand how it needs to work. Initially in VB.NET MIcrosft had trownout both MsgBox and InputBox but later added thenback in with the VB support classes.
|
|||
![]() |
|||
Post Reply
|
Page <12 |
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |