PC Review


Reply
Thread Tools Rate Thread

How to activate InputBox Cancel Function

 
 
Chua
Guest
Posts: n/a
 
      8th Aug 2008
I use InputBox to display a simple dialog box so that User can enter
information to be used in a macro. The dialog box has an OK button and a
Cancel button. How do I activate the Cancel button to perform the function
that I want?

I tried below code but it will do nothing and exit the program even when
User click the OK button.

a = InputBox ("Enter Password to Proceed:")

If vbCancel = True Then
Do nothing
End If

If a <> "12345" Then
MsgBox "INVALID PASSWORD !!!" & vbNewLine & "You have no administrator
rights to access the function.", vbCritical

End
End If
 
Reply With Quote
 
 
 
 
NateBuckley
Guest
Posts: n/a
 
      8th Aug 2008
I think when you click cancel it simple returns a 0 length string. So the
only way you can test if they clicked cancel is by checking the string that
is returned. Like so

Public Sub cancelInput()
Dim a As String
Dim hasPassword As Boolean
'Begin a loop that won't end till the user has inputted a password
While hasPassword = False
'assign the variable a to what ever Inputbox returns.
a = InputBox("Enter Password")
'If a's length is 0 then the user either clicked cancel
'or clicked OK with nothing entered.
If Len(a) = 0 Then
MsgBox ("Cancel or no password entered")
Else
MsgBox ("Your password is " & a)
hasPassword = True
End If
Wend
End Sub

Apart from checking the lenght or if a = "" don't think you can test it any
other way.

"Chua" wrote:

> I use InputBox to display a simple dialog box so that User can enter
> information to be used in a macro. The dialog box has an OK button and a
> Cancel button. How do I activate the Cancel button to perform the function
> that I want?
>
> I tried below code but it will do nothing and exit the program even when
> User click the OK button.
>
> a = InputBox ("Enter Password to Proceed:")
>
> If vbCancel = True Then
> Do nothing
> End If
>
> If a <> "12345" Then
> MsgBox "INVALID PASSWORD !!!" & vbNewLine & "You have no administrator
> rights to access the function.", vbCritical
>
> End
> End If

 
Reply With Quote
 
Chua
Guest
Posts: n/a
 
      8th Aug 2008
Hi Nate,

Thanks for sharing. I have tried this method before and it do works. I am
just wondering whether InputBox function can have similar function like
MsgBox that allows User to write different codes for OK button, Cancel
button....etc any other buttons.

Cheers
Chua


"NateBuckley" wrote:

> I think when you click cancel it simple returns a 0 length string. So the
> only way you can test if they clicked cancel is by checking the string that
> is returned. Like so
>
> Public Sub cancelInput()
> Dim a As String
> Dim hasPassword As Boolean
> 'Begin a loop that won't end till the user has inputted a password
> While hasPassword = False
> 'assign the variable a to what ever Inputbox returns.
> a = InputBox("Enter Password")
> 'If a's length is 0 then the user either clicked cancel
> 'or clicked OK with nothing entered.
> If Len(a) = 0 Then
> MsgBox ("Cancel or no password entered")
> Else
> MsgBox ("Your password is " & a)
> hasPassword = True
> End If
> Wend
> End Sub
>
> Apart from checking the lenght or if a = "" don't think you can test it any
> other way.
>
> "Chua" wrote:
>
> > I use InputBox to display a simple dialog box so that User can enter
> > information to be used in a macro. The dialog box has an OK button and a
> > Cancel button. How do I activate the Cancel button to perform the function
> > that I want?
> >
> > I tried below code but it will do nothing and exit the program even when
> > User click the OK button.
> >
> > a = InputBox ("Enter Password to Proceed:")
> >
> > If vbCancel = True Then
> > Do nothing
> > End If
> >
> > If a <> "12345" Then
> > MsgBox "INVALID PASSWORD !!!" & vbNewLine & "You have no administrator
> > rights to access the function.", vbCritical
> >
> > End
> > End If

 
Reply With Quote
 
