Question on Inputbox function

R

rich

Hello All,

I have an input box which queries users on how they want to display some
information. A datasheet on screen, or printed report. My code expects the
user to press "D" or "P". Anything else is ignored. The inputbox has OK
and Cancel buttons, I assume by default. My problem is that when the user
clicks on the Cancel button, nothing happens. The prompt remains. So, my
question is how do you programmatically handle clicking the OK or Cancel
button on an input box?

TIA,

Rich
 
A

Allen Browne

If the user cancels, InputBox() returns a zero-length string, so code like
this:

Function TestInput()
Dim strInput As String

Do
strInput = InputBox("Enter D or P")
Loop Until strInput = "D" Or strInput = "P" Or strInput = vbNullString

If strInput <> vbNullString Then
'Do your stuff.
End If
End Function
 
R

rich

Good Morning Allen,

Thanks very much for the reply. I do understand. However, 1 quick
follow-up question. What happens, or what is returned if user clicks OK?
When I click OK without pressing a key, it appears that a vbNullString is
also returned, just like when hitting Cancel. My test code seems to indicate
that.

Thanks again,
Rich
 
A

Allen Browne

Yes, that's correct: Ok returns a zero-length string if the user input
nothing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top