-1

I am trying to create a image slideshow by changing the Image control .picture property by looping through all images in a predefined folder

C:\Images

The code I am using:

    Public pixpaths As Collection
    Public pix_path As String
    Public pixnum As Integer
    Public fs As YtoFileSearch
    Public k As Integer

    Public Sub Image_set()
    Set pixpaths = New Collection
    pix_path = "C:\Images"
    Set fs = New YtoFileSearch
    With fs
      .NewSearch
      .LookIn = pix_path
      .fileName = "*.jpg"
      If fs.Execute() > 0 Then
        For k = 1 To .FoundFiles.Count
          pixpaths.Add Item:=.FoundFiles(k)
        Next k
      Else
        MsgBox "No files found!"
        DoCmd.OpenForm "Fr_Sketchpad"    ' If no images found in folder the set image from another form 'Sketchpad' image control
        Forms!Fr_Sketchpad.Visible = False
        Forms!Fr_Main!imgPixHolder.Picture = "" 'Forms!Fr_Sketchpad!Img_Std.Picture   Was getting another error here so commented this
        pixnum = 0
        Exit Sub
      End If
    End With
    'load first pix
    Forms!Fr_Main.imgPixHolder.Picture = pixpaths(1)
    pixnum = 1
    End Sub

    Public Sub Image_loop()
          If pixnum = pixpaths.Count Then
          pixnum = 1
        ElseIf pixnum = 0 Then
            Exit Sub
        Else
          pixnum = pixnum + 1
          Forms!Fr_Main!imgPixHolder.Picture = pixpaths(pixnum)
        End If
    End Sub

    Private Sub Form_Open(Cancel As Integer)
     Call Image_set
    End Sub

    Private Sub Form_Timer()
     Call Image_loop
    End Sub

The Image_Set(), Image_loop() and variables are in one module and are called in Form_open and Form_timer events The code is working fine for one loop cycle but for the next loop cycle it is showing an error:

Error 91 object variable or with block variable not set

on

If pixnum = pixpaths.Count Then

In debug mode when I check value for pixnum it is 0

[Update] Class Module YtoFileSearch

    Option Compare Database
Option Explicit

' How this is not another proof that doing VBA is a bad idea?
' Nevertheless, we'll try to make the scripts relying on Application.FileSearch works again.

' The interface of this YtoFileSearch class aims to stick to the original
' Application.FileSearch class interface.
' Cf is https://msdn.microsoft.com/en-us/library/office/aa219847(v=office.11).aspx

' For now it do not handle recursive search and only search for files.
' More precisely the following filters are not implemented:
' * SearchSubFolders
' * MatchTextExactly
' * FileType
' If that's something you need, please create an issue so we have a look at it.

' Our class attributes.
Private pDirectoryPath As String
Private pFileNameFilter As String
Private pFoundFiles As Collection

' Set the directory in which we will search.
Public Property Let LookIn(directoryPath As String)
    pDirectoryPath = directoryPath
End Property

' Allow to filter by file name.
Public Property Let fileName(fileName As String)
    pFileNameFilter = fileName
End Property

'Property to get all the found files.
Public Property Get FoundFiles() As Collection
    Set FoundFiles = pFoundFiles
End Property

' Reset the FileSearch object for a new search.
Public Sub NewSearch()
    'Reset the found files object.
    Set pFoundFiles = New Collection
    ' and the search criterions.
    pDirectoryPath = ""
    pFileNameFilter = ""
End Sub

' Launch the search and return the number of occurrences.
Public Function Execute() As Long
    'Lance la recherche
    doSearch

    Execute = pFoundFiles.Count
End Function

