[RESOLVED] InputBox ... a little prob
Hi:wave:
My prob:
If I click on a Cancel button I want that my app pass to end (also after making first entry - line of code : If MsgBox("You have incomplete value or you want to break?".....)
Unfortunately it's around displaying me message "Enter with correct an value".
Also I get the error when I click on a button OK in InputBox when nothing is entered. Because my Sum variable is Empty. How to make it correctly
It's my code:
Code:
Option Explicit
Dim intNext As Integer
Private Sub cmdOK_Click()
Dim I As Integer, War As String, Sum As Double
intNext = Text2.Text
For I = 0 To intNext
Again:
War = InputBox("Added " & I + 1 & " value", "Adding sections")
If StrPtr(War) = 0 Then 'Exit Sub
If Sum > 0 Then
If MsgBox("You have incomplete value or you want to break?", _
vbYesNo + vbQuestion, "Question") = vbYes Then Exit Sub
End If
If IsNumeric(War) = False Then
MsgBox "Enter with correct an value", vbCritical
GoTo Again
End If
End If
Sum = Sum + CDbl(War)
Next
Text1.Text = Sum
End Sub
thanks in advance
***EDIT:
It the adding a different sections and my app it passes this value as base to next count
Re: InputBox ... a little prob
You get the error if nothing is entered because a blank string is still loaded in memory and thus has a pointer, so your StrPtr line won't stop it.
Simply replace that line by 'If War = vbNullString Then ...'
Re: InputBox ... a little prob
Hi
OK. thanks something like this I tried already, unfortunately without any of the result
But still I don't have main solution that when I click on a Cancel button it should be ending work my app. Note, that if you are clicking now on the Cancel button then it will be displaying message box with information for request about correct the entry. I not want so like it's now.
So.. please about an some the help
tamgovb
Re: InputBox ... a little prob
Try this, most of which has already been suggested:
Code:
Private Sub cmdOK_Click()
Dim intNext As Integer
Dim I As Integer, War As String, Sum As Double
intNext = Len(Text2.Text)
For I = 0 To intNext
Again:
War = InputBox("Added " & I + 1 & " value", "Adding sections")
If War = "" Then Exit Sub
If Sum > 0 Then
If MsgBox("You have incomplete value or you want to break?", _
vbYesNo + vbQuestion, "Question") = vbYes Then Exit Sub
End If
If IsNumeric(War) = False Then
MsgBox "Enter with correct an value", vbCritical
GoTo Again
End If
Sum = Sum + CDbl(War)
Next
Text1.Text = Sum
End Sub
Re: InputBox ... a little prob
Hi, again me
@vbmom: very thanks, but....I’m sorry …. it’s not correctly.
Below - it's my description how this should be
If during a first the rotation of loop you click on a Cancel button then this procedure should be closed
If you click on the Cancel button after making an some the entry it should to display message box with question: "You have incomplete value, you want to stop calculation?"
You have to choice: Yes or No, if Yes - it then this procedure should be ended. Hope that you understand me.
However if you entry to InputBox other signs than numbers it should appear the message box with message - e.g. something like this " You can write down only numbers”.
Then it should display again InputBox to make for correctly of this entry.
This message box with this message it should not be then when you are doing correctly these calculation.
Now this code after corrections it works like I describe this here (below)
Clicking on the Cancel it's OK, however....when I make correctly this entry it completely unnecessarily is displaying the message box with "You have incomplete value, you want to stop calculation?"
Also, when I trying to write some other the sign than number then also completely unnecessarily is displaying the same this message box with "You have incomplete value, you want to stop calculation?"
Hope that this is comprehensible - look in a my signature, sorry.
Thanks in advance
Re: InputBox ... a little prob
I just woke up and have read your explanation. I do not completely understand mostly because I'm not clear on what this thing does. It seems to me you should try another approach to what you want to do other than using inputbox and a loop. If you really must use a loop then introduce some flags that will help you decide what to do.
If you find you need to use for/next and a GOTO in a loop, then it is likely you need to redesign the solution. That's just my opinion. I cringe whenever I use a GOTO, but I know sometimes it's necessary. Since you are implementing user input, repeated prompts are not always user friendly anyway, so that's why I say think of another way to do it. Why not plain form with OK/Cancel buttons and use flags to enable and disable them?
If I were more awake I might understand your problem. I didn't want to ignore you but I've got to go to work now and won't be able to answer later.
Re: InputBox ... a little prob
try this in OK button click
Code:
Dim I As Integer, War As String, Sum As Double
intNext = Text2.Text
For I = 1 To intNext
Again:
War = InputBox("Added " & I & " value", "Adding sections")
If War = "" And Sum <= 0 Then Exit Sub
If War = Cancel Then
If MsgBox("You have incomplete value or you want to break?", _
vbYesNo + vbQuestion, "Question") = vbYes Then
Exit Sub
Else
GoTo Again
End If
End If
If IsNumeric(War) = False Then
MsgBox "Enter with correct an value", vbCritical
GoTo Again
End If
Sum = Sum + CDbl(War)
Next
Text1.Text = CStr(Sum)
Re: InputBox ... a little prob
Hi
Thanks, but this does not work still, someone knows how to make this
Re: InputBox ... a little prob
try this:
Code:
Option Explicit
Dim intNext As Long
Private Sub Command1_Click()
Dim I As Long, dblSum As Double
Dim strWar As String
Dim bOK As Boolean
' you should have some error handling here,
' but i'll leave that for you to do
intNext = CLng(Text2.Text)
For I = 0 To intNext
bOK = False
Do Until bOK
strWar = InputBox("Added " & I + 1 & " value", "Adding sections")
If StrPtr(strWar) = 0 Then
' Cancel Pressed
If dblSum > 0 Then
If MsgBox("Do you want to Quit?", vbYesNo _
Or vbQuestion, "Question") = vbYes Then Exit Sub
Else
Exit Sub
End If
Else
' Ok Pressed
If Not IsNumeric(strWar) Then
MsgBox "Enter a correct value!", vbCritical
Else
dblSum = dblSum + CDbl(strWar)
bOK = True
End If
End If
Loop
Next I
MsgBox dblSum
End Sub
avoid using GoTo like the plague
Re: InputBox ... a little prob
Hi All:wave:
Dear Bush, how always, you are unfailing , in perfect form, a great thanks, thank you very much :thumb: :thumb: :thumb: :thumb:
Orginally posted by Bushmobile
Quote:
avoid using GoTo like the plague
yeah I agree with you...though sometimes you have not other exit, maybe it was this one time.
Believe me, I try the best like I can to remember about this and about every other suggestion that a valuable this forum
I hope that you understand me.
Re: InputBox ... a little prob
Quote:
Originally Posted by Tamgovb
Orginally posted by Bushmobile
yeah I agree with you...though sometimes you have not other exit
You always have another exit. Instead of
Code:
If <condition> Then Goto Label
'code
Label:
use
Code:
If Not <condition> Then
'code
End If