+ Reply to Thread
Results 1 to 5 of 5

Validate InputBox entry

  1. #1
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    315

    Validate InputBox entry

    My code below seeks to validate the InputBox entry. It however returns all previous wrong entries as well. For example, if 5, 3, 2 are entered, the Msgbox delivers 2, 3, 5 in that order. Why is there such implicit static attribute? And how can I cure the problem without resorting to GOTO labelling?

    Sub TestValidate()
    x = InputBox("Enter 2")
    If x <> 2 Then TestValidate
    MsgBox x
    End Sub

    DM

  2. #2
    Registered User
    Join Date
    02-18-2005
    Posts
    26

    Wink

    it return values from previous entry bcos u nv exit the sub b4 u loop the sub again, try these

    Sub TestValidate()

    x = InputBox("Enter 2")
    If x <> 2 Then
    TestValidate
    Exit Sub
    End If
    MsgBox x
    End Sub

    OR

    Sub TestValidate()

    x = InputBox("Enter 2")
    If x <> 2 Then
    TestValidate
    Else
    MsgBox x
    End If
    End Sub



    Cheers

  3. #3
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    315
    Thanks d61Helix. Both codes work altho' I am a bit puzzled about how the first works. Revisit:

    Sub TestValidate()

    x = InputBox("Enter 2")
    If x <> 2 Then
    TestValidate
    Exit Sub
    End If
    MsgBox x
    End Sub

    If an entry other than 2 is entered, the code should loop back recursively before the "Exit Sub" could be executed. Why the code is exited before looping baffles me.

  4. #4
    Registered User
    Join Date
    02-18-2005
    Posts
    26
    yes u r right where the code loop back recursively b4 the "exit sub" but dunt forget wat happens after everything...the sequence still have to end some how...in yr case it was ended by a correct entry of "2" , but keep in mind that yr previous loops of wrong entries are snowballed and have not yet ended, in another words they still have to run"MsgBox x" bcos they still exist in the sequence, so by puting exit sub right after TestValidate it forces the sequence to exit with no chance to get to "MsgBox x"..hope u get wat iam trying to say

    cheers

    Quote Originally Posted by davidm
    Thanks d61Helix. Both codes work altho' I am a bit puzzled about how the first works. Revisit:

    Sub TestValidate()

    x = InputBox("Enter 2")
    If x <> 2 Then
    TestValidate
    Exit Sub
    End If
    MsgBox x
    End Sub

    If an entry other than 2 is entered, the code should loop back recursively before the "Exit Sub" could be executed. Why the code is exited before looping baffles me.
    Last edited by d61helix; 04-12-2005 at 03:09 AM.

  5. #5
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    315
    To revisit the issue, when a wrong entry is made, say 5, the "exit sub" should force the code to terminate and thereby forestall any looping (recursively). But this doesn't happen when the code is executed. Seems curious. Any further explanation?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1