Results 1 to 14 of 14

Thread: If...or For...Next Loop

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 1999
    Location
    France
    Posts
    90

    Hi,

    How can I get this working!?

    I have three integers. I want the highest one to appear in textbox1 when the user clicks command1. I keep getting a 'If missing' error. I'm sure I don't have to use the If statement? Is there some way of looping? I'd appreciate if you could show me what's wrong with my code.

    Thanks!

    'Three labels, a command button and textbox

    Dim rfirst As Integer
    Dim rsecond As Integer
    Dim rthird As Integer


    Private Sub Form_Load()
    Randomize
    rfirst = Int(Rnd * 20)
    Label1.Caption = rfirst
    rsecond = Int(Rnd * 20)
    Label2.Caption = rsecond
    rthird = Int(Rnd * 20)
    Label3.Caption = rthird
    End Sub


    Private Sub Command1_Click()
    If rfirst > rsecond Then
    If rfirst > rthird Then
    Text1 = rfirst
    End If
    If rsecond > rfirst Then
    If rsecond > rthird Then
    Text1 = rsecond
    End If
    If rthird > rfirst Then
    If rthird > rsecond Then
    Text1 = rthird
    End If
    End Sub

  2. #2
    Guest
    You were missing some End If's, but it should work now.

    Code:
    Private Sub Command1_Click()
    
            If rfirst > rsecond Then
                If rfirst > rthird Then
                    Text1 = rfirst
                End If
            End If
            
            If rsecond > rfirst Then
                If rsecond > rthird Then
                    Text1 = rsecond
                End If
            End If
            
            If rthird > rfirst Then
                If rthird > rsecond Then
                    Text1 = rthird
                End If
            End If
            
    End Sub

  3. #3
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    Your problem is you open two if statements for each check, and only close one.

    Code:
    Dim rfirst As Integer
    Dim rsecond As Integer
    Dim rthird As Integer
    
    
    Private Sub Form_Load()
    Randomize
    rfirst = Int(Rnd * 20)
    Label1.Caption = rfirst
    rsecond = Int(Rnd * 20)
    Label2.Caption = rsecond
    rthird = Int(Rnd * 20)
    Label3.Caption = rthird
    End Sub
    
    
    Private Sub Command1_Click()
    If rfirst > rsecond And rfirst > rthird Then
            Text1 = rfirst
    End If
    
    If rsecond > rfirst And rsecond > rthird Then
            Text1 = rsecond
    End If
    
    
    If rthird > rfirst And rthird > rsecond Then
            Text1 = rthird
    End If
    
    End Sub

  4. #4
    Hyperactive Member Zaphod64831's Avatar
    Join Date
    Mar 2000
    Posts
    268
    Here ya go:
    Code:
    Private Sub Command1_Click() 
    
    If rfirst > rsecond Then 
       If rfirst > rthird Then 
          Text1 = rfirst
       End Ff
    End If
    If rsecond > rfirst Then 
       If rsecond > rthird Then 
          Text1 = rsecond
       End If
    End If 
    If rthird > rfirst Then 
       If rthird > rsecond Then 
          Text1 = rthird 
       End If 
    End If
    
    End Sub
    That should do it, but I haven't had a chance to try it out yet. You were missing three End If statements there.

    Let me know if you have any problems
    Email: [email protected]

    Home Page: www.olemac.net/~hutch

    I'm bored, VERY bored, and I got bored with my sig. So I changed it to this.

  5. #5
    Guest
    Originally posted by christophe


    Code:
    Private Sub Command1_Click()
    If rfirst > rsecond Then
       If rfirst > rthird Then
          Text1 = rfirst
       End If
       If rsecond > rfirst Then
          If rsecond > rthird Then
             Text1 = rsecond
          End If
          If rthird > rfirst Then
             If rthird > rsecond Then
                Text1 = rthird
             End If
    End Sub
    Code:
    
    
    Total lack of End Ifs there try,
    Code:
    Private Sub Command1_Click()
    if rfirst > rsecond Then
       If rfirst > rthird Then
          Text1 = rfirst
          Exit Sub
       End If
    End If
    '
    if rsecond > rfirst Then
       If rsecond > rthird Then 
          Text1 = rsecond
          Exit Sub
       End If
    End If
    '
    If rthird > rfirst Then
       if rthird > rsecond then Text1 = rthird
    Endif
    Now l hope you don't intend to keep the default names like Text1 and Command1, re-name them to something more meaningful.

    P.S Sorry had to modify the code, the Exit Sub statements should of course only be executed if the condition is true.

    Hope this helps,

    John, this is not really a helpful posting....honest

    [Edited by Jethro on 07-12-2000 at 06:27 PM]

  6. #6
    Hyperactive Member Zaphod64831's Avatar
    Join Date
    Mar 2000
    Posts
    268
    DANG! I almost managed to answer one
    Email: [email protected]

    Home Page: www.olemac.net/~hutch

    I'm bored, VERY bored, and I got bored with my sig. So I changed it to this.

  7. #7
    Guest
    You were missing some End If's, but it should work now.

    Code:
    Private Sub Command1_Click()
    
            If rfirst > rsecond Then
                If rfirst > rthird Then
                    Text1 = rfirst
                End If
            End If
            
            If rsecond > rfirst Then
                If rsecond > rthird Then
                    Text1 = rsecond
                End If
            End If
            
            If rthird > rfirst Then
                If rthird > rsecond Then
                    Text1 = rthird
                End If
            End If
            
    End Sub

  8. #8
    Guest

    Unhappy Zap l don't believe it you got me again with that Click thing

    Please no one else click on Zaps thread....it will destroy your day......aaaarrrrrrrgggggghhhhhhhhhh!!!!!!!!

  9. #9
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    An outside the box route:
    Code:
    Private Sub Form_Load()
    Dim res As Integer 
      res = Rnd * 20
      ' init "highest" random value
      Label1.Tag = res
      ' first random value  
      Label1.Caption = res 
      res = Rnd * 20
      ' second random value  
      Label2.Caption = res 
      If res > Label1.Tag Then Label1.Tag = res 
      res = Rnd * 20
      ' third random value
      Label3.Caption = res
      ' tag holds highest value of three random 
      If res > Label1.Tag Then Label1.Tag = res 
    End Sub
    
    Private Sub Command1_Click()
      Text1 = Label1.Tag
    End Sub
    Try to use as few resources and CPU cycles as necessary.

    [Edited by Mongo on 07-12-2000 at 06:53 PM]

  10. #10
    Guest

    Angry Damn you Mongo

    And there l was thinking l had the best solution including the Exit Sub additions.

    Actually l was waiting on some one to figure out the Select Case solution.

    oh well

    Code:
    Select Case rfirst
       Case > rsecond and > rthird: Text1 = rfirst
       Case < rsecond and rsecond > rthird : Text1 = rsecond
       Case < rthird and rthird > rsecond : Test1 = rthird
    End Select
    Ok tested it out with Universe and it works, don't have vb on this PC so don't know if this will work, actually will dick around with it tonite and have a look.

  11. #11
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482

    Re: Damn you Mongo

    *ROTFLMAO* I'll wear that red badge of courage proudly, Jethro! Maybe I had too many days with the 101st... you know, do more with less, night stalking, etc... (I assume there's some truth to your combat programming, ergo the milspeak). Condor 06, scouts out. Out Front!

    [Edited by Mongo on 07-12-2000 at 10:55 PM]

  12. #12
    Hyperactive Member Zaphod64831's Avatar
    Join Date
    Mar 2000
    Posts
    268

    Jethro:

    hehe...
    I don't really see the problem, all I did was say click here.

    I don't see a problem with saying click here, it's just like saying don't click here or click here or you'll regret it
    because it's your choice whether or not you want to click here, or maybe over here or even right here. It's not my fault if you click here.

    In conclusion CLICK HERE!!!!!!!!!!
    Email: [email protected]

    Home Page: www.olemac.net/~hutch

    I'm bored, VERY bored, and I got bored with my sig. So I changed it to this.

  13. #13
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Thumbs up


    Thank you all for your many ideas. I'll look at them all now, and try them out.

    That damn If statement - I'm always getting it wrong.




  14. #14
    Guest

    Talking You guys

    Mongo

    Naw the combat programming has nothing to do with the military, is to do with our website once l can get the guys to agree to put the friggin thing up. Word of warning, never have an artist on your team of web developers, they just add months to the process.

    Zap

    Not being caught by that one again, though l noticed in email some one was.hehehehehehehe

    Ian
    Glad to help...don't tell John the Admin....mongo's looks to be about the best solution...damn.

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