What type is vbCancel?

S

Shatin

In the code fragment below, when I push the cancel button in response to the
inputbox, I get a type mismatch error. What type is vbCancel?

Dim myDate as String

Do
myDate = InputBox("Please enter your name.")
If myDate = vbCancel Then
Exit Sub
ElseIf myDate = "" Then
MsgBox ("You didn't enter anything. Please try again.")
End If
Loop While myDate = ""

In the same macro, I have these similar lines which work OK:

Dim myCheck as String

mycheck = MsgBox("Proceed?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
End If

Why don't I get any type mismatch error with vbNo?
 
B

Bob Phillips

Hi Shatin,

It's an integer. As you want a date, declare it as variant to handle both
types.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
S

Steven Fatzinger

Shatin said:
In the code fragment below, when I push the cancel button in response to the
inputbox, I get a type mismatch error. What type is vbCancel?

Dim myDate as String

Do
myDate = InputBox("Please enter your name.")
If myDate = vbCancel Then
Exit Sub
ElseIf myDate = "" Then
MsgBox ("You didn't enter anything. Please try again.")
End If
Loop While myDate = ""

In the same macro, I have these similar lines which work OK:

Dim myCheck as String

mycheck = MsgBox("Proceed?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
End If

Why don't I get any type mismatch error with vbNo?
Believe it or not, you should always check the help file first.
The InputBox returns a String, MsgBox returns an Integer.
Both vbNo and vbCancel are Integers.
Your InputBox line places a string in myDate.
You then compare that string to vbNo, which is an Integer
It would be better to use the following test:
If Len(myDate)=0 Then Exit Sub
 
H

Helen Trim

vbCancel is an integer, but that's a red herring here.
You get an integer returned from MsgBox if you use it with
buttons, and that is what vbCancel is for.

For an inputbox, if you press the Cancel button, it reurns
an empty string. So you only need the test for empty
string, which you already have.

Do
myDate = InputBox("Please enter your name.")
If myDate = "" Then
MsgBox ("You didn't enter anything. Please try
again.")
End If
Loop While myDate = ""

HTH
Helen
 
C

Chip Pearson

Just for the record, vbCancel and all other results of a MsgBox
are Longs, not Integers.
 
S

Steven Fatzinger

Chip said:
Just for the record, vbCancel and all other results of a MsgBox
are Longs, not Integers.




response to the
Technically, you are correct for 32 bit systems.
However, Help still states that it returns an Integer.
Plus, I didn't want to confuse the issue by introducing the Long versus
Integer discussion.
 

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