• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 62
  • Last Modified:

Configure input box on VBA

Hi, I'm trying to create an input box that will exit the sub if the user clicks on "cancel". Here is my current code, but it ends the sub when the user does not key in anything and clicks 'OK'.

Dim strInput As String
strInput = InputBox("Key in Item code")
If strInput = vbNullString Then
 MsgBox ("Cancelled by User")
 Exit Sub
End If

Open in new window


How do I improve it such that clicking "cancel" will exit the sub, yet clicking "ok" when the user had not keyed in anything will prompt a different message ("Please key in Item code")?

Thanks in advance!
0
Pearlyn Tan
Asked:
Pearlyn Tan
  • 3
  • 3
2 Solutions
 
Rgonzo1971Commented:
Hi,

pls try
Dim strInput As String
strInput = Application.InputBox("Key in Item code")
If strInput = "False" Then
 MsgBox ("Cancelled by User")
 Exit Sub
End If

Open in new window

Regards
0
 
Ryan ChongCommented:
try use function StrPtr:

Dim strInput As String
    strInput = InputBox("Key in Item code")
    If StrPtr(strInput) = 0 Then
        MsgBox ("Cancelled by User")
        Exit Sub
    End If

Open in new window

0
 
Rgonzo1971Commented:
Corrected code
Dim strInput As String
strInput = Application.InputBox("Key in Item code")
If strInput = "False" Then
    MsgBox ("Cancelled by User")
    Exit Sub
End If
Do
    strInput = Application.InputBox("Please Key in Item code")
    If strInput = "False" Then
        MsgBox ("Cancelled by User")
        Exit Sub
    End If
Loop While strInput = ""

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ryan ChongCommented:
How do I improve it such that clicking "cancel" will exit the sub, yet clicking "ok" when the user had not keyed in anything will prompt a different message ("Please key in Item code")?

try this instead:

Sub test()
    Dim strInput As String
    strInput = InputBox("Key in Item code")
    If StrPtr(strInput) = 0 Then
        MsgBox ("Cancelled by User")
        Exit Sub
    ElseIf Trim(strInput) = "" Then
        MsgBox ("Please key in Item code")
        Exit Sub
    End If
    
    'Your following codes here...
End Sub

Open in new window

0
 
Rgonzo1971Commented:
My code but shorter
Dim strInput As String
Dim b1stTime As Boolean
b1stTime = True
Do
    strInput = Application.InputBox(IIf(b1stTime, "Key in Item code", "Please Key in Item code"))
    If strInput = "False" Then
        MsgBox ("Cancelled by User")
        Exit Sub
    End If
    b1stTime = False
Loop While strInput = ""

Open in new window

0
 
Ryan ChongCommented:
ok, but the code will error if user keyed in "False", it makes an expression that it was cancelled by user while user actually entered "False".
0
 
Pearlyn TanAuthor Commented:
Hi Rgonzo1971 and Ryan, thanks so much for both your codes, all worked perfectly. Personally preferred one with a message box prompt if user clicks ok with no input, but nonetheless I appreciate both. Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now