+ Reply to Thread
Results 1 to 6 of 6

Inputbox question

  1. #1
    Forum Contributor
    Join Date
    09-02-2005
    Posts
    146

    Inputbox question

    When using an inputbox, how do you keep it from generating an error if the user clicks cancel? I want them to be able to cancel it, just not have to think about whether they should end or debug.

    So far I've had no luck with things such as

    .....
    if VBcancel then end
    elseif
    .....

    because that cuts my program short. What is the trick?

  2. #2
    Norman Jones
    Guest

    Re: Inputbox question

    Hi Wilro85,

    Try something like:

    '=====================>>
    Public Sub TesterX()
    Dim sStr As String

    sStr = InputBox(Prompt:="Please type your name", _
    Title:="InputBox Demo")
    If StrPtr(sStr) = 0 Then
    MsgBox "You pressed Cancel"
    Else
    If Len(sStr) = 0 Then
    MsgBox "OK was pressed but no entry was made."
    Else
    MsgBox "Your entry was: " & sStr
    End If
    End If
    End Sub
    '<<=====================


    ---
    Regards,
    Norman



    "wilro85" <[email protected]> wrote in
    message news:[email protected]...
    >
    > When using an inputbox, how do you keep it from generating an error if
    > the user clicks cancel? I want them to be able to cancel it, just not
    > have to think about whether they should end or debug.
    >
    > So far I've had no luck with things such as
    >
    > ....
    > if VBcancel then end
    > elseif
    > ....
    >
    > because that cuts my program short. What is the trick?
    >
    >
    > --
    > wilro85
    > ------------------------------------------------------------------------
    > wilro85's Profile:
    > http://www.excelforum.com/member.php...o&userid=26935
    > View this thread: http://www.excelforum.com/showthread...hreadid=466059
    >




  3. #3
    George Nicholson
    Guest

    Re: Inputbox question

    Two possible approachs:

    1) If you want them to explicitly specify that they want to cancel:

    Do While strTemp = ""
    strTemp = InputBox("Enter a value or CANCEL to Abort...",
    "MyCaption", "MyDefault")
    Loop

    Select Case uCase(Trim(strTemp))
    Case "CANCEL"
    Exit Sub
    Case Else
    ' Do something else
    End Select

    2): If you don't want to be that demanding (and relying on fact that the
    InputBox returns an empty string if nothing is input):

    strTemp = InputBox("Enter a value or CANCEL to Abort...", "MyCaption",
    "MyDefault")

    Select Case uCase(Trim(strTemp))
    Case "CANCEL", ""
    Exit Sub
    Case Else
    ' Do something else
    End Select

    HTH,
    --
    George Nicholson

    Remove 'Junk' from return address.


    "wilro85" <[email protected]> wrote in
    message news:[email protected]...
    >
    > When using an inputbox, how do you keep it from generating an error if
    > the user clicks cancel? I want them to be able to cancel it, just not
    > have to think about whether they should end or debug.
    >
    > So far I've had no luck with things such as
    >
    > ....
    > if VBcancel then end
    > elseif
    > ....
    >
    > because that cuts my program short. What is the trick?
    >
    >
    > --
    > wilro85
    > ------------------------------------------------------------------------
    > wilro85's Profile:
    > http://www.excelforum.com/member.php...o&userid=26935
    > View this thread: http://www.excelforum.com/showthread...hreadid=466059
    >




  4. #4
    ArthurJ
    Guest

    Re: Inputbox question

    Norman, this works pefectly.
    What is strPtr? I pressed F1 and got "no help available".
    Art

    "Norman Jones" wrote:

    > Hi Wilro85,
    >
    > Try something like:
    >
    > '=====================>>
    > Public Sub TesterX()
    > Dim sStr As String
    >
    > sStr = InputBox(Prompt:="Please type your name", _
    > Title:="InputBox Demo")
    > If StrPtr(sStr) = 0 Then
    > MsgBox "You pressed Cancel"
    > Else
    > If Len(sStr) = 0 Then
    > MsgBox "OK was pressed but no entry was made."
    > Else
    > MsgBox "Your entry was: " & sStr
    > End If
    > End If
    > End Sub
    > '<<=====================
    >
    >
    > ---
    > Regards,
    > Norman
    >
    >
    >
    > "wilro85" <[email protected]> wrote in
    > message news:[email protected]...
    > >
    > > When using an inputbox, how do you keep it from generating an error if
    > > the user clicks cancel? I want them to be able to cancel it, just not
    > > have to think about whether they should end or debug.
    > >
    > > So far I've had no luck with things such as
    > >
    > > ....
    > > if VBcancel then end
    > > elseif
    > > ....
    > >
    > > because that cuts my program short. What is the trick?
    > >
    > >
    > > --
    > > wilro85
    > > ------------------------------------------------------------------------
    > > wilro85's Profile:
    > > http://www.excelforum.com/member.php...o&userid=26935
    > > View this thread: http://www.excelforum.com/showthread...hreadid=466059
    > >

    >
    >
    >


  5. #5
    Chip Pearson
    Guest

    Re: Inputbox question

    StrPtr is an undocumented function that returns the memory
    address of a string variable. Similar undocumented functions
    include VarPtr (for any type of variable) and ObjPtr (for object
    variables).


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "ArthurJ" <[email protected]> wrote in message
    news:[email protected]...
    > Norman, this works pefectly.
    > What is strPtr? I pressed F1 and got "no help available".
    > Art
    >
    > "Norman Jones" wrote:
    >
    >> Hi Wilro85,
    >>
    >> Try something like:
    >>
    >> '=====================>>
    >> Public Sub TesterX()
    >> Dim sStr As String
    >>
    >> sStr = InputBox(Prompt:="Please type your name", _
    >> Title:="InputBox Demo")
    >> If StrPtr(sStr) = 0 Then
    >> MsgBox "You pressed Cancel"
    >> Else
    >> If Len(sStr) = 0 Then
    >> MsgBox "OK was pressed but no entry was made."
    >> Else
    >> MsgBox "Your entry was: " & sStr
    >> End If
    >> End If
    >> End Sub
    >> '<<=====================
    >>
    >>
    >> ---
    >> Regards,
    >> Norman
    >>
    >>
    >>
    >> "wilro85"
    >> <[email protected]> wrote
    >> in
    >> message
    >> news:[email protected]...
    >> >
    >> > When using an inputbox, how do you keep it from generating
    >> > an error if
    >> > the user clicks cancel? I want them to be able to cancel
    >> > it, just not
    >> > have to think about whether they should end or debug.
    >> >
    >> > So far I've had no luck with things such as
    >> >
    >> > ....
    >> > if VBcancel then end
    >> > elseif
    >> > ....
    >> >
    >> > because that cuts my program short. What is the trick?
    >> >
    >> >
    >> > --
    >> > wilro85
    >> > ------------------------------------------------------------------------
    >> > wilro85's Profile:
    >> > http://www.excelforum.com/member.php...o&userid=26935
    >> > View this thread:
    >> > http://www.excelforum.com/showthread...hreadid=466059
    >> >

    >>
    >>
    >>




  6. #6
    Norman Jones
    Guest

    Re: Inputbox question

    Hi Art,

    > What is strPtr? I pressed F1 and got "no help available".


    See Karl Peterson's site:


    http://vb.mvps.org/tips/varptr.asp


    ---
    Regards,
    Norman



    "ArthurJ" <[email protected]> wrote in message
    news:[email protected]...
    > Norman, this works pefectly.
    > What is strPtr? I pressed F1 and got "no help available".
    > Art
    >
    > "Norman Jones" wrote:
    >
    >> Hi Wilro85,
    >>
    >> Try something like:
    >>
    >> '=====================>>
    >> Public Sub TesterX()
    >> Dim sStr As String
    >>
    >> sStr = InputBox(Prompt:="Please type your name", _
    >> Title:="InputBox Demo")
    >> If StrPtr(sStr) = 0 Then
    >> MsgBox "You pressed Cancel"
    >> Else
    >> If Len(sStr) = 0 Then
    >> MsgBox "OK was pressed but no entry was made."
    >> Else
    >> MsgBox "Your entry was: " & sStr
    >> End If
    >> End If
    >> End Sub
    >> '<<=====================
    >>
    >>
    >> ---
    >> Regards,
    >> Norman
    >>
    >>
    >>
    >> "wilro85" <[email protected]> wrote in
    >> message news:[email protected]...
    >> >
    >> > When using an inputbox, how do you keep it from generating an error if
    >> > the user clicks cancel? I want them to be able to cancel it, just not
    >> > have to think about whether they should end or debug.
    >> >
    >> > So far I've had no luck with things such as
    >> >
    >> > ....
    >> > if VBcancel then end
    >> > elseif
    >> > ....
    >> >
    >> > because that cuts my program short. What is the trick?
    >> >
    >> >
    >> > --
    >> > wilro85
    >> > ------------------------------------------------------------------------
    >> > wilro85's Profile:
    >> > http://www.excelforum.com/member.php...o&userid=26935
    >> > View this thread:
    >> > http://www.excelforum.com/showthread...hreadid=466059
    >> >

    >>
    >>
    >>




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1