Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inputbox and cancel

Status
Not open for further replies.

peter11

Instructor
Mar 16, 2001
334
0
0
US
I have a high score program where the user enters their name if they get a high score.

The problem arises when the user clicks cancel. The program still enters the score but no name.

When they hit cancel I want the progran to Exit Sub

I tired the following but vbnull = 1 regardless clicking cancel or ok
strNname = InputBox("Your Special. Enter Your _ Name", "bestscores input", "Anonymous")
If vbNull = "1" Then Exit Sub

 
If user presses cancel then a zero length string is returned.

Check for that

If len(strNname) = 0 then
Exit sub
End if

If in doubt, try help file first!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 


strNname will be an empty string ("") if the user clicks cancel. vbNull is a VB constant that will always = 1.

if strNname = "" then exit sub

Mark
 
I have tried both but neither works?

What am I missing?
 

I think you may be updating the scores outside this routine and not know it. If the code is as you have it here, your routine would always exit the routine (since vbNull will always = 1) and the Exit Sub call executed each time. If the score update code follows the posted code above, it would never be executed...





Mark
 
it doesnt work as in... it doesnt exit the sub, or that it does exit the sub but still saves the score...

any chance you post the whole/part of the code?? I like doing it the hard way! OK!
 
Here is the section of the code:

For i = 1 To 5
If today >= intBestScores(i) Then
Exit For
Else
count = count + 1
Exit Sub
End If
Next i

If count = 5 Then Exit Sub

strNname = InputBox("Enter Your name")

If strNname = "" Then Exit Sub
 

I gather the purpose of the code is to save the score if it is one of the top 5 scores of the day. I try to stay away from the Exit For and Exit Sub calls if at all possible.

This routine will accomplish the same with out the "Exit" calls:


Private Sub PostIfBestScore(today As Integer)
Dim intIndexofTodayBestScore As Integer
intIndexofTodayBestScore = 0

'assumes Option Base 1
For i = UBound(intBestScores) To 1 Step -1
If today >= intBestScores(i) Then
intIndexofTodayBestScore = i
End If
Next

If intIndexofTodayBestScore > 0 Then
strNname = InputBox("You have one of today's top 5 scores. Enter Your name")
If strNname <> &quot;&quot; Then
'update the new best score at the index found
intBestScores(intIndexofTodayBestScore) = today
' update the Name connected with the best score somewhere?
'intBestScoreNames(intIndexofTodayBestScore) = strNname
End If
End If
End Sub


Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top