Add Date Modified to file list

J

Jon22

I am brand new to VB. I was graciously given some code to bring a list of
files in to a table and was hoping someone could tell me what and where I
would need to add something that would also bring in the date modified of
each file and put it into a field named "Date" in the same table. This is the
code I am working with:

Sub GetFilenames()
Dim db As Database
Dim rs As Recordset
Dim strMyPath As String
Dim strMyFile As String
Set db = CurrentDb
Set rs = db.OpenRecordset("tblJPGFiles")
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tbljpgfiles"
DoCmd.SetWarnings True
'type in the real path in the next line
strMyPath = "c:\*.jpg"
strMyFile = Dir(strMyPath)
Do While strMyFile <> ""
rs.AddNew
rs!FileName = strMyFile
rs.Update
'get the next file name
strMyFile = Dir
Loop
Set db = Nothing
Set rs = Nothing
End Sub
 
T

Tom Wickerath

Hi Jon,

First, a couple of comments...

1.) ...and put it into a field named "Date"
Date is considered a reserved word. You should avoid using any reserved
words when assigning names to anything in Access (fields, tables, queries,
forms, reports, macros, modules, variables, controls on forms & reports,
etc.). Access MVP Allen Browne maintains an extensive list of reserved words
here:

Problem names and reserved words in Access
http://allenbrowne.com/AppIssueBadWord.html

He also offers a free utility, called the "Database Issue Checker Utility".
This is a .mdb file, which you can use to check your existing Access
databases for reserved words.

Notes:
It covers fields, tables and queries only (not controls, variable names, etc.)
Your database should be closed when using this utility to test your database.

2.) Dim rs As Recordset
You should explicitly declare recordsets as either DAO or ADO recordset, to
avoid the possibility of run-time error 13: Type Mismatch. More information
here:

ADO and DAO Library References in Access Databases
http://www.accessmvp.com/TWickerath/articles/adodao.htm

3.) DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tbljpgfiles"
DoCmd.SetWarnings True

This line of code accomplishes the same thing, and you don't have to worry
about warnings getting turned back on, in the event that the DoCmd.RunSQL
line of code errors out:

db.Execute "DELETE * FROM tbljpgfiles", dbFailOnError

4.) You should close DAO recordsets, in addition to setting the variable to
nothing, in order to help prevent bloat:

How to prevent database bloat after you use Data Access Objects (DAO)
http://support.microsoft.com/kb/289562

With that in mind, you can use the built-in FileDateTime function to return
the date and time when a file was created or last modified. Here is an edited
version of your procedure. I used the field name "FileLastModified" to record
the date. I also defined a new string variable for the file extension, so
that I wouldn't have to later remove the *.jpg from the strMyPath value.

Sub GetFilenames()
On Error GoTo ProcError

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strMyPath As String
Dim strExt As String
Dim strMyFile As String

Set db = CurrentDb
Set rs = db.OpenRecordset("tblJPGFiles")

db.Execute "DELETE * FROM tbljpgfiles", dbFailOnError

'type in the real path in the next line
strMyPath = "c:\"
strExt = "*.jpg"

strMyFile = Dir(strMyPath & strExt)


Do While strMyFile <> ""
rs.AddNew
rs("FileName") = strMyFile
rs("FileLastModified") = FileDateTime(strMyPath & strMyFile)
rs.Update
'get the next file name
strMyFile = Dir
Loop

MsgBox rs.RecordCount & " files were logged.", _
vbInformation, "Done..."

ExitProc:
'Cleanup
If Not rs Is Nothing Then
rs.Close: Set rs = Nothing
End If
Set db = Nothing
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure GetFilenames..."
Resume ExitProc
End Sub


Notes:
The strMyPath needs to have a trailing slash included. I'll leave that as a
programming challenge to you, to check for a trailing slash and add one if it
is missing.

You might also use code to allow a user to browse to a folder, instead of
hard-coding the strMyPath value.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
J

Jon22

Thanks Tom, that's great.

