-
Jun 14th, 2015, 02:51 PM
#1
Thread Starter
Lively Member
[RESOLVED] Do Until inside If statement?
Hi -- i've written the following code... it runs but doesnt work. I'm guessing that you cant have a 'Do Until' loop inside of an 'If' statement?
Could someone explain what i've done wrong and point me in the right direction?
Code:
Private Sub Calc()
Dim Clmn%
Dim Rslt$
DA = New OleDbDataAdapter("SELECT A , B, C, D, E, F, G FROM tblData WHERE RefNumber = " & lblRefNum.Text, con)
DA.Fill(DT)
If CInt(txtResult.Text) < DT.Rows(0).Item(6) Then
txtLetter.Text = "Nothing"
Exit Sub
Else
Do Until CInt(txtResult.Text) >= DT.Rows(0).Item(Clmn) - 1
Clmn = Clmn + 1
Loop
End If
Rslt = DT.Columns(Clmn).ColumnName
End Sub
Any help appreciated -- thanks in advance.
GTD
-
Jun 14th, 2015, 03:24 PM
#2
Re: Do Until inside If statement?
What are you expecting it to do? If the If condition is false, the Do loop will increment the Clmn local variable until the textbox entry is greater than or equal to the value in that column. Having done that it updates another local variable with the name of that column and then exits the sub. Presumably you are expecting some sort of side effect, but you've not coded one. The loop itself doesn't seem to be a problem, albeit a slightly strange way of going about things.
-
Jun 14th, 2015, 06:04 PM
#3
Re: Do Until inside If statement?
Code:
Do Until CInt(txtResult.Text) >= DT.Rows(0).Item(Clmn) - 1
Clmn = Clmn + 1
Loop
What do you think will happen if the condition is not satisfied and Clmn has reached a value greater than (DT.Rows(0).ItemArray.Length - 1)?
-
Jun 15th, 2015, 04:26 AM
#4
Re: Do Until inside If statement?
Presumably that won't happen because the values in the columns decrease and therefore the If would catch that scenario. As I said, it's an unusual approach that doesn't communicate intent very well, but aside from not outputting, it's not horribly broken.
-
Jun 15th, 2015, 04:59 AM
#5
Thread Starter
Lively Member
Re: Do Until inside If statement?
Originally Posted by TnTinMN
Code:
Do Until CInt(txtResult.Text) >= DT.Rows(0).Item(Clmn) - 1
Clmn = Clmn + 1
Loop
What do you think will happen if the condition is not satisfied and Clmn has reached a value greater than (DT.Rows(0).ItemArray.Length - 1)?
It'll error. I anticipated this:The 'If' portion is how I'm dealing with that scenario . Im learning, so so thanks for the suggestion
Last edited by GTDriver; Jun 15th, 2015 at 05:07 AM.
-
Jun 15th, 2015, 05:28 AM
#6
Thread Starter
Lively Member
Re: Do Until inside If statement?
You're right. The values do decrease. Would you have done it differently? This example is my best attempt and I'm curious to know how more skilled/experienced people would approach this.
Basically, what im trying to achieve (would have been helpful if i stated this upfront i guess - appologies):
DT Columns A to G contain numbers. These numbers decrease from column A. For example, A = 50, B = 40, C = 30... and so on.
txtResult.text accepts integer values. So... if the user inputs 40 (for arguments sake) the code will return the name of the column which txtResult.text relates to; therefore only the column which is equal to or above (>=) the number provided in txtResult.text. Now lets say G = 10 and txtResult.text = 9... now i need the code to return "Nothing". The matching column's name (or "Nothing) will be held in the Rslt variable and used else where in my code.
This seems like a simple task in my mind's eye... but i guess i just dont have the knowledge to cobble something together without assistance.
I hope this clarifies everyone's understanding and thanks again for your help with this.
Last edited by GTDriver; Jun 15th, 2015 at 06:46 AM.
-
Jun 15th, 2015, 06:43 AM
#7
Re: Do Until inside If statement?
Remove the If/then and just loop around the rows/columns, as appropriate, performing the comparison. However, It's not clear what will to happen, since there's no ordering of values that I can see. You should explicitly order the values.
Also, remove the 'shortcut' postfixes and declare the variables properly. use parameters instead of just appending a user entered string in a textbox to the query (SQL Injection attack could delete all your data, for example).
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jun 17th, 2015, 01:40 PM
#8
Thread Starter
Lively Member
Re: Do Until inside If statement?
Did a bit of digging and Cases suited what i need and my current skill/experience - thanks all for helping
Tags for this Thread
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
|