6

I'm building a VB application that accepts numbers in the beginning. I want to be able to exit if the user presses the cancel button in any of the input boxes. The code is :

Private Sub Command1_Click()
Dim a, b, c, d As Integer
Dim response As Integer
Dim a1, b1, c1, d1 As String
a = InputBox("Enter Numerator 1")
b = InputBox("Enter Denominator 1")
c = InputBox("Enter Numerator 2")
d = InputBox("Enter Denominator 2")

a1 = Str(a)
b1 = Str(b)
c1 = Str(c)
d1 = Str(d)

If a1 <> "" And b1 <> "" And c1 <> "" And d1 <> "" Then
'All Actions
...
Else

 response = MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, AdditionV1.0")
 If response = vbYes Then
 End
 Else
 Addition.Show
 End If

I've tried using StrPtr and it stil doesnt work. What happens is even if I press Cancel, it still displays the error message.

Help will really be appreciated.

Deanna
  • 23,876
  • 7
  • 71
  • 156
Bharat Kashyap
  • 73
  • 1
  • 2
  • 5

2 Answers2

7

StrPtr is the way to go. Since you didn’t show the relevant code, there is no telling what you did wrong (but there are several errors in the code anyway). In principle, the following works:

Dim a As String
a = InputBox("Enter Numerator 1")
If StrPtr(a) = 0 Then
    ' Nothing was entered.
End If

I suspect that you applied the check to a1 etc. instead of the original variables. Not only does this not work (Str forces the string to be non-null), it also makes no sense: what are those variables for, anyway?

Furthermore, all your variable declarations are wrong. The following:

Dim a, b, c, d As Integer

declares a, b and c as Variant. Only d will be an Integer. For this, and for other reasons (readability), never declare multiple variables in one statement. Always declare them separately. Oh, and use meaningful names. a, b, c, d aren’t helpful.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Why StrPtr(a) when Len(a) is enough? – Maestro Jun 11 '13 at 13:00
  • @Joshua Because that *isn’t* enough. You want to distinguish between *empty* input and *no input at all* (i.e. Cancel button). Testing for `Len(a) = ` is the same as `a = ""` and, as OP noted, doesn’t make the necessary distinction. – Konrad Rudolph Jun 11 '13 at 13:55
  • I personally never came across any code using inputboxes, where it was important to distinguish between the two. In cases where user input is not 100% required, an inputbox is a very bad solution, and in all other cases empty input can be used to cancel. And that the OP wanted to distinguish wasnt clear for me from the question neither. But now I understand why you use StrPtr. – Maestro Jun 11 '13 at 20:30
  • @Joshua If the case were not required then the box wouldn’t have a “Cancel” button. There are plenty of cases where a blank input is *valid* and distinct from cancelling an action (although OP’s isn’t one, and is obviously a bad use-case for an input box anyway) – such as whenever a user is prompted to edit an *existing* value that may be empty (“Enter your new favourite food”), where “Cancel” would obviously revert to the old value, rather than saving a new empty value. – Konrad Rudolph Jun 11 '13 at 21:05
0

If I understand your question correctly, you want to stop processing the moment user presses a cancel button.

I would suggest doing something like this (Note that I changed the variables to a1, b1,c1,d1 of assignment from InputBox):

    Private Sub Command1_Click()
    Dim a, b, c, d As Integer
    Dim response As Integer
    Dim a1, b1, c1, d1 As String

    a1 = InputBox("Enter Numerator 1")
    if (a1 = "")
       exit sub
    endif

    b1 = InputBox("Enter Denominator 1")
    if (b1 = "")
       exit sub
    endif

    c1 = InputBox("Enter Numerator 2")
    if (c1 = "")
       exit sub
    endif

    d1 = InputBox("Enter Denominator 2")
    if (d1 = "")
       exit sub
    endif


    If a1 <> "" And b1 <> "" And c1 <> "" And d1 <> "" Then
    'All Actions
    ...
    Else

     response = MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, AdditionV1.0")
     If response = vbYes Then
     End
     Else
     Addition.Show
     End If
Gopal Nair
  • 830
  • 4
  • 7
  • Thank You both, I changed the Data Types of the numerators and denominators to strings and StrPtr worked wonders. Konrad, I am starting to learn Python and this was just a question I had tried to attempt months ago and stumbled upon today. Nevertheless, I have taken all your tips into account and will do as you told from now onward. Also, could you guide me as to where to start learning Python from or whether I should learn Python in the first place, or C++. I'm Bharat from India, and I'm in the ninth grade. – Bharat Kashyap Jan 01 '12 at 11:26