473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

automatically update an Access table by a files listing

2
Hello,
I looked everywhere on the net but I can't to find an answer, so I come to solicit your help .
I have a directory "D: \ Books", all books are in pdf (book1.pdf, book2.pdf ... book500.pdf).
I have a very very simple database for these books (I will try to complexify this database later (with several tables, relations, etc ...)) but for now I prefer to take things in order which suits my neuron; It is in Access 2016 and contains a table "tBooks" with just 2 fields: "Num_Book (Key) and" Book_name ", the names of the records correspond perfectly to the names of the files (book1.pdf, book2.pdf ... book500 .pdf); There is a form "fBooks" based on the table "tBooks"; If I add or remove books in the "D: \ Books" directory how can I automatically update my "tLivres" table just by clicking on an "Update" command button in the associated "fBooks" form ; I just want to update the table: if there is no new book added to the directory then no new record is created in the table; If the added book does not exist in the table, then a new record is added to the table, if a book is removed from the directory then the corresponding record is removed from the table.
Thanks in advance, I have been rowing for weeks ...
Aug 1 '17 #1
2 1461
PhilOfWalton
1,430 Expert 1GB
I'm not going to give you the full answer, but this is the tricky bit. With a bit of modification, that will create your table.

Remember to have BookName as No Duplicates

If you get stuck, come back

Expand|Select|Wrap|Line Numbers
  1. Public Function ListFiles(strPath As String, Optional strFileSpec As String, _
  2.     Optional bIncludeSubfolders As Boolean)
  3.     'Debug.Print ListFiles("G:\SBEAM", "*.csv", True)
  4.     '? ListFiles("E:\Phil Data\Access\Mdb 2010", "*.PDF", True)
  5.  
  6.     On Error GoTo Err_Handler
  7.     'Purpose:   List the files in the path.
  8.     'Arguments: strPath = the path to search.
  9.     '           strFileSpec = "*.*" unless you specify differently.
  10.     '           bIncludeSubfolders: If True, returns results from subdirectories of strPath as well.
  11.     '           lst: if you pass in a list box, items are added to it. If not, files are listed to immediate window.
  12.     '               The list box must have its Row Source Type property set to Value List.
  13.     'Method:    FilDir() adds items to a collection, calling itself recursively for subfolders.
  14.     Dim colDirList As New Collection
  15.     Dim VarItem As Variant
  16.  
  17.     Call FillDir(colDirList, strPath, strFileSpec, bIncludeSubfolders)
  18.  
  19.     'Add the files to a list box if one was passed in. Otherwise list to the Immediate Window.
  20.  
  21.     For Each VarItem In colDirList
  22.         Debug.Print VarItem & " " & GetFileDate(CStr(VarItem))
  23.     Next
  24.  
  25. Exit_Handler:
  26.     Exit Function
  27.  
  28. Err_Handler:
  29.     MsgBox "Error " & Err.Number & ": " & Err.Description
  30.     Resume Exit_Handler
  31.  
  32. End Function
  33.  
And the FillDir Routine is
Expand|Select|Wrap|Line Numbers
  1. Private Function FillDir(colDirList As Collection, ByVal strFolder As String, strFileSpec As String, _
  2.     bIncludeSubfolders As Boolean)
  3.     'Debug.Print ListFiles("H:\SBEAM", "*.Csv", True)
  4.     'Build up a list of files, and then add add to this list, any additional folders
  5.  
  6.     Dim strTemp As String
  7.     Dim colFolders As New Collection
  8.     Dim vFolderName As Variant
  9.  
  10.     'Add the files to the folder.
  11.     strFolder = TrailingSlash(strFolder)
  12.     strTemp = Dir(strFolder & strFileSpec)
  13.     Do While strTemp <> vbNullString
  14.         colDirList.Add strFolder & strTemp
  15.         strTemp = Dir
  16.     Loop
  17.  
  18.     If bIncludeSubfolders Then
  19.         'Build collection of additional subfolders.
  20.         strTemp = Dir(strFolder, vbDirectory)
  21.         Do While strTemp <> vbNullString
  22.             If (strTemp <> ".") And (strTemp <> "..") Then
  23.                 If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then
  24.                     colFolders.Add strTemp
  25.                 End If
  26.             End If
  27.             strTemp = Dir
  28.         Loop
  29.         'Call function recursively for each subfolder.
  30.         For Each vFolderName In colFolders
  31.             Call FillDir(colDirList, strFolder & TrailingSlash(vFolderName), strFileSpec, True)
  32.         Next vFolderName
  33.     End If
  34.  
  35. End Function
  36.  
Phil
Aug 1 '17 #2
bip54
2
Many thanks for your very quick response .

I try today or tomorrow (because job...) and I send an S.O.S. if I'm flooded .
Aug 2 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jimmy | last post by:
I have a table that is shown below in ORIGINAL TABLE. It is common for the VEHICLE # field to have repeating vehicles (ID 3,4,5 & 8,9). Usually the Vehicle will have SHIPDATE, DATE_RECEIVED, and...
1
by: Pierre Maricq | last post by:
Hi, I am using Win2000 and Access2000. I need to build build a macro or write a VBA in Access that would screen all files contained in a directory on my C drive (files are structrured DAT...
1
by: john_liu | last post by:
How can I update an Access table based on a sheet in Excel by VBA in excel. Thanks
1
by: Esmi | last post by:
I am developing an application in Ms Excel 2003 that has a feature to update records in Ms Access. The problem is the table name I am trying to update contains a space and whenever I run the below...
3
by: pizzaface | last post by:
Hello: I'm wanting to update a Microsoft Access Table automatically with the network computer name and user name as soon as a user logs onto his/her computer. I would like this to happen with...
4
by: Kelly Warden | last post by:
I want to create a form in Access to update my table automatically with the information selected from combo boxes, lists, comments, etc. Also, I want to automate this w/ some VBA or SQL or...
2
by: simon penny | last post by:
I have a table in which I record referral records. I have a second table in which I record service records. One referral can have many service records attached. The tables are linked by referral ID -...
3
by: Daryl Austin | last post by:
I know this can be down, I'm just not sure where to begin. I have a table with fields , basically I am using Access to manage sales proposals. I export out to Excel a report query based on...
1
by: jimkay | last post by:
1. table with fields id, LastName, FirstName, Address, City, State. 2. New records added to table are place at the end in last row. 3. Is there a way after a period of time to rearrange the records...
1
by: kevinsnewmatrix | last post by:
This can update daily only. Tuesday thru Thursday are fine as Im using date()-1. but on Monday it wont work as i have to change to date-3 etc. Is there a way to use the Max date from the table Im...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.