NateBuckley
Guest
Posts: n/a
 
      8th Aug 2008
Unfortunetly unlike the MsgBox which returns an integer and that value
depending if you clicked Ok,cancel etc, Inputbox just returns a string.

Perhaps there is a way but I'm not aware of one. You could just create your
own userform and make it act like a Inputbox that performs as you wish.



"Chua" wrote:

> Hi Nate,
>
> Thanks for sharing. I have tried this method before and it do works. I am
> just wondering whether InputBox function can have similar function like
> MsgBox that allows User to write different codes for OK button, Cancel
> button....etc any other buttons.
>
> Cheers
> Chua
>
>
> "NateBuckley" wrote:
>
> > I think when you click cancel it simple returns a 0 length string. So the
> > only way you can test if they clicked cancel is by checking the string that
> > is returned. Like so
> >
> > Public Sub cancelInput()
> > Dim a As String
> > Dim hasPassword As Boolean
> > 'Begin a loop that won't end till the user has inputted a password
> > While hasPassword = False
> > 'assign the variable a to what ever Inputbox returns.
> > a = InputBox("Enter Password")
> > 'If a's length is 0 then the user either clicked cancel
> > 'or clicked OK with nothing entered.
> > If Len(a) = 0 Then
> > MsgBox ("Cancel or no password entered")
> > Else
> > MsgBox ("Your password is " & a)
> > hasPassword = True
> > End If
> > Wend
> > End Sub
> >
> > Apart from checking the lenght or if a = "" don't think you can test it any
> > other way.
> >
> > "Chua" wrote:
> >
> > > I use InputBox to display a simple dialog box so that User can enter
> > > information to be used in a macro. The dialog box has an OK button and a
> > > Cancel button. How do I activate the Cancel button to perform the function
> > > that I want?
> > >
> > > I tried below code but it will do nothing and exit the program even when
> > > User click the OK button.
> > >
> > > a = InputBox ("Enter Password to Proceed:")
> > >
> > > If vbCancel = True Then
> > > Do nothing
> > > End If
> > >
> > > If a <> "12345" Then
> > > MsgBox "INVALID PASSWORD !!!" & vbNewLine & "You have no administrator
> > > rights to access the function.", vbCritical
> > >
> > > End
> > > End If

 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      8th Aug 2008
check out the inputbox method in vb help

Option Explicit
Sub test()
Dim result As String
result = Application.InputBox("Enter String", "Test", , , , , , 2)
If result = False Then
MsgBox "cancel pressed"
Exit Sub
Else
MsgBox result
End If
End Sub

--


Gary


"Chua" <(E-Mail Removed)> wrote in message
news:6C004D5F-E54F-479F-86AD-(E-Mail Removed)...
>I use InputBox to display a simple dialog box so that User can enter
> information to be used in a macro. The dialog box has an OK button and a
> Cancel button. How do I activate the Cancel button to perform the function
> that I want?
>
> I tried below code but it will do nothing and exit the program even when
> User click the OK button.
>
> a = InputBox ("Enter Password to Proceed:")
>
> If vbCancel = True Then
> Do nothing
> End If
>
> If a <> "12345" Then
> MsgBox "INVALID PASSWORD !!!" & vbNewLine & "You have no administrator
> rights to access the function.", vbCritical
>
> End
> End If



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cancel InputBox Alfredo_CPA Microsoft Excel Programming 6 8th Jun 2009 08:00 PM
Cancel an InputBox? =?Utf-8?B?Q2hyaXMgQnVybmV0dGU=?= Microsoft Access Form Coding 4 4th Dec 2005 08:14 PM
InputBox Function & Cancel Otto Moehrbach Microsoft Excel Programming 4 30th Sep 2004 01:13 AM
InputBox Cancel Wembly Microsoft Access VBA Modules 8 15th Jan 2004 05:09 AM
InputBox Cancel Bob Microsoft VB .NET 3 9th Nov 2003 07:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:36 PM.