469,644 Members | 2,030 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

Rotate Images on Form at Different Time Intervals

418 256MB
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Activate()
  2. 'When the database is opened rotate 3 different images.
  3.  
  4.  
  5.     If Time() < 0.5 Then
  6.         [Image1].Visible = False
  7.         [Image2].Visible = False
  8.         [Image3].Visible = True
  9.  
  10.     ElseIf Time() > 0.5 And Time() < 0.75 Then
  11.         [Image1].Visible = False
  12.         [Image2].Visible = True
  13.         [Image3].Visible = False
  14.  
  15.     ElseIf Time() > 0.75 Then
  16.         [Image1].Visible = True
  17.         [Image2].Visible = False
  18.         [Image3].Visible = False
  19.     End If
  20.  
  21. End Sub
  22.  
I used similar codes to display 3 different welcome message on a different DB and that works just fine.

Thanks for your help.

M
Jun 10 '10 #1

✓ answered by MMcCarthy

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.

Expand|Select|Wrap|Line Numbers
  1. [Image1].Visible = False
should be

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant)
  2. Dim strDatabasePath As String
  3. Dim intSlashLocation As Integer
  4.  
  5.     With ctlImageControl
  6.         .Visible = True
  7.         .Picture = strImagePath
  8.     End With
  9.  
  10. End Function
Now if you change your code as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Activate() 
  2. 'When the database is opened rotate 3 different images. 
  3.  
  4.     If Time() < 0.5 Then 
  5.         DisplayImage Image1, "Path to Picture1"
  6.     ElseIf Time() > 0.5 And Time() < 0.75 Then 
  7.         DisplayImage Image1, "Path to Picture2"  
  8.     ElseIf Time() > 0.75 Then 
  9.         DisplayImage Image1, "Path to Picture3"
  10.     End If 
  11.  
  12. End Sub
This should rotate the images as you want them. Please note I only used one image control and just rotated the picture.

24 5877
MMcCarthy
14,534 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. [Image1].Visible = False
should be

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant)
  2. Dim strDatabasePath As String
  3. Dim intSlashLocation As Integer
  4.  
  5.     With ctlImageControl
  6.         .Visible = True
  7.         .Picture = strImagePath
  8.     End With
  9.  
  10. End Function
Now if you change your code as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Activate() 
  2. 'When the database is opened rotate 3 different images. 
  3.  
  4.     If Time() < 0.5 Then 
  5.         DisplayImage Image1, "Path to Picture1"
  6.     ElseIf Time() > 0.5 And Time() < 0.75 Then 
  7.         DisplayImage Image1, "Path to Picture2"  
  8.     ElseIf Time() > 0.75 Then 
  9.         DisplayImage Image1, "Path to Picture3"
  10.     End If 
  11.  
  12. End Sub
This should rotate the images as you want them. Please note I only used one image control and just rotated the picture.
Jun 11 '10 #2
NeoPa
32,203 Expert Mod 16PB
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.
Jun 11 '10 #3
MNNovice
418 256MB
@msquared
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
Jun 11 '10 #4
MNNovice
418 256MB
@NeoPa
NeoPa,

Thanks for keeping an eye on my questions. Thanks.
Jun 11 '10 #5
MMcCarthy
14,534 Expert Mod 8TB
@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

Expand|Select|Wrap|Line Numbers
  1. DisplayImage Image1, "C:\MyImages\mypicture.jpg"
