• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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

0
SteveL13
Asked:
SteveL13
  • 6
  • 5
1 Solution
 
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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
 
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now