Rotate Images on Form at Different Time Intervals

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MNNovice
    Contributor
    • Aug 2008
    • 418

    Rotate Images on Form at Different Time Intervals

    I would like to display 3 different images on my form fMenu. I created three different images and placed them on the same spot of fMenu. Now I added this code but it is not working. What am I doing wrong? How do I fix it? Any help will be much appreciated.

    Code:
    Private Sub Form_Activate()
    'When the database is opened rotate 3 different images.
    
    
        If Time() < 0.5 Then
            [Image1].Visible = False
            [Image2].Visible = False
            [Image3].Visible = True
    
        ElseIf Time() > 0.5 And Time() < 0.75 Then
            [Image1].Visible = False
            [Image2].Visible = True
            [Image3].Visible = False
    
        ElseIf Time() > 0.75 Then
            [Image1].Visible = True
            [Image2].Visible = False
            [Image3].Visible = False
        End If
    
    End Sub
    I used similar codes to display 3 different welcome message on a different DB and that works just fine.

    Thanks for your help.

    M
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    i think your problem may lie in the fact that the images are unbound. Firstly I should point out that you should ALWAYS use the Me or Forms!Formname reference when referring to a control on a form.

    Code:
    [Image1].Visible = False
    should be

    Code:
    Me.Image1.Visible = False
    Instead of using three different image controls you could just use the one and use a simple function like this instead.

    Code:
    Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant)
    Dim strDatabasePath As String
    Dim intSlashLocation As Integer
    
        With ctlImageControl
            .Visible = True
            .Picture = strImagePath
        End With
        
    End Function
    Now if you change your code as follows:

    Code:
    Private Sub Form_Activate() 
    'When the database is opened rotate 3 different images. 
      
        If Time() < 0.5 Then 
            DisplayImage Image1, "Path to Picture1"
        ElseIf Time() > 0.5 And Time() < 0.75 Then 
            DisplayImage Image1, "Path to Picture2"  
        ElseIf Time() > 0.75 Then 
            DisplayImage Image1, "Path to Picture3"
        End If 
      
    End Sub
    This should rotate the images as you want them. Please note I only used one image control and just rotated the picture.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32645

      #3
      Safer hands you couldn't find for an imaging question MH. This is the member who answered my first really tricky question on Bytes (TheScripts as it was then) over 3.5 years ago.

      Comment

      • MNNovice
        Contributor
        • Aug 2008
        • 418

        #4
        Originally posted by msquared
        i think your problem may lie in the fact that the images are unbound. Firstly I should point out that you should ALWAYS use the Me or Forms!Formname reference when referring to a control on a form.

        Code:
        [Image1].Visible = False
        should be

        Code:
        Me.Image1.Visible = False
        Instead of using three different image controls you could just use the one and use a simple function like this instead.

        Code:
        Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant)
        Dim strDatabasePath As String
        Dim intSlashLocation As Integer
        
            With ctlImageControl
                .Visible = True
                .Picture = strImagePath
            End With
            
        End Function
        Now if you change your code as follows:

        Code:
        Private Sub Form_Activate() 
        'When the database is opened rotate 3 different images. 
          
            If Time() < 0.5 Then 
                DisplayImage Image1, "Path to Picture1"
            ElseIf Time() > 0.5 And Time() < 0.75 Then 
                DisplayImage Image1, "Path to Picture2"  
            ElseIf Time() > 0.75 Then 
                DisplayImage Image1, "Path to Picture3"
            End If 
          
        End Sub
        This should rotate the images as you want them. Please note I only used one image control and just rotated the picture.
        Thanks for your suggestions and for the codes. Here are my questions:

        1. When I tried to execute I got this error message: "Ambiguous Name Detected: DisplayImage" on line #5

        2. You said I should have one image control instead of 3. So do I name this image control as Image1?
        3. What do these Picture1 , 2 and 3 refer to? Are these 3 different pictures that will be rotated with time?

        4. Where will I have these pictures saved then? I prefer it to be outside of the DB.

        Thanks for your time to help me out with this. MNNovice

        Comment

        • MNNovice
          Contributor
          • Aug 2008
          • 418

          #5
          Originally posted by NeoPa
          Safer hands you couldn't find for an imaging question MH. This is the member who answered my first really tricky question on Bytes (TheScripts as it was then) over 3.5 years ago.
          NeoPa,

          Thanks for keeping an eye on my questions. Thanks.

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Originally posted by MNNovice
            Thanks for your suggestions and for the codes. Here are my questions:

            1. When I tried to execute I got this error message: "Ambiguous Name Detected: DisplayImage" on line #5

            2. You said I should have one image control instead of 3. So do I name this image control as Image1?
            3. What do these Picture1 , 2 and 3 refer to? Are these 3 different pictures that will be rotated with time?

            4. Where will I have these pictures saved then? I prefer it to be outside of the DB.

            Thanks for your time to help me out with this. MNNovice
            1. Where did you put the code for the DisplayImage function I gave you?

            2. Yes for the purposes of this code I named the image control Image1

            3. The answer is yes and to cover your question 4 as well. You save the three pictures in a folder anywhere you like that can be accessed by the user. So for example if I create a folder directly on the C drive called MyImages and save the three pictures in there. We'll say the first picture file is called mypicture.jpg so the code to display that image would be

            Code:
            DisplayImage Image1, "C:\MyImages\mypicture.jpg"

            Comment

            • MNNovice
              Contributor
              • Aug 2008
              • 418

              #7
              Originally posted by msquared
              1. Where did you put the code for the DisplayImage function I gave you?

              2. Yes for the purposes of this code I named the image control Image1

              3. The answer is yes and to cover your question 4 as well. You save the three pictures in a folder anywhere you like that can be accessed by the user. So for example if I create a folder directly on the C drive called MyImages and save the three pictures in there. We'll say the first picture file is called mypicture.jpg so the code to display that image would be

              Code:
              DisplayImage Image1, "C:\MyImages\mypicture.jpg"
              1. I created a new Module (Module2), that's where I added the codes mentioned above. i.e., Public Function DisplayImage. AND
              I added the other code, i.e., Private Sub Form... as On Active Event procedure for the form.

              Thanks.

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Originally posted by MNNovice
                1. I created a new Module (Module2), that's where I added the codes mentioned above. i.e., Public Function DisplayImage. AND
                I added the other code, i.e., Private Sub Form... as On Active Event procedure for the form.

                Thanks.
                That should have worked. Have you by any chance got another control or object that also has the name DisplayImage?

                Comment

                • MNNovice
                  Contributor
                  • Aug 2008
                  • 418

                  #9
                  Originally posted by msquared
                  That should have worked. Have you by any chance got another control or object that also has the name DisplayImage?
                  Yes, I do. Now I know why it was ambiguous. Well here is the code I have in Module 1. I used it to include picture of DVD cover for each DVD record.

                  I just don't know how to combine yours with this one. Can you please help?

                  Code:
                  Option Compare Database
                  Option Explicit
                  
                  Dim strResult As String
                  Dim strDatabasePath As String
                  Dim intSlashLocation As Integer
                  Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
                  On Error GoTo Err_DisplayImage
                  
                  
                  With ctlImageControl
                      If IsNull(strImagePath) Then
                          .Visible = False
                          strResult = "No image Available"
                      Else
                          If InStr(1, strImagePath, "\") = 0 Then
                              ' Path is relative
                              strDatabasePath = CurrentProject.FullName
                              intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
                              strDatabasePath = Left(strDatabasePath, intSlashLocation)
                              strImagePath = strDatabasePath & strImagePath
                          End If
                          .Visible = True
                          .Picture = strImagePath
                          strResult = ""
                      End If
                  End With
                      
                  Exit_DisplayImage:
                      DisplayImage = strResult
                      Exit Function
                  
                  Err_DisplayImage:
                      Select Case Err.Number
                          Case 2220       ' Can't find the picture.
                              ctlImageControl.Visible = False
                              strResult = "Can't find image in the specified name."
                              Resume Exit_DisplayImage:
                          Case Else       ' Some other error.
                              MsgBox Err.Number & " " & Err.Description
                              strResult = "An error occurred displaying image."
                              Resume Exit_DisplayImage:
                      End Select
                  End Function

                  Comment

                  • MMcCarthy
                    Recognized Expert MVP
                    • Aug 2006
                    • 14387

                    #10
                    Don't combine them. Add my function to the same module but change the name from DisplayImage to ShowImage.

                    Code:
                    Public Function ShowImage(ctlImageControl As Control, strImagePath As Variant) 
                    Dim strDatabasePath As String 
                    Dim intSlashLocation As Integer 
                      
                        With ctlImageControl 
                            .Visible = True 
                            .Picture = strImagePath 
                        End With 
                      
                    End Function
                    And then change the code in your form event from DisplayImage to ShowImage.

                    Code:
                        If Time() < 0.5 Then  
                            ShowImage Image1, "Path to Picture1" 
                        ElseIf Time() > 0.5 And Time() < 0.75 Then  
                            ShowImage Image1, "Path to Picture2"   
                        ElseIf Time() > 0.75 Then  
                            ShowImage Image1, "Path to Picture3" 
                        End If

                    Comment

                    • MNNovice
                      Contributor
                      • Aug 2008
                      • 418

                      #11
                      Originally posted by msquared
                      Don't combine them. Add my function to the same module but change the name from DisplayImage to ShowImage.

                      Code:
                      Public Function ShowImage(ctlImageControl As Control, strImagePath As Variant) 
                      Dim strDatabasePath As String 
                      Dim intSlashLocation As Integer 
                        
                          With ctlImageControl 
                              .Visible = True 
                              .Picture = strImagePath 
                          End With 
                        
                      End Function
                      And then change the code in your form event from DisplayImage to ShowImage.

                      Code:
                          If Time() < 0.5 Then  
                              ShowImage Image1, "Path to Picture1" 
                          ElseIf Time() > 0.5 And Time() < 0.75 Then  
                              ShowImage Image1, "Path to Picture2"   
                          ElseIf Time() > 0.75 Then  
                              ShowImage Image1, "Path to Picture3" 
                          End If
                      Wallah!!! It's working great. Thank you so much.

                      Last question. How the codes will change if I were to choose pictures to change every 10 minutes or so instead of one for morning, one for the afternoon and one for the evening?

                      Many thanks for your time and effort in teaching me this new trick. Regards.

                      Comment

                      • MMcCarthy
                        Recognized Expert MVP
                        • Aug 2006
                        • 14387

                        #12
                        Originally posted by MNNovice
                        Wallah!!! It's working great. Thank you so much.

                        Last question. How the codes will change if I were to choose pictures to change every 10 minutes or so instead of one for morning, one for the afternoon and one for the evening?

                        Many thanks for your time and effort in teaching me this new trick. Regards.
                        No problem. To change the interval the only thing you should need to change is the interval times on your if statements.

                        Comment

                        • MNNovice
                          Contributor
                          • Aug 2008
                          • 418

                          #13
                          Originally posted by msquared
                          No problem. To change the interval the only thing you should need to change is the interval times on your if statements.
                          I was having problem with that very issue that you mentioned. How do I change the time from 24-hour setting to minutes? I mean right now the code is set for before 12pm, after 12pm and before 6pm, and after 6 pm. If I were to change it to minutes - how do I write the code to reflect a 10 minutes interval? Do I also have to change Time() to Minute(Time())? Can you give me some tips?

                          Thanks.

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32645

                            #14
                            You'll probably find it easier to work with time values (literals) if you do it in the Date/Time format (#06:00# for instance, which is numerically equaivalent to 0.25).

                            If you change Mary's code to say :
                            Code:
                            Private Sub Form_Activate()
                            'When the database is opened rotate 3 different images.
                            
                                If Time() < #12:00# Then
                                    DisplayImage Image1, "Path to Picture1"
                                ElseIf Time() < #18:00# Then
                                    DisplayImage Image1, "Path to Picture2"
                                Else
                                    DisplayImage Image1, "Path to Picture3"
                                End If
                            
                            End Sub
                            I think you'll find that easier to work with :)

                            Comment

                            • MMcCarthy
                              Recognized Expert MVP
                              • Aug 2006
                              • 14387

                              #15
                              Originally posted by MNNovice
                              I was having problem with that very issue that you mentioned. How do I change the time from 24-hour setting to minutes? I mean right now the code is set for before 12pm, after 12pm and before 6pm, and after 6 pm. If I were to change it to minutes - how do I write the code to reflect a 10 minutes interval? Do I also have to change Time() to Minute(Time())? Can you give me some tips?

                              Thanks.
                              To do what you want you will need to look at the forms On Timer event.

                              If you check out the event list for the form you will see an event near the bottom called On Timer. Underneath it is something called timer interval which defaults to 0. This is set in milliseconds so 10000 would be 10 seconds.

                              Now if you set the timer interval and add code to the On timer event then the code will execute every time this interval is reached.

                              Now this is fine if we just want to do a specific action requery a form every 10 seconds. However, what we are looking to do here is to rotate between three files every interval so let me think on the logic and I'll get back to you.

                              Mary

                              Comment

                              Working...