Results 1 to 12 of 12

Thread: [RESOLVED] What is wrong with this code?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    32

    Resolved [RESOLVED] What is wrong with this code?

    VB Code:
    1. strinput = InputBox("Who are you?")
    2.  
    3.     For i = 0 To 2
    4.         If guest(i) = strinput Then
    5.                 blnvalid = True
    6.                 Else: blnvalid = False
    7.           End If
    8.      Do While blnvalid = False
    9.      strinput = ("reenter")
    10.      Loop
    11.     Next i

    I'm trying to validate an input and if validation = false, a new question will be asked instead of the thing looping the same question.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: What is wrong with this code?

    What type of error are you getting????????

    When I put in your code onto a simple program, the program gets stuck in the Do Loop structure...

    Why is the Do Loop there anyways... ???

    Please be more precise!!!
    Hey... If you found this post helpful please rate it.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    32

    Re: What is wrong with this code?

    The code won't run at all in my program, probably because some errors in my other part of program.

    For the do..loop... i'm trying to get the program to loop a different question if blnvalid = false but stops the loop if blnvalid is found to be true again.

    guest is an array of names...

  4. #4
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: What is wrong with this code?

    VB Code:
    1. strinput = InputBox("Who are you?")
    2.     For i = 0 To 2
    3.         If guest(i) = strinput Then Exit For
    4.     Next i
    5.     If i = 3 Then ' no match was found
    6.         ' Ask new question
    7.     End if
    What might the new question be? (This code is not complete)

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    32

    Re: What is wrong with this code?

    new question is strinput = ("reenter")

    Anyway, here is my original, perectly working code, but it loops the same question when blnvalid = false but I want it to ask a different one and validate it once more.

    VB Code:
    1. Do
    2.         strinput = InputBox("Who are you?")
    3.         'Check if the name is in the array
    4.     For i = 0 To 2
    5.         If guest(i) = strinput Then
    6.                 blnvalid = True
    7.             End If
    8.             Next i
    9.     'Loop while a valid name has not been entered
    10.     Loop While Not blnvalid

  6. #6
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: What is wrong with this code?

    Are you trying to say that you are getting the same question.... If so then where are you getting your questions from???
    Hey... If you found this post helpful please rate it.

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    32

    Re: What is wrong with this code?

    The question is from here:

    strinput = InputBox("Who are you?")

    When it first validates, if the blnvalid = true, then it just carry on with other things, however if blnvalid = false, it just loops to the start at the same question.

    What i want it to do is to loop when blnvalid = false, but with different question.

    Like this: strinput = Inputbox("reenter") when blnvalid = false, this will loop over and over until blnvalid = true

  8. #8
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: What is wrong with this code?

    I wouldn't say that code works perfectly. There is no way to exit the loop except by entering a valid string. Anyway, try this:
    VB Code:
    1. Do
    2.   strinput = InputBox("Who are you?")
    3.   'Check if the name is in the array
    4.   For i = 0 To UBound(guest) ' In case you change guest to have more names
    5.     If guest(i) = strinput Then Exit For
    6.   Next i
    7.   If i > UBound(guest) Then
    8.     blnvalid = False
    9.     answer = MsgBox("No match found. Try again?", vbYesNo)
    10.   Else
    11.     blnvalid = True
    12.   End If
    13.   ' Loop as long as user wants to try again
    14.   ' Note that the loop may be exited without a valid input
    15. Loop While answer = vbYes

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    32

    Re: What is wrong with this code?

    That works perfectly except one little small thing...

    when I click "No" for the loop, it doesn't actually quit the program but just move on to the next part of it, which i do no want...

  10. #10
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: What is wrong with this code?

    VB Code:
    1. If answer = "7" Then
    2.           MsgBox "No"
    3.     End If
    Hey... If you found this post helpful please rate it.

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    32

    Re: What is wrong with this code?

    After a little tinkering, manages to solve it.

    Thanks everyone.

  12. #12
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: What is wrong with this code?

    Quote Originally Posted by vert
    That works perfectly except one little small thing...

    when I click "No" for the loop, it doesn't actually quit the program but just move on to the next part of it, which i do no want...
    That might not be what you want, but it is what you need. After exiting the loop, check the value of blnvalid, then continue accordingly.

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