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!
Pearlyn TanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.