Jun 11 '10 #6
MNNovice
418 256MB
@msquared
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.
Jun 11 '10 #7
MMcCarthy
14,534 Expert Mod 8TB
@MNNovice
That should have worked. Have you by any chance got another control or object that also has the name DisplayImage?
Jun 11 '10 #8
MNNovice
418 256MB
@msquared
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?

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim strResult As String
  5. Dim strDatabasePath As String
  6. Dim intSlashLocation As Integer
  7. Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
  8. On Error GoTo Err_DisplayImage
  9.  
  10.  
  11. With ctlImageControl
  12.     If IsNull(strImagePath) Then
  13.         .Visible = False
  14.         strResult = "No image Available"
  15.     Else
  16.         If InStr(1, strImagePath, "\") = 0 Then
  17.             ' Path is relative
  18.             strDatabasePath = CurrentProject.FullName
  19.             intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
  20.             strDatabasePath = Left(strDatabasePath, intSlashLocation)
  21.             strImagePath = strDatabasePath & strImagePath
  22.         End If
  23.         .Visible = True
  24.         .Picture = strImagePath
  25.         strResult = ""
  26.     End If
  27. End With
  28.  
  29. Exit_DisplayImage:
  30.     DisplayImage = strResult
  31.     Exit Function
  32.  
  33. Err_DisplayImage:
  34.     Select Case Err.Number
  35.         Case 2220       ' Can't find the picture.
  36.             ctlImageControl.Visible = False
  37.             strResult = "Can't find image in the specified name."
  38.             Resume Exit_DisplayImage:
  39.         Case Else       ' Some other error.
  40.             MsgBox Err.Number & " " & Err.Description
  41.             strResult = "An error occurred displaying image."
  42.             Resume Exit_DisplayImage:
  43.     End Select
  44. End Function
Jun 11 '10 #9
MMcCarthy
14,534 Expert Mod 8TB
Don't combine them. Add my function to the same module but change the name from DisplayImage to ShowImage.

Expand|Select|Wrap|Line Numbers
  1. Public Function ShowImage(ctlImageControl As Control, strImagePath As Variant) 
  2. Dim strDatabasePath As String 
  3. Dim intSlashLocation As Integer 
  4.  
  5.     With ctlImageControl 
  6.         .Visible = True 
  7.         .Picture = strImagePath 
  8.     End With 
  9.  
  10. End Function 
And then change the code in your form event from DisplayImage to ShowImage.

Expand|Select|Wrap|Line Numbers
  1.     If Time() < 0.5 Then  
  2.         ShowImage Image1, "Path to Picture1" 
  3.     ElseIf Time() > 0.5 And Time() < 0.75 Then  
  4.         ShowImage Image1, "Path to Picture2"   
  5.     ElseIf Time() > 0.75 Then  
  6.         ShowImage Image1, "Path to Picture3" 
  7.     End If  
Jun 11 '10 #10
MNNovice
418 256MB
@msquared
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.
Jun 11 '10 #11
MMcCarthy
14,534 Expert Mod 8TB
@MNNovice
No problem. To change the interval the only thing you should need to change is the interval times on your if statements.
Jun 11 '10 #12
MNNovice
418 256MB
@msquared
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.
Jun 12 '10 #13
NeoPa
32,203 Expert Mod 16PB
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 :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Activate()
  2. 'When the database is opened rotate 3 different images.
  3.  
  4.     If Time() < #12:00# Then
  5.         DisplayImage Image1, "Path to Picture1"
  6.     ElseIf Time() < #18:00# Then
  7.         DisplayImage Image1, "Path to Picture2"
  8.     Else
  9.         DisplayImage Image1, "Path to Picture3"
  10.     End If
  11.  
  12. End Sub
I think you'll find that easier to work with :)
Jun 12 '10 #14
MMcCarthy
14,534 Expert Mod 8TB
@MNNovice
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
Jun 12 '10 #15
MMcCarthy
14,534 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim i As Integer
  5.  
  6.  
  7. Private Sub Form_Load()
  8.  
  9.     i = 1
  10.  
  11. End Sub
  12.  
  13.  
  14. Private Sub Form_Timer()
  15. 'When the database is opened rotate 3 different images.
  16. Dim path As String
  17.  
  18.     path = "C:\Documents\"
  19.  
  20.     Select Case i
  21.     Case 1
  22.         ShowImage Me.Image1, path & "MyPicture1.jpg"
  23.     Case 2
  24.         ShowImage Me.Image1, path & "MyPicture2.jpg"
  25.     Case 3
  26.         ShowImage Me.Image1, path & "MyPicture3.jpg"
  27.     End Select
  28.  
  29.     If i = 3 Then
  30.         i = 1
  31.     Else
  32.         i = i + 1
  33.     End If
  34.  
  35. End Sub
Jun 12 '10 #16
MNNovice
418 256MB
@msquared
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Timer()
  2. 'When the database is opened rotate 3 different images.
  3.  
  4. Static i As Integer
  5. Dim path As String
  6. Dim path1 As String
  7. Dim iString As String
  8.  
  9.  
  10.  
  11.     path = "C:\HRS_DATABASE\MenuPics\C-"
  12.     path1 = ".jpg"
  13.  
  14.    If i = 26 Then
  15.         i = 1
  16.     Else
  17.         i = i + 1
  18.     End If
  19.  
  20.   iString = i
  21.  
  22.     Select Case i
  23.  
  24.     Case i
  25.         ShowImage Me.Image1, path & iString & path1
  26.  
  27.     End Select
  28.  
  29.  
  30. End Sub
  31.  
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.
Jun 13 '10 #17
MMcCarthy
14,534 Expert Mod 8TB
@MNNovice
I take it all your pictures have been renamed C1.jpg to C26.jpg :D
Jun 13 '10 #18
MNNovice
418 256MB
@msquared
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.
Jun 13 '10 #19
NeoPa
32,203 Expert Mod 16PB
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.
Jun 13 '10 #20
MMcCarthy
14,534 Expert Mod 8TB
@MNNovice
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 ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Timer() 
  2. ' on any error go to Err_Form_Timer
  3. On Error GoTo Err_Form_Timer  
  4. Static i As Integer 
  5. Dim path As String 
  6. Dim path1 As String 
  7. Dim iString As String 
  8.  
  9.  
  10.  
  11.     path = "C:\HRS_DATABASE\MenuPics\C-" 
  12.     path1 = ".jpg" 
  13.  
  14.    If i = 26 Then 
  15.         i = 1 
  16.     Else 
  17.         i = i + 1 
  18.     End If 
  19.  
  20.   iString = i 
  21.  
  22.     Select Case i 
  23.  
  24.     Case i 
  25.         ShowImage Me.Image1, path & iString & path1 
  26.  
  27.     End Select 
  28.  
  29. Exit_Form_Timer: 
  30.  
  31.     Exit Sub   
  32.  
  33. Err_Form_Timer: 
  34.  
  35.     MsgBox Err.Number & " " & Err.Description 
  36.     Resume Exit_Form_Timer: 
  37.  
  38. End Sub 
Mary
Jun 13 '10 #21
NeoPa
32,203 Expert Mod 16PB
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.
Jun 13 '10 #22
MMcCarthy
14,534 Expert Mod 8TB
Hi MN

The only thing showing in your post is my quoted thread

Mary
Jun 13 '10 #23
MNNovice
418 256MB
@msquared
Sorry, I just wanted to let you know that everything worked out fine. Many thanks for your help. Until my next question... MNNovice
Jun 14 '10 #24
MMcCarthy
14,534 Expert Mod 8TB
@MNNovice
No problem, glad you got it working. I deleted that post as it didn't make sense :)
Jun 14 '10 #25

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Michele Simionato | last post: by
1 post views Thread by Marco Alting | last post: by
1 post views Thread by magix | last post: by
7 posts views Thread by Correia | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.