Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 28th, 2004, 06:50 PM
Authorized User
 
Join Date: Dec 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Automate adding record and populating field

I would like to add new records to a table (one for each file name) and populate a field with each of the file names in a directory. And I want to do it with the push of a button. I have not figured out how to automate this process and would really appreciate any help you can give me.

Nashville_Bill
__________________
Nashville_Bill
 
Old January 28th, 2004, 09:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Put this code in your button's Click event procedure:
(You might want to declare the variables, which is something I haven't done in this example)
Code:
DoCmd.SetWarnings False
Set fso = CreateObject("Scripting.FileSystemObject")
Set folderObject = fso.GetFolder("C:\Some\Directory")
Set filesObject = folderObject.Files

For Each file In filesObject
DoCmd.RunSQL "INSERT INTO YourTable (YourField) VALUES ('" & file.Name & "');"
Next
DoCmd.SetWarnings True
Of course - you'll need to change the bits
C:\Some\Directory
YourTable
YourField

Steven

I am a loud man with a very large hat. This means I am in charge
 
Old January 29th, 2004, 11:57 AM
Authorized User
 
Join Date: Dec 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Steven,

Thanks for the response. I get an compile error message that says the variable file is not defined:

    Dim fso As Object
    Dim folderObject As Object
    Dim filesObject As Object
    DoCmd.SetWarnings False
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folderObject = fso.GetFolder("C:\PICS\Pic\1")
    Set filesObject = folderObject.Files

    For Each file In filesObject

Did I forget to do something?

Nashville_Bill
 
Old January 29th, 2004, 12:25 PM
Authorized User
 
Join Date: Dec 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I added a DIM for file and all is well now.

Thanks a lot!



Nashville_Bill





Similar Threads
Thread Thread Starter Forum Replies Last Post
populating a listbox with a field contains comma bjcountry Access 3 February 17th, 2016 05:07 PM
Automate Exporting data to text file if new record NC_Juggler Access VBA 1 November 21st, 2008 04:35 PM
populating drop down through text field nasirmunir Javascript How-To 5 June 30th, 2008 11:01 AM
Populating field Popper Javascript How-To 10 September 23rd, 2004 06:06 PM
populating a text field sand133 VB.NET 2002/2003 Basics 12 June 18th, 2004 08:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.