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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop until given Value

Status
Not open for further replies.

zuza

Programmer
Nov 9, 2003
54
YU
Dear all,
this peace fo code is not working, enntering into table records through looping for a given value(limit):
a - is value that user enters(selects from combo in this case), so adding of records should end to this up to this value.

This code below is looping to the end is not stoping to the given value !???!

Private Sub Save_Click()

Dim a, b As Double
a = Val(Combo1.Text)

b = 1

While Not (b >= a)


If Text1.Text = "" Then
MsgBox "Please enter the name of the Village.."
Text1.SetFocus
Exit Sub
ElseIf Combo2.Text = "" Then
MsgBox "Please select a Municipality."
Exit Sub
End If

If Text2 <> "" Then
If Not IsNumeric(Text2.Text) Then
MsgBox "The Surface you entered is invalid!"
Exit Sub
End If
End If

Dim rstAdd As New ADODB.Recordset
Dim adoAdd As New ADODB.Connection
Dim strMun As String


adoAdd.Open "PROVIDER=MSDASQL;dsn=idp;uid=;pwd=;database=IDPSurvey;"
adoAdd.Execute "insert into Lands values('" & Text3 & "','" & Text1 & "', '" & _
Combo2 & "', '" & Text2 & "')"

Me.Text1 = ""
Me.Combo2 = ""
Me.Text2 = ""

adoAdd.Close

b = b + 1
Wend

MsgBox "Saving finished, you have reached maximum of parcels", vbCritical, "Author : ZuZa"

Unload Me
End Sub

Regards,
ZuZa
 
a) use a and b as long
b) change a = Val(Combo1.Text) to a=clng(Combo1.Text)
c) make shure that it's Combo1.Text and not Combo1.list
(combo1.listindex)
 
Again, it's looping 'till no end ... and to the given value a ... under c) I didn't get you.

Thnx,
ZuZa
 
Zuza

This line:
Dim a, b As Double
means the same as this:
Dim a as Variant
Dim b As Double

Why this: While Not (b >= a)
and not this: While (a > b)?

vladk
 
z,

What's the VALUE of a going into the loop?

Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
Skip,

a is value that user select from the dropdown, so entry has to go 'till that value ... is maximum of the Entering the fields above into the table.

No solution 'till now
ZuZa.
 
I think what Skip was getting at is what is the actual value of a. In other words, what happens if you put:

MsgBox a

Right before the loop starts. Is this what you thought the value should be?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
And you are declaring BOTH a & b as LONG?
Code:
Din a as long, b as long


Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
Yes on msgbox I'm getting the right value ...for 'a' AND declaration is like this
Dim a As Variant
Dim b As Double

Rgds,
ZuZa.
 
Why do you have them declared differently? You were previously advised TWICE, to declare them the SAME as LONG.

Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
Then I think the declaration should be:
Dim a As Double
Dim b As Double

And the assignment should be:
a = CDbl(Combo1.Text)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I tried and like this ... but same thing ! Is not not going out from the loop !?

rgds
 
I'd only use double if the user will be entering decimal values?

Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
TomThumbKP
I would seriously advise against using Double for a counter. You should always use VB's native integer type, which as already mentioned, is Long.

Double is a floating point type and is not at all robust in any form of comparison

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

'If we're supposed to work in Hex, why have we only got A fingers?'
 
I agree that a Long would be better. I was merely attempting to make the test variable type match the counter type. Of course you are correct that making both longs is the best solution.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Zuza,

You should probably keep getting message:
"Please enter the name of the Village.."
and you are not looping but rather forcing yourself start your loop over and over again. Look, you repeat your loop with blank boxes and go out of the loop all the time.

Why your style is so inconsistent? Sometimes you explicitely refer to default properties, sometimes not. Sometimes you use Me., sometimes not..

What the point in putting declaration section inside the loop???

Why you don't set your objects to Nothing when you don't need them?

Why your loop condition is so weird? "While a > b" is simple and natural.

vladk
 
Thanks guys! It's fine now, cause on_save evrrytime I defaulted b to 1, so I put it as public declaration max_value and variable b for looping.

Rgds
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top