Regarding your comments:
1.) Yep - new that, bad name example on my part.
2.) Not quite following that.
3.) Thank you, easier way to skin this cat.
4.) A little beyond my scope of comprehension at this time as well. But
happy to comply. The more I am looking at codes, the more it is starting to
make sense to me and I am trying things on my own but for now I am going to
just copy and paste suggested code such as yours and tweak as needed. The
things I'm trying to accomplish in my current database project require me to
look into something that I've always found daunting, but help from people
like yourself is quickly knocking down the wall.

With that in mind, I would like to tweak the DELETE query section of the
code to just delete records from the table where the value in the FileName
field starts with the current year (in "yy" format). ie, if a record has
"09-267" in the FileName value, it will get deleted but if it is "07-134" it
will remain untouched.

I was in the middle of trying to attack this myself by creating a Delete
Query with these parameters and I was then going to copy and paste the SQL
into the code I had been given earlier and see if that worked. Is there a
better way?
 
T

Tom Wickerath

Hi Jon,
2.) Not quite following that.

Have you had time to read the article I wrote, and provided a link to? To
see the run-time failure without the explicit declaration (ie. without the
"DAO." in front of the word "Recordset"), open any code module. Then click on
Tools | References. You likely have a default reference already checked for
the ADO library. Bump it's priority above (higher than) the DAO object
library. Click on OK to dismiss the references dialog. Compile your code (the
code will still compile just fine). Now try running it. If you just have "Dim
rs As Recordset", you should get run-time error 13: Type Mismatch. The point
of explicit declaration is to have code that is not dependent upon the order
of the checked refernences.


Add one string variable declaration:
Dim strSQL As String

Assign it as follows:
strSQL = "DELETE * FROM tbljpgfiles " _
& "WHERE Left$(FileName, 2) = Right$(Year(Now()), 2)"

'Debug.Print strSQL '<---Uncomment to see result printed to
' the Immediate Window (open with
<Ctrl><G>)

Change the db.Execute line of code to use this new variable:

db.Execute strSQL, dbFailOnError


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
J

Jon22

Hi Tom,

I just tried the code that you gave me and I'm getting a Runtime error. I
made a couple of small modifications but even without these mods the same
error came up.

Error is titled "Error in procedure GetFilenames / Error 3265. item not
found in this collection."

This is what I'm using exactly as it's typed (Table Name is Actually "Jpegs"
with Fields "File" and "Datemod"):

Private Sub Command4_Click()

On Error GoTo ProcError

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strMyPath As String
Dim strExt As String
Dim strMyFile As String

Set db = CurrentDb
Set rs = db.OpenRecordset("Jpegs")

db.Execute "DELETE * FROM Jpegs", dbFailOnError

'type in the real path in the next line
strMyPath = "\\Server\orion\Artwork\JPEG Files\20" & Format(Date, "yy")
& "\"
strExt = "*.jpg"

strMyFile = Dir(strMyPath & strExt)


Do While strMyFile <> ""
rs.AddNew
rs("File") = strMyFile
rs("Datemod") = FileDateTime(strMyPath & strMyFile)
rs.Update
'get the next file name
strMyFile = Dir
Loop

MsgBox rs.RecordCount & " files were logged.", _
vbInformation, "Done..."

ExitProc:
'Cleanup
If Not rs Is Nothing Then
rs.Close: Set rs = Nothing
End If
Set db = Nothing
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure GetFilenames..."
Resume ExitProc

End Sub
 
T

Tom Wickerath

Hi Jon,

Double-check the spelling of the fields in your VBA code. For example, I can
intentionally attract error 3265 if I change this line of code:

rs("FileName") = strMyFile

to this with a misspelled fieldname:

rs("FileNameZ") = strMyFile


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
H

Hans Up

Jon22 said:
I just tried the code that you gave me and I'm getting a Runtime error. I
made a couple of small modifications but even without these mods the same
error came up.

Error is titled "Error in procedure GetFilenames / Error 3265. item not
found in this collection."

Disable your subroutine's error handler by placing a single quote before
the On Error statement:
'On Error GoTo ProcError

Then run Command4_Click again. Choose debug from the error dialog. It
will show you your code with the problem line highlighted.

After you fix the code problem, remove the single quote to make your
custom error handler active again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top