|
-
Apr 28th, 2007, 06:03 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] InputBox ... a little prob
Hi
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
Last edited by Tamgovb; Apr 29th, 2007 at 04:52 AM.
I know, I know, my English is bad, sorry .....
-
Apr 29th, 2007, 12:01 AM
#2
Junior Member
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 ...'
-
Apr 29th, 2007, 05:22 AM
#3
Thread Starter
Hyperactive Member
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
I know, I know, my English is bad, sorry .....
-
Apr 29th, 2007, 05:51 AM
#4
Hyperactive Member
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
-
Apr 29th, 2007, 02:33 PM
#5
Thread Starter
Hyperactive Member
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
I know, I know, my English is bad, sorry .....
-
Apr 30th, 2007, 05:51 AM
#6
Hyperactive Member
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.
-
Apr 30th, 2007, 07:39 AM
#7
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)
-
Apr 30th, 2007, 04:20 PM
#8
Thread Starter
Hyperactive Member
Re: InputBox ... a little prob
Hi
Thanks, but this does not work still, someone knows how to make this
I know, I know, my English is bad, sorry .....
-
Apr 30th, 2007, 05:03 PM
#9
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
-
May 1st, 2007, 07:37 AM
#10
Thread Starter
Hyperactive Member
Re: InputBox ... a little prob
I know, I know, my English is bad, sorry .....
-
May 1st, 2007, 10:36 AM
#11
Re: InputBox ... a little prob
 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
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|