Results 1 to 5 of 5
  1. #1
    kdbailey is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    228

    FSO - Finding properties of a specific "item" in a folder


    I am attempting to randomly generate a picture from a folder but am having issues with file extensions. All pictures are named integers for simplification (1-19 at the moment).

    Code:
    Private Sub Form_Load()
    
    Dim thepic As Integer, themax As Integer, fso As Object, filecount As Integer, picstr as String
    
    
    Screen.ActiveForm.Painting = False
    
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    
    folderspec = "S:\Shared\Warranty Returns\WRA Data Storage\Backups\Files\Pictures"
    
    
    If fso.FolderExists(folderspec) Then
        themax = fso.getfolder(folderspec).files.Count
        thepic = Int((themax - 1 + 1) * Rnd + 1)
        picstr = fso.getfolder(folderspec).files.Items(thepic).name
        Me.imgpic.Picture = "S:\Shared\Warranty Returns\WRA Data Storage\Backups\Files\Pictures\" & picstr
    End If
    
    
    finish:
    Screen.ActiveForm.Painting = True
    
    
    End Sub
    The bolded section is where I am getting tripped up and can't seem to find the correct code to find this property. I just want to grab the ".jpg" or ".png" portion of the name
    Last edited by June7; 06-02-2014 at 11:41 AM.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    There will be jpg and png files? Will there be more than one file with the same number name but different extension type? Like:

    17.jpg
    17.png
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    kdbailey is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    228
    No, theres only one for each integer. However, I don't want to do a trial and error try each extension. I'd rather just quick pull the extension from its properties.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I think you want something more like this:

    Code:
    Dim fso
    Dim fsoFile
    Dim fsoFolder
    Dim FolderSpec As String
    Dim theMax As Integer
    Dim thePic As Integer
    Dim iCurrPic As Integer
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    FolderSpec = CurrentProject.Path
    
    If fso.folderexists(FolderSpec) Then
        theMax = fso.getfolder(FolderSpec).files.Count
        Randomize
        thePic = Int((theMax - 1 + 1) * Rnd + 1)
    End If
    
    Set fsoFolder = fso.getfolder(FolderSpec)
    iCurrPic = 1
    
    For Each fsoFile In fsoFolder.files
        If iCurrPic = thePic Then
            Debug.Print "THE FILE YOU WANT IS " & fsoFile.Name
            GoTo CLOSESTUFF
        Else
            iCurrPic = iCurrPic + 1
        End If
    Next fsoFile
    
    CLOSESTUFF:
    Set fsoFile = Nothing
    Set fsoFolder = Nothing
    Set fso = Nothing
    you want to use RANDOMIZE before using the RND function because your database will always create the same FIRST value every time you open your database (in essence your picture will always be the same if it's only loaded when the database opens)

    Secondly you do not need to name your files with numbers, your current code is basically expecting your file names to be 17 when they are in fact something more akin to 17.jpg or 17.png

    So what this code does is generates a random number then cycles through the files in your folder until it finds that file then returns the file name. You can then test that name for the extension and open the appropriate application to display it.

  5. #5
    kdbailey is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    228
    Thanks for the help, I thought I responded last week. I did not know about the RANDOMIZE issue, but the code is not run upon database open. However, I still used it in my code now. I some reason didn't realize to just have it cycle through the items instead of the names.

    Works like a charm.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Where have my "Field Properties" gone?
    By BaldFox in forum Database Design
    Replies: 3
    Last Post: 06-19-2013, 05:49 AM
  2. Replies: 1
    Last Post: 04-12-2013, 07:56 AM
  3. oie.document.all.item("AHHHH").value = False
    By redbull in forum Programming
    Replies: 4
    Last Post: 12-13-2012, 01:54 PM
  4. Trouble Finding "Oldest" Data
    By bigdan5428 in forum Access
    Replies: 5
    Last Post: 04-26-2012, 05:25 PM
  5. Relocating objects via "Left" properties value
    By Ripcut in forum Programming
    Replies: 0
    Last Post: 08-01-2008, 06:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums