Results 1 to 8 of 8

Thread: [RESOLVED] Do Until inside If statement?

  1. #1

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    Resolved [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

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    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)?

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  5. #5

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    Re: Do Until inside If statement?

    Quote Originally Posted by TnTinMN View Post
    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.

  6. #6

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    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.

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  8. #8

    Thread Starter
    Lively Member GTDriver's Avatar
    Join Date
    Apr 2015
    Location
    Blighty
    Posts
    66

    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
  •  



Click Here to Expand Forum to Full Width