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. - 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
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.
should be - 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. - 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: - 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.
24 6236
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.
should be - 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. - 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: - 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.
NeoPa 32,556
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.
@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
@NeoPa
NeoPa,
Thanks for keeping an eye on my questions. Thanks.
@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 - DisplayImage Image1, "C:\MyImages\mypicture.jpg"
@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.
@MNNovice
That should have worked. Have you by any chance got another control or object that also has the name DisplayImage?
@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? - 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
Don't combine them. Add my function to the same module but change the name from DisplayImage to ShowImage. - 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. -
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
@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.
@MNNovice
No problem. To change the interval the only thing you should need to change is the interval times on your if statements.
@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.
NeoPa 32,556
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 : - 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 :)
@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
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 - 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
@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: - 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.
@MNNovice
I take it all your pictures have been renamed C1.jpg to C26.jpg :D
@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.
NeoPa 32,556
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.
@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 ... - 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
NeoPa 32,556
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.
Hi MN
The only thing showing in your post is my quoted thread
Mary
@msquared
Sorry, I just wanted to let you know that everything worked out fine. Many thanks for your help. Until my next question... MNNovice
@MNNovice
No problem, glad you got it working. I deleted that post as it didn't make sense :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michele Simionato |
last post by:
Strangely enough, I never needed the datetime and calendar module
before,
so I just looked at them today. I am surprised I don't easily find an
interval
function such this:
import datetime
...
|
by: Marco Alting |
last post by:
Can anyone tell me how to write a routine that uses time-intervals, I would
like to dynamically create an array of times like this:
9:00
9:30
10:00
10:30
This has an interval of half an...
|
by: sunilkeswani |
last post by:
Hi
I want an access table to auto export the data in a HTML format, at
regular time intervals specified...Like every hour. The html file
should have the same name, so that it can display on...
|
by: magix |
last post by:
Hi,
Do you have any javascript available for changing images according to time ?
let say Morning (7am-12pm), using images 1 (e.g morning.jpg)
Afternoon (12 - 6pm), using images 2 (e.g...
|
by: Rombolt |
last post by:
Hi I have a MSSQL table with many time intervals stored as datetime. Each time interval is also assigned a numeric type that specifies what type of job was done during the time interval.
I need to...
|
by: sudhashekhar30 |
last post by:
hi all
i am using formview control(asp.net 2.0) 1rst time. don't know much about it.
i want to display record in it from different tables at different time. like 1rst time form view is showing...
|
by: Correia |
last post by:
I have a webserver that is in another country and have a different
time zone.
How can i fix this and use the scripts with the correct time zone?
Thanks
|
by: CGatto |
last post by:
Hello,
Just wondering if anyone has ever managed to find a way to have a
datagridviewimagecolumn display different images on different rows depending
on some data element in the row. Our...
|
by: handique |
last post by:
Hi,
I have javascript code for rotating images, but the rotation starts only when mouse is placed over the image. But i want to rotate images automatically when the page loads. Can any guide me in...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |