PDA

View Full Version : Cycle Picture from Folder



tlamanna
11-09-2011, 04:26 PM
Hi All,

I’m relatively new to vba. What I am looking for, is a way to cycle through a group of pictures in a folder, for a record. Each record may have 3 to 5 pictures.
I am trying to use the Dir function to pass the file name to the picture control. The code below is the only way I have been able to make it work.
Can someone point me in the right direction?

Private Sub Imageloop_Click()
Dim strDocPath As String
Dim strCurrentFile As String
strDocPath = "N:\Images\"
strCurrentFile = Dir(strDocPath & "*.*")
Do While strCurrentFile <> ""
MsgBox strCurrentFile
Imageloop.Picture = [PATH] & strCurrentFile

strCurrentFile = Dir
Loop

End Sub

HiTechCoach
11-10-2011, 01:14 PM
My guess is that you can only see the last picture found since an image control can only display a single image at a time.

I think part of your issue may be how you are trying to display the image and not the Dir() function.

This may also be an issue:

Imageloop.Picture = [PATH] & strCurrentFile
Where is [PATH] coming from? You are using a different variable in the code for the Dir().

try:

Imageloop.Picture = strDocPath & strCurrentFile
We really need some more details. What are you trying to do with the code? What is not working? Are you getting any error messages?

tlamanna
11-11-2011, 10:10 AM
Hi Boyd,

Yep you are right. If I comment out the message box, all I see is the last image. if I leave it in the loop, loop breaks at every message box showing the image each time i click OK on the msgbox.
[Path] is a field containing "N:\Images\" . I know it is redundant with strDocPath. I used [PATH] because each record will have a different folder of images.
Makes me realize I should have strDocPath = [PATH]

What I would ultimately like to accomplish is in a form or report have an image control that will cycle 3 to 5 pictures(as many images that are in the folder listed in [PATH]) for each record, with the use of a control button or by clicking the image.


Thank you for your help.

tlamanna
11-11-2011, 12:34 PM
I got it thanks. I did it like this. I'm not sure it is right but it seems to work.

Thanks for the replay Boyd. :hi:

Option Compare Database
'Declared Global variable for Dir Function
Public strCurrentFile As String


Private Sub Form_Current()
On Error Resume Next

Dim strDocPath As String
'[PATH] = Field containing the path of the folder with images
strDocPath = [PATH]

strCurrentFile = Dir(strDocPath & "*.*")
'Loads the first image from folder on record change
ImageLoop.Picture = [PATH] & strCurrentFile

End Sub



Private Sub ImageLoop_Click()

're-calls Dir()
strCurrentFile = Dir
'Loads next image in folder on click.
ImageLoop.Picture = [TestPath] & strCurrentFile

End Sub

HiTechCoach
11-11-2011, 02:54 PM
Thanks fo posting your solution.

The issue here is in th UI. I have handle this in a few ways:

1) fill a list box with all the images names. When you click an item in the list box the image control will show that image. This allow the use to view the images in any order and redisplay an image.

2) same as above but use a treeview control

3) use a continuous form. This is tricky with the image control in Access. There are some third party controls that make this very easy. See DBPix (http://www.hitechcoach.com/index.php?option=com_weblinks&view=weblink&id=133:dbpix-the-image-control-for-microsoftr-access&catid=56:access-developer-tools-add-ins-and-vba-code&Itemid=20) and AccesImage (http://www.hitechcoach.com/index.php?option=com_weblinks&view=weblink&id=424:accessimagine-image-contol&catid=56:access-developer-tools-add-ins-and-vba-code&Itemid=20)