How insert file names into a table

I'm trying to insert the names of jpg files that reside in a particular directory into a field in a table.

I have this code in an oncurrent event of a form that the field is on but it is not working.  Can someone please help?

    'Populate field named AssetPictures with file names...
    Dim FSO As Object, fld As Object, Fil As Object
    Dim SubFolderName As String
    Dim i As Integer
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    SubFolderName = strPropertyAssetFolderName = strPropertyRoomName & Trim(Me.txtManualAssetN) & "\"
    Set fld = FSO.GetFolder(SubFolderName)
    For Each Fil In fld.Files
        i = i + 1
        If Right(Fil, 4) = ".jpg" Then
            Me.txtAssetPictures.AddItem Fil.Name
        End If
    Next Fil
    'End of code to populate field named AssetPictures with file names

Open in new window

SteveL13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You want to insert the names of ALL .jpg files in directory into a SINGLE field in a table?

Dim sFiles As String
Dim sFile As String
sFile = Dir("YourFolder", "*.jpg")

Do Until Len(sFile)=0
  sFiles = sFiles & sFile & ","
  sFile = Dir
Loop

Currentdb.Execute "UPDATE YourTable SET YourField='" & sFiles & "' WHERE SomeIDField=" & Me.YourIDField

Note that, in general, storing multiple values in a single field is a very bad idea, so if you could provide a little more context, perhaps we'd be able to direct you to a more reliable process.
0
PatHartmanCommented:
I don't think the Current event is appropriate for this task.  The current event fires every time you navigate to a new record.  The Click event of a button would be more appropriate.
0
SteveL13Author Commented:
I'm getting a type mismatch on the line:

sFile = Dir("strPropertyAssetFolderName", "*.jpg")

Here's my code now:

    Dim strClientFolderName As String
    Dim strPropertyAssetFolderName As String
    Dim strPropertyFolderName As String
    Dim strPropertyRoomName As String

    strClientFolderName = TempVars!strPicturesDataPath & "\" & Trim(Me.txtClientID) & "\"
    strPropertyFolderName = strClientFolderName & Trim(Me.txtPropertyID) & "\"
    strPropertyRoomName = strPropertyFolderName & Trim(Me.cboRoom) & "\"
    strPropertyAssetFolderName = strPropertyRoomName & Trim(Me.txtManualAssetN) & "\"
    
    Me.txtAssetFolder = strPropertyAssetFolderName
    
    'Populate field named AssetPictures with file names...
    Dim sFiles As String
    Dim sFile As String
    sFile = Dir("strPropertyAssetFolderName", "*.jpg")

    Do Until Len(sFile) = 0
    sFiles = sFiles & sFile & ","
    sFile = Dir
    Loop

    CurrentDb.Execute "UPDATE tblAssetDetail SET YourField='" & sFiles & "' WHERE ManualAssetN=" & Me.txtManualAssetN
    'End of code to populate field named AssetPictures with file names

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You don't enclose variables in quotes. This line:

sFile = Dir("strPropertyAssetFolderName", "*.jpg")

Should read:

sFile = Dir(strPropertyAssetFolderName, "*.jpg")

Assuming strPropertyAssetFolderName contains the full path to the folder where you want to search for files, of course ..
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Also, the UPDATE statement refers to "YourField", and I'm betting you don't have a Field in tblAssetDetail named "YourField" ...
0
SteveL13Author Commented:
Still getting type mismatch with:

sFile = Dir(strPropertyAssetFolderName, "*.jpg")

But I did enter the right field name
0
SteveL13Author Commented:
strPropertyAssetFolderName returns...

C\Inventory\1\1\7\1

which is the correct directory.
0
SteveL13Author Commented:
Any thoughts?
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Patience ... I have real-world work that I must attend to ...

Try this:

sFile = Dir(strPropertyAssetFolderName & "\*.jpg")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
I'm truly sorry.  I really didn't mean to be pushy.

The first time I tried it for some reason I ended up with corrupt data and can't figure out why.  Fortunately I had a back up and tried again and its been fine sine the first time.

???

--Steve
0
SteveL13Author Commented:
Thank you again.  Much appreciated.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
The first time I tried it for some reason I ended up with corrupt data and can't figure out why
Be sure you've performed maintenance on the database - make a backup, then Compact the database on a regular basis.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.