
Dreaded vbscript INPUTBOX |
Post Reply
|
Page 12> |
| Author | |||||||||||||||||||||||||||||||||||||||||||||||||||||
raster_gfx
I'm new here
Joined: 23 Jan 2010 Online Status: Offline Posts: 31 |
Quote Reply
Topic: Dreaded vbscript INPUTBOXPosted: 23 Jan 2010 at 23:01 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I've searched high and low over the last 2 years trying to find the consummate answer to this riddle... however, looking high and low and nary a straight answer was to be found (I think).... anyways... for those of you that work in certain applications (like iSeries telnet app) that do have vbscript macro functionality... you may have run across putting together an inputbox or 2... and to the reason for this post... the OK and Cancel buttons... how to differentiate between the 2? What if they click Cancel ... what if they click OK without entering any data... what if what if... if you've ever tried to make a foolproof inputbox then you know what I'm referring to... so... here it is... a simple inputbox.. 2 options... loops around and around giving you msgbox's of the choices you made... typing exit will exit the inputbox... simple......
>>>>>>>>>> do
project = inputbox("1 - OPTION 1" & CHR(13) & CHR(13) & "2 - OPTION 2" & CHR(13) & CHR(13) &" Please enter 1 or 2 ONLY and press Enter or click OK." & CHR(13) & CHR(13) & " Type EXIT to exit." , "Vbscript Error Rountines") if (VarType(project) = 8 AND len(project) = 0 OR not IsNumeric(project)) then if (VarType(project) = 8 AND len(project) = 0) then msgbox"OK selected with no data. Try again." elseif (project = "exit" OR project = "EXIT") then msgbox"Exiting..." exit do elseif not IsNumeric(project) then msgbox"" & project & " is Invalid data, try again." end if elseif (VarType(project) = 8 AND len(project) > 0 OR VarType(project) = 0) then if (len(project) > 0 AND project > 0 AND project < 3) then msgbox"" & project & " selected" elseif VarType(project) = 0 then msgbox"CANCEL selected. NOT CANCELLING HOWEVER!" else msgbox"" & project & " is an Invalid #. Try again." end if end if loop >>>>>>>>>>
Edited by raster_gfx - 24 Jan 2010 at 11:48 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 23 Jan 2010 at 23:50 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This is all you need to do or know:
The InputBox will only ever return type 0 (zero) if teh cancel buttonis selected. Any other selection will be a string. It will be empty, the user value or the default.
Why are you making it so complex. It's a simple one or teh other cchioce and never anyting else.
When we want more complexity it is helpfule to move to an HTA as the message boxes can be built on dialogs and become as complex as you like.
I suggest you try one of the numerous custom versions of InputBox that can be downloaded from The Internet. Many of these return an integer result and fill one or more strings or not. They can have three or even more buttons.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
raster_gfx
I'm new here
Joined: 23 Jan 2010 Online Status: Offline Posts: 31 |
Quote Reply
Posted: 24 Jan 2010 at 02:21 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
It's not that I am trying (or want to) make this complex... this code was meant to show how all aspects of the inputbox can be defined... the purpose of the code is inside a .mac file that is used by the iSeries Access telnet interface... which utilizes basic VBSCRIPT... this is the actual code I use for just one particula inputbox (that I don't use, that's why it needs to verify what the user actually inputted).....
Here is a small auto-data entry script used by the iSeries program.... this will enter in the selected Company Data (modified to generic for this purpose) into the AS400 telnet session... enters it all in like a second... ------------------------------------------------------------------ [PCOMM SCRIPT HEADER] LANGUAGE=VBSCRIPT DESCRIPTION=Auto_Data_Entry [PCOMM SCRIPT SOURCE] OPTION EXPLICIT autECLSession.SetConnectionByName(ThisSessionName) dim project dim name1, name2, name3 dim addr1, addr2, addr3 dim extension, phone1, phone2 do_job sub do_job() do project = inputbox(" 1 - Company One"& CHR(13) & CHR(13) & " 2 - Company Two" & CHR(13) & CHR(13) & " 3 - Company Three" & CHR(13) & CHR(13) & " 4 - Company Four" & CHR(13) & CHR(13) & " 5 - Company Five" & CHR(13) & CHR(13) & " 6 - Company Six" & CHR(13) & CHR(13) & " 7 - Company Seven" & CHR(13) & CHR(13) & " 8 - Company Eight" & CHR(13) & CHR(13) & " Please enter the # and press Enter or click OK."," Company Auto-Data Completion") if (VarType(project) = 8 AND len(project) = 0 OR not IsNumeric(project)) then elseif (VarType(project) = 8 AND len(project) > 0 AND project > 0 AND project < 9 OR VarType(project) = 0) then exit do end if loop if VarType(project) = 0 then msgbox"Cancelled!" & CHR(13) & CHR(13) & "Press Enter or click OK.",vbCritical,"Company Auto-Data Cancelled" cleanup exit sub end if companyinfo autECLSession.autECLPS.SendKeys "[pf15]" autECLSession.autECLPS.WaitForCursor 7,20,3000 autECLSession.autECLPS.SendKeys name1 autECLSession.autECLPS.SetCursorPos 8,20 autECLSession.autECLPS.WaitForCursor 8,20,3000 autECLSession.autECLPS.SendKeys name2 autECLSession.autECLPS.SetCursorPos 9,20 autECLSession.autECLPS.WaitForCursor 9,20,3000 autECLSession.autECLPS.SendKeys name3 autECLSession.autECLPS.SetCursorPos 10,20 autECLSession.autECLPS.WaitForCursor 10,20,3000 autECLSession.autECLPS.SendKeys addr1 autECLSession.autECLPS.SetCursorPos 11,20 autECLSession.autECLPS.WaitForCursor 11,20,3000 autECLSession.autECLPS.SendKeys addr2 autECLSession.autECLPS.SetCursorPos 12,20 autECLSession.autECLPS.WaitForCursor 12,20,3000 autECLSession.autECLPS.SendKeys addr3 autECLSession.autECLPS.SetCursorPos 13,20 autECLSession.autECLPS.WaitForCursor 13,20,3000 autECLSession.autECLPS.SendKeys extension autECLSession.autECLPS.SetCursorPos 13,38 autECLSession.autECLPS.WaitForCursor 13,38,3000 autECLSession.autECLPS.SendKeys phone1 autECLSession.autECLPS.SetCursorPos 13,62 autECLSession.autECLPS.WaitForCursor 13,62,3000 autECLSession.autECLPS.SendKeys phone2 autECLSession.autECLPS.SetCursorPos 14,48 autECLSession.autECLPS.WaitForCursor 14,48,3000 autECLSession.autECLPS.SendKeys "10" autECLSession.autECLPS.SendKeys "[field+]" autECLSession.autECLPS.SetCursorPos 15,60 autECLSession.autECLPS.WaitForCursor 15,60,3000 autECLSession.autECLPS.SendKeys "[field+]" autECLSession.autECLPS.SendKeys "Y" autECLSession.autECLPS.SetCursorPos 17,62 autECLSession.autECLPS.WaitForCursor 17,62,3000 autECLSession.autECLPS.SendKeys "[field+]" autECLSession.autECLPS.SetCursorPos 19,40 autECLSession.autECLPS.WaitForCursor 19,40,3000 autECLSession.autECLPS.SendKeys "[field+]" autECLSession.autECLPS.SetCursorPos 20,40 autECLSession.autECLPS.WaitForCursor 20,40,3000 autECLSession.autECLPS.SendKeys "[field+]" autECLSession.autECLPS.SetCursorPos 14,62 autECLSession.autECLPS.WaitForCursor 14,62,3000 autECLSession.autECLPS.SendKeys "[field+]" autECLSession.autECLPS.SetCursorPos 17,62 autECLSession.autECLPS.WaitForCursor 17,62,3000 autECLSession.autECLPS.SendKeys project autECLSession.autECLPS.SendKeys "[field+]" msgbox"Data entry complete." & CHR(13) & "Please verify information." & CHR(13) & CHR(13) & "Press Enter or click OK.",vbInformation," Finished" cleanup end sub function companyinfo() if project = "1" then name1 = "Company 1 " name2 = "Sales Guy 1 " name3 = "Sales Guy 2 " addr1 = "123 Kitty Kat Lane " addr2 = "Suite 100 " addr3 = "Jacksonville, FL 00000 " extension = "4565 " phone1 = "5557771234 " phone2 = "5557771234 " elseif project = "2" then name1 = "Company 2 " name2 = "Sales Guy 1 " name3 = "Sales Guy 2 " addr1 = "123 Kitty Kat Lane " addr2 = "Suite 100 " addr3 = "Jacksonville, FL 00000 " extension = "4565 " phone1 = "5557771234 " phone2 = "5557771234 " elseif project = "3" then name1 = "Company 3 " name2 = "Sales Guy 1 " name3 = "Sales Guy 2 " addr1 = "123 Kitty Kat Lane " addr2 = "Suite 100 " addr3 = "Jacksonville, FL 00000 " extension = "4565 " phone1 = "5557771234 " phone2 = "5557771234 " elseif project = "4" then name1 = "Company 4 " name2 = "Sales Guy 1 " name3 = "Sales Guy 2 " addr1 = "123 Kitty Kat Lane " addr2 = "Suite 100 " addr3 = "Jacksonville, FL 00000 " extension = "4565 " phone1 = "5557771234 " phone2 = "5557771234 " elseif project = "5" then name1 = "Company 5 " name2 = "Sales Guy 1 " name3 = "Sales Guy 2 " addr1 = "123 Kitty Kat Lane " addr2 = "Suite 100 " addr3 = "Jacksonville, FL 00000 " extension = "4565 " phone1 = "5557771234 " phone2 = "5557771234 " elseif project = "6" then name1 = "Company 6 " name2 = "Sales Guy 1 " name3 = "Sales Guy 2 " addr1 = "123 Kitty Kat Lane " addr2 = "Suite 100 " addr3 = "Jacksonville, FL 00000 " extension = "4565 " phone1 = "5557771234 " phone2 = "5557771234 " elseif project = "7" then name1 = "Company 7 " name2 = "Sales Guy 1 " name3 = "Sales Guy 2 " addr1 = "123 Kitty Kat Lane " addr2 = "Suite 100 " addr3 = "Jacksonville, FL 00000 " extension = "4565 " phone1 = "5557771234 " phone2 = "5557771234 " elseif project = "8" then name1 = "Company 8 " name2 = "Sales Guy 1 " name3 = "Sales Guy 2 " addr1 = "123 Kitty Kat Lane " addr2 = "Suite 100 " addr3 = "Jacksonville, FL 00000 " extension = "4565 " phone1 = "5557771234 " phone2 = "5557771234 " end if end function sub cleanup() set project = nothing set name1 = nothing set name2 = nothing set name3 = nothing set addr1 = nothing set addr2 = nothing set addr3 = nothing set extension = nothing set phone1 = nothing set phone2 = nothing end sub -------------------------- Yeah I know I don't need the cleanup, it's supposed to... I like to do it though.... call me crazy... ;) Edited by raster_gfx - 24 Jan 2010 at 02:35 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 24 Jan 2010 at 08:36 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Sorry raster - I don't mean to seem like I am picking on you. I was just trying to show how the same thing can be accomplished much easier with fewer lines of code.
When we use an Inputbox the are only two things that are of importance in most cases. What is the input? Did the user cancel? This is an issue that even haunts regular programmers. Too ofter many see it as more complex.
The first decision is was the input canceled. It is was then nothing else can be determined as the out put does not exist. If the input WAS NOT canceled then we have the users input. By providing a default value we can allow the user to accept a standard or default answer. All processing after checking for canceled is application specific and is handles as needed. Default. No default, Empty string. String value. These can best be handled via a switch statement except in cases where we want to treat the empty string like cancellation.
YOur example is very complete. Unfortunately it tends, in my opinion, to cloud the fundamental issue of InputBox usage.
We can also shorten and simplify the empty string like this:
If Vartype(return) = 0 OR return = ""Then
MsgBox "Canceled" Else MsgBox return End If The above handles ALL conditions under all circumstance and leaves us with the string value for use in the main program stream.
Sometimes things can become overcomplicated because we fail to define a "main program flow" and an "Exception" flow. By this I mean to point out that a program has a primary flow of execution that is what we are primarily looking for. When we try to handle the exceptions to the main flow we sometimes create complex program structures to deal with what should, under normal circumstances, be seldom used code.
What I am trying to say is that you need to consider the affect of the exception and handle it directly.
If Vartype(return) = 0 OR return = ""Then
' either exit program or loop to tell user to try again. ' force default if user has accidentally cleared input and selected OK.
Else
' normally we would no use anElse as program flow would fall into ' main code stream although a call to a subroutine could be placed her.
End If Of course whatever you have that is clearly working is OK as much of this is about style and about origin of usage. Admins don't really need to do formal analysis and design as much of scripting is for ad-hoc purposes. If it works then use it. I just wanted to post this to try to show you that you can take much simpler approach with InputBox and nearly all other aspects of managing program flow.
Note that the same is true of handling errors. For many the folowing is normal.
If Err.Number <> 0 Then
.'blah blah 100 lines of code
ElseIf Err.Number = 3
' do somthing.
ElseIf Err.Number = 4
' do somthing.
ElseIf Err.Number = 99
' do somthing.
End If This is better handled like this:
If Err.Number = 3
' do somthing.
ElseIf Err.Number = 4
' do somthing.
ElseIf Err.Number = 99
' do somthing.
End If
' no error occured so fall through to main program flow.
' blah blah 100 lines of code
I know it appears to be asubtle difference but understanding this can help to decrease overall program complexity.
There are a number of excellent book written on this and similar issues of coding. They are usually considered too technical for administrative scripting. I still think they can be valuable and time saviong is you have need of large and complex scripts. FOr samller one-time scripts whatever gets it done is fine.
By the way, this:
sub cleanup()
set project = nothing set name1 = nothing set name2 = nothing set name3 = nothing set addr1 = nothing set addr2 = nothing set addr3 = nothing set extension = nothing set phone1 = nothing set phone2 = nothing end sub - is completely wrong by any standards. It won't throw an error but it is not what you really want. You have a number of objects that are string and you are assingning them to a Null object. This can cause erts later on as string tests will no longer work without throwing an error.
If you are trying to deallocate these then that is not necessary as they will cease to exists once the script has finished since they are declared in teh body of the script. If you are just trying to clear the values for say... display; do it with string assignment.
name1 = ""\
It is less likely to cause errors.
This may not be true with this script but bad habits die hard.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 24 Jan 2010 at 10:38 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
As an example of declaritive programming that can make this simpler and easier to maintain consider the following:
As an HTML file: uploads/2491/hrmlcode.txt
As a plain text file: uploads/2491/textcode.txt
[code]
Dim aProjects(8)
' aProjects(0) is skipped for convenience aProjects(1) = Array( _ "Company 1 ", _ "Sales Guy 1 ", _ "Sales Guy 2 ", _ "123 Kitty Kat Lane ", _ "Suite 100 ", _ "Jacksonville, FL 00000 ", _ "4565 ", _ "5557771234 ", _ "5557771234 " _ ) aProjects(2) = Array( _ "Company 2 ", _ "Sales Guy 1 ", _ "Sales Guy 2 ", _ "123 Kitty Kat Lane ", _ "Suite 100 ", _ "Jacksonville, FL 00000 ", _ "4565 ", _ "5557771234 ", _ "5557771234 " _ ) aProjects(3) = Array( _ "Company 3 ", _ "Sales Guy 1 ", _ "Sales Guy 2 ", _ "123 Kitty Kat Lane ", _ "Suite 100 ", _ "Jacksonville, FL 00000 ", _ "4565 ", _ "5557771234 ", _ "5557771234 " _ ) aProjects(4) = Array( _ "Company 4 ", _ "Sales Guy 1 ", _ "Sales Guy 2 ", _ "123 Kitty Kat Lane ", _ "Suite 100 ", _ "Jacksonville, FL 00000 ", _ "4565 ", _ "5557771234 ", _ "5557771234 " _ ) aProjects(5) = Array( _ "Company 5 ", _ "Sales Guy 1 ", _ "Sales Guy 2 ", _ "123 Kitty Kat Lane ", _ "Suite 100 ", _ "Jacksonville, FL 00000 ", _ "4565 ", _ "5557771234 ", _ "5557771234 " _ ) aProjects(6) = Array( _ "Company 6 ", _ "Sales Guy 1 ", _ "Sales Guy 2 ", _ "123 Kitty Kat Lane ", _ "Suite 100 ", _ "Jacksonville, FL 00000 ", _ "4565 ", _ "5557771234 ", _ "5557771234 " _ ) aProjects(7) = Array( _ "Company 7 ", _ "Sales Guy 1 ", _ "Sales Guy 2 ", _ "123 Kitty Kat Lane ", _ "Suite 100 ", _ "Jacksonville, FL 00000 ", _ "4565 ", _ "5557771234 ", _ "5557771234 " _ ) aProjects(8) = Array( _ "Company 8 ", _ "Sales Guy 1 ", _ "Sales Guy 2 ", _ "123 Kitty Kat Lane ", _ "Suite 100 ", _ "Jacksonville, FL 00000 ", _ "4565 ", _ "5557771234 ", _ "5557771234 " _ ) Do return = InputBox("Enter data","My Input") If Vartype(return) = 0 OR return = ""Then MsgBox "Cancelled" WScript.Quit ElseIf CInt(return) < 1 Or CInt(return) > UBound(aProjects) Then MsgBox "Value must be between 1 And " & UBound(aProjects) Else Exit Do End If Loop While True i = CInt(return) 'apply array to variables name1 = aProjects(i)(0) name2 = aProjects(i)(1) name3 = aProjects(i)(2) addr1 = aProjects(i)(3) addr2 = aProjects(i)(4) addr3 = aProjects(i)(5) extension = aProjects(i)(6) phone1 = aProjects(i)(7) phone2 = aProjects(i)(8) MsgBox "You Chose - " & name1 [\code]
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 24 Jan 2010 at 10:40 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Consider also that by using the array technique we can load the project data into the array from CSV file very easily. This allows us to use Excel to create the file of values for the script.
Also consider two things. On is that your SendKeys routine can be generlized, simplidied and shortened significantly however you mioght look into using teh scriptted access to record update and display inyour main application. This would eliminate the need to use SendKeys and should make it more reliable.
Edited by jvierra - 24 Jan 2010 at 10:44 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
raster_gfx
I'm new here
Joined: 23 Jan 2010 Online Status: Offline Posts: 31 |
Quote Reply
Posted: 24 Jan 2010 at 11:27 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Apparently you don't know how iSeries Personal Communications app works or don't use it... the above full code is how the macro works within it and must be formatted that way... it doesn't use .vbs or .hta or any other macro language other than it's .mac files, which can utilize VBSCRIPT as a language....
The purpose of my original code is to show 4 things: 1) If you have an inputbox with 2 selections, if they press OK with no data, it will loop upon itself without throwing an error. 2) If they press Cancel, it will exit the loop and give a Cancel msgbox. 3) If they press either 1 or 2 (the 2 options) it will store that option and exit the loop and continue the script. 4) If they enter anything other than the 3 above scenarios it will loop back upon the inputbox without throwing an error. Clicking OK and Cancel are NOT the same result..... Your array example has an inputbox (with no options) that, when clicking Cancel, it exits and says you cancelled, but if you click OK with no data, it exits and still says you cancelled, which ISN"T correct... As to the full on Sendkeys script, it CAN be used like that with arrays and such, however, variables must be set for each string of data because they are entered into different cursor positions on the screen. Here is a cutdown Inputbox with the full functionality of what I am trying to show. When the inputbox has it's selection, then more code could follow.... do project = inputbox(" 1 - Company One"& CHR(13) & CHR(13) & " 2 - Company Two"," Press ONLY 1 or 2") if (VarType(project) = 8 AND len(project) = 0 OR not IsNumeric(project)) then elseif (VarType(project) = 8 AND len(project) > 0 AND project > 0 AND project < 2 OR VarType(project) = 0) then exit do end if loop if VarType(project) = 0 then msgbox"Cancelled!" & CHR(13) & CHR(13) & "Press Enter or click OK to exit." , vbCritical , "Cancelled" else msgbox"You entered Option "&project end if It will continually loop (AND NOT THROW AN ERROR) if they entered one of these: 0 01 3 z 1' abc 123 If you copy/paste my ORIGINAL code into a blank .vbs file and ran it you would understand how it is determining values that were inputted........ Edited by raster_gfx - 24 Jan 2010 at 11:36 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 24 Jan 2010 at 11:46 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
iSeries on a PC uses the vbscript control and SDK. It is still VBScript and works just like VBScript on any other PC or application.
It doesn't matter. If it works for you then by all means use it the way you are.
VBScript is VBSCript on any PC. IBM has chosent ouse Microsoft VBScript because it is native to the PC and supports access to things withing the PC environment. After all, it was IBM who created both the PC and Microsoft.
We have been using IBM utilites to access mainframe data from PCs for more than two decades now.
Believe me. The InputBox is not that complicated.
If what you have written works for you then you should use it as you like. I was only trying to extend your knowledge of how to script by a bit. If what I have posted does not seem helpful then feel free to ignore it. Someone else might find it useful though.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 24 Jan 2010 at 11:52 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Here is a lonk to teh Macro COnvert Utility that get's you from teh limited MAC world to full vbscript.
Convert Macro
Usage fo vb like utilites in MAC file sis not really addressed by this forum as it is specific to usage within MIcrosoft WSH 5.6 and later and the included version of VCBScript. IBM MAC files are a simple subset of VBScript. The way they execute is, to all intents and purposes, identical to VBScript. Only access to variables has beenlimited to protect the system. Conversion to VBSCcript or XML removes many of these limitations.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
raster_gfx
I'm new here
Joined: 23 Jan 2010 Online Status: Offline Posts: 31 |
Quote Reply
Posted: 24 Jan 2010 at 12:11 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
So I'm going to try this again...
I've searched high and low over the last 2 years trying to find the consummate answer to this riddle... however, looking high and low and nary a straight answer was to be found (I think).... anyways... for those of you that work in certain applications (like iSeries telnet app) that do have vbscript macro functionality... you may have run across putting together an inputbox or 2... and to the reason for this post... the OK and Cancel buttons... how to differentiate between the 2? What if they click Cancel ... what if they click OK without entering any data... what if what if... if you've ever tried to make a foolproof inputbox then you know what I'm referring to... so... here it is... a simple inputbox.. 2 options... loops around and around giving you msgbox's of the choices you made... typing exit will exit the inputbox... simple...... >>>>>>>>>> do project = inputbox("1 - OPTION 1" & CHR(13) & CHR(13) & "2 - OPTION 2" & CHR(13) & CHR(13) &" Please enter 1 or 2 ONLY and press Enter or click OK." & CHR(13) & CHR(13) & " Type EXIT to exit." , "Vbscript Error Routines") if (VarType(project) = 8 AND len(project) = 0 OR not IsNumeric(project)) then if (VarType(project) = 8 AND len(project) = 0) then msgbox"OK selected with no data. Try again." elseif (project = "exit" OR project = "EXIT") then msgbox"Exiting..." exit do elseif not IsNumeric(project) then msgbox"" & project & " is Invalid data, try again." end if elseif (VarType(project) = 8 AND len(project) > 0 OR VarType(project) = 0) then if (len(project) > 0 AND project > 0 AND project < 3) then msgbox"" & project & " selected" elseif VarType(project) = 0 then msgbox"CANCEL selected. NOT CANCELLING HOWEVER!" else msgbox"" & project & " is an Invalid #. Try again." end if end if loop And here is the cutdown version. do project = inputbox("1 - OPTION 1" & CHR(13) & CHR(13) & "2 - OPTION 2" & CHR(13) & CHR(13) &" Please enter 1 or 2 ONLY and press Enter or click OK." & CHR(13) , "Vbscript Error Routines") if (VarType(project) = 8 AND len(project) = 0 OR not IsNumeric(project)) then elseif (VarType(project) = 8 AND len(project) > 0 OR VarType(project) = 0) then if (len(project) > 0 AND project > 0 AND project < 3) then msgbox"" & project & " selected" exit do elseif VarType(project) = 0 then msgbox"CANCELLING." exit do end if end if loop Edited by raster_gfx - 24 Jan 2010 at 12:19 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 24 Jan 2010 at 13:16 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I guess I don't see what teh problem is then. SOrry about that. I always assumed that one test was all that was eeded to determoine bewenn two choices.
If a then x else b.
This is only one test. (if a).
If VarTyp(answer) = 0 Then do thing
Else do other thing
If you are just interested in empty strings then the following is the only test needed. Try it:
return = InputBox("Enter data","My Input","MyDefault")
If return = "" Then MsgBox "Cance; Clicked!" Both VarType 0 and an empty string eveluate the same as you can see here. This is by design for all VB implementations (vba,vb,vbscript) with InpuBox. This has been done to simplify the use of InputBox. All documentation that I haev seen has always made this completely clear.
Here is the text from teh VBScript 5.6 Documentation:
I suggest downloading the VBSript/WSH 5.6 help file from Microsoft. Its' should be posted on the Technet ScriptCenter site.
There are very few errors in the latest version. MOst of teh explanations are well done but may require some thoght and testing of examples to get to see what it is they are trying to do. Most of VBScript is very well designed and works well. Occasionally there are deficiencies or so it may seem. Mostly they are not serious deficiencies in the language fo ra reduced implementation of a compiled language carried over to a script engine.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
raster_gfx
I'm new here
Joined: 23 Jan 2010 Online Status: Offline Posts: 31 |
Quote Reply
Posted: 24 Jan 2010 at 13:58 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I apologize, however, I don't think you see what the problem is... using your script:
return = InputBox("Enter data","My Input","MyDefault") If return = "" Then MsgBox "Cance; Clicked!" If you click Cancel that's all well and good, it will cancel. If you REMOVE THE DEFAULT (which we DONT WANT) and click OK, it will give the "Cance; Clicked!" msgbox... which we both know isn't true... we clicked a blank OK. WE DON'T WANT A DEFAULT (there will be options to select from, 1 or 2 or however many), then clicking OK should loop back upon itself. If you enter any Numeric value other than those OPTIONS available it should loop back upon itself. Try to make an inputbox with 2 options, the user can ONLY select those 2. Clicking Cancel will exit the input box with a CANCEL message. If they click OK with no value, it should loop back upon itself. If they choose xbda then it should loop back upon itself. If they choose 0 or 3 or 5 it should loop back upon itself. If they hit the spacebar and hit Enter or OK it should loop back upon itself. I'm unsure of whether you've actually tried to make a basic inputbox that can differentiate between a BLANK OK or a CANCEL or an input of 0 or even a blank space.... try it... you will find it isn't as 'simplistic' as you would imagine... and don't use MY CODE I created do do it.. try to actually do it first... :) Edited by raster_gfx - 24 Jan 2010 at 14:14 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 24 Jan 2010 at 16:13 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Again - a simple solution
return = InputBox("Enter data","My Input","MyDefault") If 0 = VarType(return) Then MsgBox "Cancel Clicked!" The above will give you a shot at the empty string. Of course if you are mixing apples and oranges thenyuo nee one extra step. Do you see it? Edited by jvierra - 25 Jan 2010 at 07:52 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 24 Jan 2010 at 16:29 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This version always waits for q astriong that is not empty or a cancel.
Still only rewuires one test but we add a conditional to the loop. Yhe empty string is NOT the responsibility of the InputBox btu can be handled in the loop test. The actual outcome has to be handled after the fact but is still reduced to a one off single test.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
rasimmer
Frequent Contributor
Joined: 30 Jan 2009 Location: United States Online Status: Offline Posts: 340 |
Quote Reply
Posted: 25 Jan 2010 at 06:32 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Just a quick note, you can also use IsEmpty(return) vs VarType(return) = 0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6517 |
Quote Reply
Posted: 25 Jan 2010 at 08:17 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
rasimmer - no you can't. It is not the same thing. This is exactly what we are thrashing about.
VarType(0) is not an empty string. This is where many get confused with vbscript variable management and it is a hard issue to try and describe to a non-programmer.
In VBScript ALL variables are Variants. A variant is a wrapper around a type. The type can be many depending on the VB implementation.
VarType 0 is is vbEmpty or "uninitialized" which is not the same as an empty string.
Try the following and you will see what I am talking about:
As you can see there is more going on here than most would assume. In normal admin scripting variable types are usually only of modest concern. Only when we have problems or are looking for good solutions to tricky problems is it necessary to inspect things more carefully.
Most admin scripters never look at variable management in VBScript and, mostly, they don't need to. It is, however, very useful to study this carefully at least once and keep the lessons in mind. WHen things get tricky return to the documentation and see if variable identity can help.
I recommended the VBScript help file. also recommend finding and saving links to some of the many vb and vbscript web resources.
I posted some of this information earlier as a copy of the VBScript documentation that clearly shows how the InpuBox outcome is designed. Unfortunately the authors didn't think it was necessary to discuss it's design in detail. Those of us who have used InputBox in VB, VBA and VBScript are well aware of the subtle details of it's implementation. Unfortunately the novice user will probably miss this subtlety.
The important take away - you should nearly always believe the VBScript documentation.
Her is the full VarType decode along with the Easy to remember VBScript built-in constants. (vbString, vbEmpty, vbInteger, etc)
After fixo9ng my errored post I will repeat it here using teh constant.
return = InputBox("Enter data","My Input")
If vbEmpty = VarType(return) Then MsgBox "Cancel Clicked!" If vbString = VarType(return) Then MsgBox "String returned" If "" = return Then MsgBox "Empty String" What you will notice is that Cancel applies both vbEmpty and empty string but not to vbString. If you clikc OK then vbString applies as well as empty string.
Only vbEmpty aplies when teh Cancel button is clicked. This is what the original poster was trying to figure out but hadn't seen that this was true. It seemed to me that this kind of mistake in understanding is fundamental to scripting. If a user can see this subtlety and how it is implemented in InputBox, then they might begin to understand other areas of VBscript and general programming. YThis kind of design is normal for re-OOP APIs. With OOP we might have a more sophisticated object that would return an object with multiple values just like WMI does. Flat APIs are usually designed to return on integer value with 0 meaning OK in some, one in others and positive/negative in others.
What will the following do(true/false?
MsgBox CBool(1)
MsgBox CBool(0) MsgBox CBool(-1) Testing is a major part of programming. In fact nearly all most programs is taken up testing. This is sometimes called branch logic or switching logic and an IF or SELECT statement is refered to as a switch. Understanding the technical and historical design of "switched logic" systems can help make programming much easier as you canbeter leverage what the engineers have designd.
Much of admin programming is "brute force" programming. This is OK for for most admins as it gets the job done easily and quickly. Unfortunately, msny of these scripts are passed on to newer admins.
I have been called in a few times to modify and fix scipts done by a previous admin. In most cases I have found it easier to re-write these scripts from scratch. Most admin scripts of 100+ lines have been reduced to a dozen or two lines by applying good programming discipline and taking advantage of more COM subsystems like Excel for formatting reports or an HTA for complex user input.
In the end scripting is an admin tool that is loosely implemented to make it easy to use for non-programmers. As a step up from the old DOS batch language it is wonderful and much easier to use. Admins who use vbscript in any way, technically correct or just off-the-cuff, have a great tool that has only recently been upstaged.
Learning a few more technical items about vbScript can only improve ones ability to quickly solve problems with VBScript.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
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 |