Results 1 to 12 of 12

Thread: [RESOLVED] Simple Loop question i think

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Resolved [RESOLVED] Simple Loop question i think

    I have never used loops before so i dont know where to begin, i looked on the internet an found this code but it doesnt work
    VB Code:
    1. Do While Timer1.Enabled
    2.     DoEvents
    3. Loop
    How do you loop a timer?

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Simple Loop question i think

    Of course it works Add a Timer on your form and try this (it should end the loop after 3 seconds - .Interval):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     With Timer1
    5.         .Interval = 3000
    6.         .Enabled = True
    7.     End With
    8.  
    9.     'this means loop while Timer1.Enabled = True
    10.     'in other words, end the loop when Timer1.Enabled = False
    11.     Do While Timer1.Enabled
    12.         DoEvents
    13.     Loop
    14.         Debug.Print "Loop ended"
    15. End Sub
    16.  
    17. Private Sub Timer1_Timer()
    18.     Timer1.Enabled = False
    19. End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Simple Loop question i think

    As gavio said, what you have should work.

    What are you trying to accomplish that isn't working?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Simple Loop question i think

    This is the code i have now. I have created mt own browser and now i am trying to block access to some sites. It still dosent work do you know what i am doin wrong.

    VB Code:
    1. With Timer5
    2.         .Interval = 3000
    3.         .Enabled = True
    4.     End With
    5.  
    6.     'this means loop while Timer1.Enabled = True
    7.     'in other words, end the loop when Timer1.Enabled = False
    8.     Do While Timer1.Enabled
    9.         DoEvents
    10.     Loop
    11.         Debug.Print "Loop ended"
    12.  
    13.  
    14. Timer5.Enabled = True
    15. End Sub


    VB Code:
    1. Private Sub Timer5_Timer()
    2.  
    3. If Timer5.interval + 1 Then
    4. check
    5. End If
    6. End Sub


    VB Code:
    1. Private Sub check()
    2. If txtWebsite.Text = "http://www.google.com" Then
    3. MsgBox "Your System Administrator has disallowed access to this site as the content is inappropiate."
    4. 'home
    5. txtWebsite.Text = frmOptions.txtHome.Text
    6. WB1.Navigate txtWebsite.Text
    7. End If
    8. End Sub

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Simple Loop question i think

    What are you trying to accomplish with your timer loop that isn't working?

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Simple Loop question i think

    It doesn't work cause Timer5 is never "disabled". When you'll do that the loop will end. Also, you have Timer1 in your loop but you're using Timer5.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Simple Loop question i think

    i dont want the timer to be disabled untill the form is closed as it checks to see if the address bar contains the text "http://google.com" every 3 seconds.
    If it does then it will go to the homepage.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Simple Loop question i think

    So remove the loop - all it does it wait until the timer is disabled.

    The sub Timer5_Timer will automatically re-run every 3 seconds (3000ms), as that is what you set the Interval to.


    I can't see a reason for the If statement in the Timer5_Timer sub either (or what you actually wanted to check with it).

    The way I see it, your code should be like this:
    VB Code:
    1. With Timer5
    2.         .Interval = 3000
    3.         .Enabled = True
    4.     End With
    5.  
    6. End Sub
    VB Code:
    1. Private Sub Timer5_Timer()
    2.  
    3. check
    4.  
    5. End Sub
    (check would stay as it is).


    If this doesn't do what you want, then tell us what you want different, and we'll explain how to do it.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Simple Loop question i think

    it still dosent work

    i'm tring to make a site blocker that blocks sites, the timer is suppose to run the check command every 3 secs to see if the text in the address bar is http://www.google.com, if it is then it will show a message box and go to the home page.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Simple Loop question i think

    The sub Timer5_Timer will run every 3 seconds. If you want to test, put a MsgBox in there.

    I would guess that the check routine is the problem, as (for whatever reason) txtWebsite.Text is not exactly equal to "http://www.google.com".

    I would recommend removing any spaces from both sides of the text you are checking, and converting it to lowercase, like this:
    VB Code:
    1. If LCase(Trim(txtWebsite.Text)) = "http://www.google.com" Then
    ..if that doesn't work, what is in txtWebsite.Text?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: Simple Loop question i think

    Thanks everyone, its working now.

    Do you think i coud add an access 97 database to the code so it will search through the database.

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Simple Loop question i think

    You certainly could.

    See the ADO Tutorial link in my signature for how to use an Access database from VB, and the "Further Steps" link (within the ADO tutorial) for how to find a record with specific data.

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