Anti-Bug If/Then Makes a Bug 
Author Message
 Anti-Bug If/Then Makes a Bug

I'm using input boxes that are working fine for getting data from the
user.  However, I haven't been able to to make things run right when
the user hits the cancel button.    When the user hits cancel, I want
that event to send him to the end of the subroutine to a place named
"dump" that looks like this:

Dump: MsgBox "Never mind"

Here is the code that works as long as the user doesn't hit the cancel
button:

strStringName = _
    InputBox("What is your string variable's name?", "?", "strFName")

intNum = _
    InputBox("How many names do you want to enter?", "Names", "20")

When the code is like that, the following happens:
If the user hits cancel on the first input box, the next input box
simply comes up.  If the user hits cancel on that one, I get this
error: Run type errors 13 -- type mismatch.  

I expected that since there's no coding for vbCancel.  However, I
still get problems when I change the code as follows:

strStringName = _
    InputBox("What is your string variable's name?", "?", "strFName")
    If strStringName = vbCancel Then
        GoTo Dump
    End If

    intNum = _
    InputBox("How many names do you want to enter?", "Names", "20")
    If intNum = vbCancel Then
        GoTo Dump
    End If

On the first inputbox, I get that same run type error whether the user
hits OK or cancel.  If I delete that If/Then/GoTo statement, the
second inputbox gives me that error only if the cancel button is hit.
Both variables are declared locally.  I thought the first inputbox's
troubles might be because that variable is declared as a string rather
than as an integer.  However, it did not work when I tried declaring
it as an integer.  Besides, my program needs strStringName's
information in string format.  The second inputbox's variable is
indeed an integer; however, it does not run either.  

What gives?  There must be someting I'm overlooking.  I haven't
figured out what.  

Any help is greatly appreciated.  

Love, Dina
********************************************
My e-mail is:
dinwinseventysix at fetchsource.com
(Change "seventysix" to the number, the "at" to the at symbol, and "fetchsource" to "vegsource," then push it all together.  



Fri, 24 Sep 2004 00:41:41 GMT  
 Anti-Bug If/Then Makes a Bug
If the user clicks the cancel button in an InputBox, the InputBox
returns a zero-length string "".  The InputBox has nothing to do with
vbOK or vbCancel.

Try this:

strStringName = _
    InputBox("What is your string variable's name?", "?", "strFName")

    'NOTE:  "" instead of vbCancel
    If strStringName = "" Then
        GoTo Dump
    End If

    intNum = _
    InputBox("How many names do you want to enter?", "Names", "20")

    'NOTE:  "" instead of vbCancel
    If intNum = "" Then
        GoTo Dump
    End If

Another thing you could consider is to nest the input boxes to get
more structured code, and use a temporary variable to guard against
the user supplying text for the second one:

Dim sName As String
Dim iNumber As Integer
Dim s As Sring

s = InputBox("What is your string variable's name?", "?", "strFName")

If s <> "" Then
    sName = s
    s = InputBox("How many names do you want to enter?", "Names",
"20")

    If s <> "" Then

        If IsNumeric(s) Then
            iNumber = CInt(s)
        Else
            'Bad user.  they entered text
        End If

    Else
        'User clicked cancel for number
        'Do integer "dump" stuff
    End If

Else
    'User clicked cancel for string
    'Do string "dump" stuff
End If

--
Peace & happy computing,

Mike Labosh, MCSD MCT
"Feed the children!  Save the whales!  Free the mallocs!" --Unknown



Fri, 24 Sep 2004 01:11:29 GMT  
 Anti-Bug If/Then Makes a Bug
 That solved my mystery.  Thank you so much for helping.  
********************************************
My e-mail is:
dinwinseventysix at fetchsource.com
(Change "seventysix" to the number, the "at" to the at symbol, and "fetchsource" to "vegsource," then push it all together.  


Fri, 24 Sep 2004 12:41:32 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. IDE BUGs, BUGs, BUGs....

2. BUGS BUGS BUGS!

3. Project98 OLE & VB: bugs, bugs, bugs

4. BUG: VB6 Fails to debug VB-Made ActiveX control with external program on Windows2000\XP

5. Stange String Behavior - Bug or Bugged ?

6. My Bug or VB.Net Bug ???

7. BUG: Workaround for cool menu accelerator bug?

8. Bug.. VBScript ASP Dictionary Bug

9. BUG BUG in VB

10. BUG BUG in VB

11. BUG, VERY BIG BUG IN VB5

12. BUG, VERY BIG BUG in VB5

 

 
Powered by phpBB® Forum Software