Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with vbs InputBox Cancel button

1,741 views
Skip to first unread message

D.P. Roberts

unread,
Mar 18, 2005, 1:29:31 AM3/18/05
to
I have a vbscript that uses an InputBox. It works fine if the user enters
data and clicks OK, but if the user clicks Cancel I get the following error:

Line: 4
Char: 1
Error: 0x80041021
Code: 80041021
Source: (null)

Anyone know what's causing this error? Here's the code for my inputbox:
dim objProcess, strComputer, strProcess
strComputer = InputBox("Enter computer name or IP address", "Identify PC to
receive GPupdate /force")

Set objProcess = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
_
strComputer & "\root\cimv2:Win32_Process")

Dim intResult, intProcessID
intResult = objProcess.Create("gpupdate /force", null, null, intProcessID)

MsgBox"GPupdate has completed on the machine you
specified",vbInformation,"GPupdate Complete!"


Björn Holmgren

unread,
Mar 18, 2005, 3:14:40 AM3/18/05
to
"D.P. Roberts" <dpro...@pbride.com> wrote in message
news:eoxTZP4K...@TK2MSFTNGP14.phx.gbl...


This is one of the problems with the InputBox function. If the user clicks
"cancel" the function returns a zero-length string, so you have to check for
that before proceeding. The problem is that you don't know if the user
clicked "cancel" or just left the textbox blank and clicked "OK". The result
is the same. You'd be better off creating your own input form.

--
Björn Holmgren

Björn Holmgren

unread,
Mar 18, 2005, 3:16:52 AM3/18/05
to
"Björn Holmgren" <bjo...@hotmail.com> wrote in message
news:Z5w_d.35172$Of5....@nntpserver.swip.net...

> This is one of the problems with the InputBox function. If the user clicks
> "cancel" the function returns a zero-length string, so you have to check
for
> that before proceeding. The problem is that you don't know if the user
> clicked "cancel" or just left the textbox blank and clicked "OK". The
result
> is the same. You'd be better off creating your own input form.


Sorry! Forgot this is a scripting NG. Just forget the part about creating a
custom input form.

--
Björn Holmgren

Keith Miller

unread,
Mar 18, 2005, 3:23:29 AM3/18/05
to
Download the documentation:

http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en

You'll see that:

If the user clicks OK or presses ENTER, the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string ("").

You need to verify the value of strComputer before calling GetObject

Keith

"D.P. Roberts" <dpro...@pbride.com> wrote in message news:eoxTZP4K...@TK2MSFTNGP14.phx.gbl...

Joe Fawcett

unread,
Mar 18, 2005, 4:30:50 AM3/18/05
to
"Björn Holmgren" <bjo...@hotmail.com> wrote in message
news:Z5w_d.35172$Of5....@nntpserver.swip.net...
I believe michael Harris taught me this one:

Dim vResponse

vResponse = InputBox("Enter Name:", "Data Entry")

If IsEmpty(vResponse) Then

MsgBox "No data entered", vbExclamation, "Cancel Pressed"

ElseIf Len(vResponse) = 0 Then

MsgBox "Ah! The man with no name.", vbInformation, "OK pressed"

Else

MsgBox "Greetings '" & vResponse & "'.", vbInformation, "OK pressed"

End If


--

Joe (MVP)


Björn Holmgren

unread,
Mar 18, 2005, 4:44:52 AM3/18/05
to
"Joe Fawcett" <joefa...@newsgroups.nospam> wrote in message
news:uvqey05K...@tk2msftngp13.phx.gbl...

Interesting. In VB, Inputbox always returns a zero-length string, even when
cancel is pressed. I had assumed it would be the same in VBScript, but
obviously not. I've learned something new. Thanx for that, Joe!

--
Björn Holmgren

D.P. Roberts

unread,
Mar 18, 2005, 10:14:41 AM3/18/05
to
Thanks for all the quick help, everyone. I inserted the following right
after the inputbox function and now it works great:

If strComputer = vbNullString Then
wscript.quit(1)
End If


"Keith Miller" <k.mil...@verizon.net> wrote in message
news:e4KmDP5K...@TK2MSFTNGP10.phx.gbl...

asd

unread,
Sep 21, 2007, 4:15:51 AM9/21/07
to
sad

Kerry H

unread,
Feb 21, 2008, 11:58:55 AM2/21/08
to
I haven't gotten any of these to work correctly. However, they did give me
the idea for this method that I discovered will work well.

EXAMPLE:

usrSelect = InputBox("Select a number:" & VbCrLf & _
VbCrLf & "1. Option 1" & _
VbCrLf & "2. Option 2" & _
VbCrLf & "3. Option 3", strScriptName," ")

If usrSelect = vbNullString then
MsgBox "CANCEL was selected.", vbOKOnly, strScriptName
WScript.Quit
ElseIf trim(usrTermSelect) = "" Then
MsgBox "Please enter a value before clicking OK.", vbOKOnly,
strScriptName
Else
Select Case usrTermSelect
Case 1
Case 2
Case 3
Case Else 'for all out of range values
MsgBox "Invalid selection. Please try again.", vbOKOnly, strScriptName
End Select
End If

END EXAMPLE

The key to this is the space character that is used as default text for the
input box. Technically, having anything in the input box as a default
(including out of range values, or a special string such as "DEFAULT") can
help with this situation as you can check to see if the default has been
changed. If not, then the user likely did not enter anything before
clicking OK. However, if you are looking to have no visible text in the
input box, use the method above. It will only put a space character in the
input area of the dialog box, although the space will be highlighted for
selection. If CANCEL is selected, then a null string is returned. If OK is
selected without the user entering anything, then the space character is
returned (not an empty string).

The line:
ElseIf trim(usrTermSelect) = "" Then

could easily be replaced with:
ElseIf usrTermSelect = " " Then

or this:
ElseIf usrTermSelect = "default" Then

if you choose the default text to be "default" as in the following:

usrSelect = InputBox("Select a number:" & VbCrLf & _
VbCrLf & "1. Option 1" & _
VbCrLf & "2. Option 2" & _
VbCrLf & "3. Option 3", strScriptName, "default")

Hope this helps!

url:http://www.ureader.com/msg/1675187.aspx

0 new messages