' Do the nasty work here.
Private Sub doSearch()
    Dim directoryPath As String
    Dim currentFile As String
    Dim filter As String

    directoryPath = pDirectoryPath
    If InStr(Len(pDirectoryPath), pDirectoryPath, "\") = 0 Then
        directoryPath = directoryPath & "\"
    End If

    ' If no directory is specified, abort the search.
    If Len(directoryPath) = 0 Then
        Exit Sub
    End If

    ' Check that directoryPath is a valid directory path.
    ' http://stackoverflow.com/questions/15480389/excel-vba-check-if-directory-exists-error
    If Dir(directoryPath, vbDirectory) = "" Then
        Debug.Print "Directory " & directoryPath & " does not exists"
        Exit Sub
    Else
        If (GetAttr(directoryPath) And vbDirectory) <> vbDirectory Then
            Debug.Print directoryPath & " is not a directory"
            Exit Sub
        End If
    End If

    ' We rely on the Dir() function for the search.
    ' cf https://msdn.microsoft.com/fr-fr/library/dk008ty4(v=vs.90).aspx

    ' Create the filter used with the Dir() function.
    filter = directoryPath

    If Len(pFileNameFilter) > 0 Then
        ' Add the file name filter.
        filter = filter & "*" & pFileNameFilter & "*"
    End If

    ' Start to search.
    currentFile = Dir(filter)
    Do While currentFile <> ""
        ' Use bitwise comparison to make sure currentFile is not a directory.
        If (GetAttr(directoryPath & currentFile) And vbDirectory) <> vbDirectory Then
            ' Add the entry to the list of found files.
            pFoundFiles.Add directoryPath & currentFile
        End If
        ' Get next entry.
        currentFile = Dir()
    Loop
End Sub

Please advice how to resolve!

Tinker
  • 1
  • 3
  • Did you compile it - what line is highlighted on the error message? What is `YtoFileSearch` – dbmitch May 15 '18 at 14:30
  • I tried to recreate that error using simpler code and I'm not able to do so. However, some things I am seeing: If your YtoFileSearch has an object array (.FoundFiles) you shouldn't need to pass the information to a collection. It looks like you're never manipulating the collection so it's a waste of resources. You also have some spaghetti code in your Image_Set if statement, you don't need the exit sub statement in your else statement if you move the "load first pix" block into the if statement. – Jeffrey May 15 '18 at 19:41
  • @Jeffrey `YtoFilesearch` class module _(question updated)_ has the `Foundfiles` as a collection, but then how do I refer to this collection in `Image_Set`? – Tinker May 16 '18 at 06:03
  • @dbmitch Error highlights on `pixpaths.Count` inside `Image_loop()` when the form timer is in runtime and my apologies,`YtoFilesearch` is a class module that replaces `Application.FileSearch`(depreciated in Ms Ac 2010 which i am using); I have updated the above question with the class module – Tinker May 16 '18 at 06:06
  • 1
    Check your form code for `Set pixpaths = Nothing` - or could be another form since it looks like you've made it a globally accessible collection – dbmitch May 16 '18 at 15:12
  • @dbmitch Error not yet resolved, ignore above comments! please advice. – Tinker Jul 09 '18 at 06:13

1 Answers1

0

I have to answer your comment question you had for me here. This may not solve your issue, but it may help you find it, especially if the error is from you setting pixpaths = nothing in another function as @dbmitch suggested.

You would refer to .FoundFiles in Image_Set the same way you would pixpath, the collection gets populated by the doSearch sub from the .Execute function so the following code should work the same. Also, unless you are using your arguments in another module, you may want to consider making them Private like I did here.

Private pix_path As String
Private pixnum As Integer
Private fs As YtoFileSearch

Public Sub Image_set()
    pix_path = "C:\Images"
    Set fs = New YtoFileSearch

    With fs
        .NewSearch
        .LookIn = pix_path
        .fileName = "*.jpg"

        If fs.Execute() > 0 Then
            'load first pix
            Forms!Fr_Main.imgPixHolder.Picture = .FoundFiles(1)
            pixnum = 1
        Else
            MsgBox "No files found!"
            DoCmd.OpenForm "Fr_Sketchpad"    ' If no images found in folder the set image from another form 'Sketchpad' image control
            Forms!Fr_Sketchpad.Visible = False
            Forms!Fr_Main!imgPixHolder.Picture = "" 
            'Forms!Fr_Sketchpad!Img_Std.Picture   Was getting another error here so commented this
            pixnum = 0
        End If
    End With
End Sub

Public Sub Image_loop()
    With fs
        If pixnum = .FoundFiles.Count Then
            pixnum = 1
        ElseIf pixnum <> 0 Then
            pixnum = pixnum + 1
            Forms!Fr_Main!imgPixHolder.Picture = .FoundFiles(pixnum)
        End If
    End With
End Sub
Jeffrey
  • 528
  • 5
  • 14
  • @dbmitch Thanks! That was the issue, I had not `Set pixpaths = nothing` anywhere else but the global declaration was causing the error – Tinker May 17 '18 at 13:02
  • Changing the global declaration to `Private` solved the issue Thanks! – Tinker May 17 '18 at 13:03
  • What do you mean error not yet resolved? Are you getting Error 91 on "If pixnum = pixpaths.Count Then" still? – Jeffrey Jul 09 '18 at 13:17