Rotate Images on Form at Different Time Intervals

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #16
    OK I've tested this out and it seems to work fine.

    Fistly you will have to declare an integer variable outside of the forms events so it can remember its value. I've used i as the variable name and declared it just under Option Explicit. The variable needs to be initialised as soon as the form is opened so I've added a statement to the forms load event to initialise it to 1. Now in the forms On Timer Event I added a select case statement to rotate through 3 pictures. If there is anything you don't understand let me know.

    Don't forget to set the time interval in the properties events list to whatever interval you want to use. e.g. 3000 for 3 seconds

    Code:
    Option Compare Database
    Option Explicit
    
    Dim i As Integer
    
    
    Private Sub Form_Load()
    
        i = 1
        
    End Sub
    
    
    Private Sub Form_Timer()
    'When the database is opened rotate 3 different images.
    Dim path As String
    
        path = "C:\Documents\"
        
        Select Case i
        Case 1
            ShowImage Me.Image1, path & "MyPicture1.jpg"
        Case 2
            ShowImage Me.Image1, path & "MyPicture2.jpg"
        Case 3
            ShowImage Me.Image1, path & "MyPicture3.jpg"
        End Select
        
        If i = 3 Then
            i = 1
        Else
            i = i + 1
        End If
    
    End Sub

    Comment

    • MNNovice
      Contributor
      • Aug 2008
      • 418

      #17
      Originally posted by msquared
      OK I've tested this out and it seems to work fine.

      Fistly you will have to declare an integer variable outside of the forms events so it can remember its value. I've used i as the variable name and declared it just under Option Explicit. The variable needs to be initialised as soon as the form is opened so I've added a statement to the forms load event to initialise it to 1. Now in the forms On Timer Event I added a select case statement to rotate through 3 pictures. If there is anything you don't understand let me know.

      Don't forget to set the time interval in the properties events list to whatever interval you want to use. e.g. 3000 for 3 seconds

      Code:
      Option Compare Database
      Option Explicit
      
      Dim i As Integer
      
      
      Private Sub Form_Load()
      
          i = 1
          
      End Sub
      
      
      Private Sub Form_Timer()
      'When the database is opened rotate 3 different images.
      Dim path As String
      
          path = "C:\Documents\"
          
          Select Case i
          Case 1
              ShowImage Me.Image1, path & "MyPicture1.jpg"
          Case 2
              ShowImage Me.Image1, path & "MyPicture2.jpg"
          Case 3
              ShowImage Me.Image1, path & "MyPicture3.jpg"
          End Select
          
          If i = 3 Then
              i = 1
          Else
              i = i + 1
          End If
      
      End Sub
      Many thanks for your help. But I added too many pics (26 for now). So my husband helped me modify these codes to make it more efficient. Thought you would like to know how we did it. Here it is:

      Code:
      Private Sub Form_Timer()
      'When the database is opened rotate 3 different images.
      
      Static i As Integer
      Dim path As String
      Dim path1 As String
      Dim iString As String
      
      
        
          path = "C:\HRS_DATABASE\MenuPics\C-"
          path1 = ".jpg"
          
         If i = 26 Then
              i = 1
          Else
              i = i + 1
          End If
          
        iString = i
        
          Select Case i
          
          Case i
              ShowImage Me.Image1, path & iString & path1
          
          End Select
        
         
      End Sub
      I have two questions.

      1. How can I make the first pic to show at 0 second and not wait for the set time interval. Right now the first picture takes 3 seconds to display?

      2. How do we add an error comment to this code?

      Thanks.

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #18
        Originally posted by MNNovice
        Many thanks for your help. But I added too many pics (26 for now). So my husband helped me modify these codes to make it more efficient. Thought you would like to know how we did it. Here it is:

        Code:
        Private Sub Form_Timer()
        'When the database is opened rotate 3 different images.
        
        Static i As Integer
        Dim path As String
        Dim path1 As String
        Dim iString As String
        
        
          
            path = "C:\HRS_DATABASE\MenuPics\C-"
            path1 = ".jpg"
            
           If i = 26 Then
                i = 1
            Else
                i = i + 1
            End If
            
          iString = i
          
            Select Case i
            
            Case i
                ShowImage Me.Image1, path & iString & path1
            
            End Select
          
           
        End Sub
        I take it all your pictures have been renamed C1.jpg to C26.jpg :D

        Comment

        • MNNovice
          Contributor
          • Aug 2008
          • 418

          #19
          Originally posted by msquared
          I take it all your pictures have been renamed C1.jpg to C26.jpg :D
          My pics are named C-1.jpg, C-2.jpg ... C-26.jpg.

          You didn't answer my questions. How can I make the first pic to display without waiting 3 seconds?

          Thanks.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32645

            #20
            You need to set that when the form opens M, or just design it into the form if it is always the same picture.

            In such situations where the same code is run from multiple places, it's always a good idea to encapsulate the logic into a single procedure then call that procedure from all the places it needs to work from, rather than duplicating the same logic in different places. That's never good.

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #21
              Originally posted by MNNovice
              My pics are named C-1.jpg, C-2.jpg ... C-26.jpg.

              You didn't answer my questions. How can I make the first pic to display without waiting 3 seconds?

              Thanks.
              Sorry I didn't see the questions at the bottom :)

              As NeoPa says, if you put an image in the box in the forms open or load event that will populate immediately. The On Timer event doesn't trigger until the first interval has passed. When running this on test I started with an image in the control.

              To add an error comment you just add ...

              Code:
              Private Sub Form_Timer() 
              ' on any error go to Err_Form_Timer
              On Error GoTo Err_Form_Timer  
              Static i As Integer 
              Dim path As String 
              Dim path1 As String 
              Dim iString As String 
                
                
                
                  path = "C:\HRS_DATABASE\MenuPics\C-" 
                  path1 = ".jpg" 
                
                 If i = 26 Then 
                      i = 1 
                  Else 
                      i = i + 1 
                  End If 
                
                iString = i 
                
                  Select Case i 
                
                  Case i 
                      ShowImage Me.Image1, path & iString & path1 
                
                  End Select 
              
              Exit_Form_Timer: 
              
                  Exit Sub   
               
              Err_Form_Timer: 
               
                  MsgBox Err.Number & " " & Err.Description 
                  Resume Exit_Form_Timer: 
               
              End Sub
              Mary

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32645

                #22
                An interesting alternative which saves the requirement to duplicate the code (but does involve the interval being hidden in the code and less obvious for project review) would be to set the timer interval to 1 in the design, then change that in the timer code itself. That way the first occurrence happens (to all intents and purposes) immediately, but all subsequent occurrences wait the requisite interval before triggering.

                Comment

                • MMcCarthy
                  Recognized Expert MVP
                  • Aug 2006
                  • 14387

                  #23
                  Hi MN

                  The only thing showing in your post is my quoted thread

                  Mary

                  Comment

                  • MNNovice
                    Contributor
                    • Aug 2008
                    • 418

                    #24
                    Originally posted by msquared
                    Hi MN

                    The only thing showing in your post is my quoted thread

                    Mary
                    Sorry, I just wanted to let you know that everything worked out fine. Many thanks for your help. Until my next question... MNNovice

                    Comment

                    • MMcCarthy
                      Recognized Expert MVP
                      • Aug 2006
                      • 14387

                      #25
                      Originally posted by MNNovice
                      Sorry, I just wanted to let you know that everything worked out fine. Many thanks for your help. Until my next question... MNNovice
                      No problem, glad you got it working. I deleted that post as it didn't make sense :)

                      Comment

                      Working...