PC Review


Reply
Thread Tools Rate Thread

Appending Excel data into an Access table while inserting the file name

 
 
Greg Mathes
Guest
Posts: n/a
 
      17th Dec 2009
I have a loop in an Access 2003 database that appends records from multiple Excel files within a given directory. The name of each Excel file cycles through one variable (ImpFileName) as its records are imported, but I also need to add the Excel file name to each record. I wanted to assign the value of ImpFileName to the DefaultValue property of the FileName field in the table as part of the DoWhile loop, but something is wrong with my syntax.

My code is below, but only works if I comment out the line that assigns the DefaultValue property. Any help would be much appreciated!

Dim tdfImportedFiles As TableDef
Dim ImpFileName As String

Set tdfImportedFiles = CurrentDb.TableDefs!ImportedFiles

ImpPath = "C:\Temp\Course Evaluations\Working\Source\"
ImpFileName = Dir("C:\Temp\Course Evaluations\Working\Source\*.xls")
' sets a dynamic variable that scrolls through the list of Excel source files

Do While Len(ImpFileName) > 0
' cycle through the list until the list is empty

'tdfImportedFiles.Fields!FileName.DefaultValue = "ImpFileName"
'set the DefaultValue property for the FileName field within the ImportedFiles table to the current ImpFileName

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "ImportedFiles", "C:\Temp\Course Evaluations\Working\Source\" & ImpFileName, True
'imports all populated rows from the spreadsheet into the ImportedFiles Table

Kill "C:\Temp\Course Evaluations\Working\Source\" & ImpFileName
'deletes the source spreadsheet once it's been imported

ImpFileName = Dir
'advances to the next file in the list

Loop


Submitted via EggHeadCafe - Software Developer Portal of Choice
Quick and easy SHA1 Hash of a String
http://www.eggheadcafe.com/tutorials...sha1-hash.aspx
 
Reply With Quote
 
 
 
 
Jeff Boyce
Guest
Posts: n/a
 
      17th Dec 2009
Greg

I suspect that you don't need/want to try to assign the Default Value.
Instead, just assign the value to the field.

Regards

Jeff Boyce
Microsoft Access MVP


--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

<Greg Mathes> wrote in message news:(E-Mail Removed)...
>I have a loop in an Access 2003 database that appends records from multiple
>Excel files within a given directory. The name of each Excel file cycles
>through one variable (ImpFileName) as its records are imported, but I also
>need to add the Excel file name to each record. I wanted to assign the
>value of ImpFileName to the DefaultValue property of the FileName field in
>the table as part of the DoWhile loop, but something is wrong with my
>syntax.
>
> My code is below, but only works if I comment out the line that assigns
> the DefaultValue property. Any help would be much appreciated!
>
> Dim tdfImportedFiles As TableDef
> Dim ImpFileName As String
>
> Set tdfImportedFiles = CurrentDb.TableDefs!ImportedFiles
>
> ImpPath = "C:\Temp\Course Evaluations\Working\Source\"
> ImpFileName = Dir("C:\Temp\Course Evaluations\Working\Source\*.xls")
> ' sets a dynamic variable that scrolls through the list of Excel source
> files
>
> Do While Len(ImpFileName) > 0
> ' cycle through the list until the list is empty
>
> 'tdfImportedFiles.Fields!FileName.DefaultValue = "ImpFileName"
> 'set the DefaultValue property for the FileName field within the
> ImportedFiles table to the current ImpFileName
>
> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8,
> "ImportedFiles", "C:\Temp\Course Evaluations\Working\Source\" &
> ImpFileName, True
> 'imports all populated rows from the spreadsheet into the ImportedFiles
> Table
>
> Kill "C:\Temp\Course Evaluations\Working\Source\" & ImpFileName
> 'deletes the source spreadsheet once it's been imported
>
> ImpFileName = Dir
> 'advances to the next file in the list
>
> Loop
>
>
> Submitted via EggHeadCafe - Software Developer Portal of Choice
> Quick and easy SHA1 Hash of a String
> http://www.eggheadcafe.com/tutorials...sha1-hash.aspx



 
Reply With Quote
 
Ken Snell
Guest
Posts: n/a
 
      18th Dec 2009
I'd run an append query after each import to put the value into the
ExcelFile field for the just-imported file's records:

Dim tdfImportedFiles As TableDef
Dim ImpFileName As String

Set tdfImportedFiles = CurrentDb.TableDefs!ImportedFiles

ImpPath = "C:\Temp\Course Evaluations\Working\Source\"
ImpFileName = Dir("C:\Temp\Course Evaluations\Working\Source\*.xls")
' sets a dynamic variable that scrolls through the list of Excel source
files

Do While Len(ImpFileName) > 0
' cycle through the list until the list is empty

'tdfImportedFiles.Fields!FileName.DefaultValue = "ImpFileName"
'set the DefaultValue property for the FileName field within the
ImportedFiles table to the current ImpFileName

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8,
"ImportedFiles", "C:\Temp\Course Evaluations\Working\Source\" & ImpFileName,
True
'imports all populated rows from the spreadsheet into the ImportedFiles
Table

'update FileName field with just-imported filename, where the FileName
field contains a NULl value (which will be true for the just-imported
records only)
CurrentDB.Execute "UPDATE ImportedFiles SET FileName = '" & ImpFileName &
"' WHERE FileName IS NULL"

Kill "C:\Temp\Course Evaluations\Working\Source\" & ImpFileName
'deletes the source spreadsheet once it's been imported

ImpFileName = Dir
'advances to the next file in the list

Loop

--

Ken Snell
http://www.accessmvp.com/KDSnell/



<Greg Mathes> wrote in message news:(E-Mail Removed)...
>I have a loop in an Access 2003 database that appends records from multiple
>Excel files within a given directory. The name of each Excel file cycles
>through one variable (ImpFileName) as its records are imported, but I also
>need to add the Excel file name to each record. I wanted to assign the
>value of ImpFileName to the DefaultValue property of the FileName field in
>the table as part of the DoWhile loop, but something is wrong with my
>syntax.
>
> My code is below, but only works if I comment out the line that assigns
> the DefaultValue property. Any help would be much appreciated!
>
> Dim tdfImportedFiles As TableDef
> Dim ImpFileName As String
>
> Set tdfImportedFiles = CurrentDb.TableDefs!ImportedFiles
>
> ImpPath = "C:\Temp\Course Evaluations\Working\Source\"
> ImpFileName = Dir("C:\Temp\Course Evaluations\Working\Source\*.xls")
> ' sets a dynamic variable that scrolls through the list of Excel source
> files
>
> Do While Len(ImpFileName) > 0
> ' cycle through the list until the list is empty
>
> 'tdfImportedFiles.Fields!FileName.DefaultValue = "ImpFileName"
> 'set the DefaultValue property for the FileName field within the
> ImportedFiles table to the current ImpFileName
>
> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8,
> "ImportedFiles", "C:\Temp\Course Evaluations\Working\Source\" &
> ImpFileName, True
> 'imports all populated rows from the spreadsheet into the ImportedFiles
> Table
>
> Kill "C:\Temp\Course Evaluations\Working\Source\" & ImpFileName
> 'deletes the source spreadsheet once it's been imported
>
> ImpFileName = Dir
> 'advances to the next file in the list
>
> Loop
>
>
> Submitted via EggHeadCafe - Software Developer Portal of Choice
> Quick and easy SHA1 Hash of a String
> http://www.eggheadcafe.com/tutorials...sha1-hash.aspx



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting Excel Data Range into Access Table =?Utf-8?B?TWlrZUVsZWN0cmljVXRpbGl0eQ==?= Microsoft Excel Programming 2 14th Feb 2007 06:07 PM
appending data to ms access table using ms excel MA Microsoft Excel Misc 1 2nd Sep 2004 03:43 AM
Excel – Macro Problem – Inserting, Appending Data, Deleting Aussiexcel Microsoft Excel Programming 0 25th Aug 2004 07:48 AM
inserting a picture from an excel file into an access table gusz1@freemail.hu Microsoft Excel Programming 0 24th Jun 2004 10:05 AM
Appending Excel file to an Access table Diana Microsoft Excel Programming 1 10th Sep 2003 08:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:59 PM.