PC Review


Reply
Thread Tools Rate Thread

append file name to a table

 
 
GEORGIA
Guest
Posts: n/a
 
      24th Sep 2009
Hi I have this code that will import multiple excle files into one table:

Private Sub Command3_Click()
Dim strFile As String
Dim strFolder As String

strFolder = Me.txtfilename.Value
strFile = Dir$(strFolder & "*.xls")
Do While Len(strFile) > 0

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTest",
strFolder & strFile, True

strFile = Dir$()
Loop

MsgBox "Finished", vbOKOnly

End Sub

There are over 50 excel files that are being imported. I have created a
column called "File_Name" in a table and would like to update it with file
name each time the excel is being imported so that I can refer back to where
the data came from. How would I go about doing so?

Thank you for help!!
 
Reply With Quote
 
 
 
 
Ken Snell MVP
Guest
Posts: n/a
 
      24th Sep 2009
You'll need a few extra steps.

One, use an interim table to receive the imported data from one spreadsheet
file.

Two, run a delete query on that interim table to delete all records from it
before you do the TransferSpreadsheet action for the file.

Three, do the TransferSpreadsheet action to the interim table.

Four, run an append query to copy the data from the interim table to the
permanent table, and add a calculated field to the append query that
provides the name of the file from which the data came (this can be done by
building the SQL statement dynamically in code for the append query, and
then executing the append query's SQL statement via code).

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


"GEORGIA" <(E-Mail Removed)> wrote in message
news72A4305-72C1-4D48-946B-(E-Mail Removed)...
> Hi I have this code that will import multiple excle files into one table:
>
> Private Sub Command3_Click()
> Dim strFile As String
> Dim strFolder As String
>
> strFolder = Me.txtfilename.Value
> strFile = Dir$(strFolder & "*.xls")
> Do While Len(strFile) > 0
>
> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTest",
> strFolder & strFile, True
>
> strFile = Dir$()
> Loop
>
> MsgBox "Finished", vbOKOnly
>
> End Sub
>
> There are over 50 excel files that are being imported. I have created a
> column called "File_Name" in a table and would like to update it with file
> name each time the excel is being imported so that I can refer back to
> where
> the data came from. How would I go about doing so?
>
> Thank you for help!!



 
Reply With Quote
 
GEORGIA
Guest
Posts: n/a
 
      24th Sep 2009
Hi,
on # 4, how would i do a calculated field to show the file name?
Thanks


"Ken Snell MVP" wrote:

> You'll need a few extra steps.
>
> One, use an interim table to receive the imported data from one spreadsheet
> file.
>
> Two, run a delete query on that interim table to delete all records from it
> before you do the TransferSpreadsheet action for the file.
>
> Three, do the TransferSpreadsheet action to the interim table.
>
> Four, run an append query to copy the data from the interim table to the
> permanent table, and add a calculated field to the append query that
> provides the name of the file from which the data came (this can be done by
> building the SQL statement dynamically in code for the append query, and
> then executing the append query's SQL statement via code).
>
> --
>
> Ken Snell
> <MS ACCESS MVP>
> http://www.accessmvp.com/KDSnell/
>
>
> "GEORGIA" <(E-Mail Removed)> wrote in message
> news72A4305-72C1-4D48-946B-(E-Mail Removed)...
> > Hi I have this code that will import multiple excle files into one table:
> >
> > Private Sub Command3_Click()
> > Dim strFile As String
> > Dim strFolder As String
> >
> > strFolder = Me.txtfilename.Value
> > strFile = Dir$(strFolder & "*.xls")
> > Do While Len(strFile) > 0
> >
> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTest",
> > strFolder & strFile, True
> >
> > strFile = Dir$()
> > Loop
> >
> > MsgBox "Finished", vbOKOnly
> >
> > End Sub
> >
> > There are over 50 excel files that are being imported. I have created a
> > column called "File_Name" in a table and would like to update it with file
> > name each time the excel is being imported so that I can refer back to
> > where
> > the data came from. How would I go about doing so?
> >
> > Thank you for help!!

>
>
>

 
Reply With Quote
 
Ken Snell MVP
Guest
Posts: n/a
 
      24th Sep 2009
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTest",
strFolder & strFile, True

Dim strSQL As String
Dim dbs As DAO.Database
Set dbs = CurrentDb
strSQL = "INSERT INTO [RealTableName] ( " & _
"FieldName1, FieldName2, FieldName3, FileNameField )" & _
" SELECT FieldName1, FieldName2, FieldName3, " & _
"'" & strFile & "' FROM [InterimTableName]"
dbs.Execute strSQL, dbFailOnError
Set dbs = Nothing

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/



"GEORGIA" <(E-Mail Removed)> wrote in message
news:80F626DF-4313-4697-BE99-(E-Mail Removed)...
> Hi,
> on # 4, how would i do a calculated field to show the file name?
> Thanks
>
>
> "Ken Snell MVP" wrote:
>
>> You'll need a few extra steps.
>>
>> One, use an interim table to receive the imported data from one
>> spreadsheet
>> file.
>>
>> Two, run a delete query on that interim table to delete all records from
>> it
>> before you do the TransferSpreadsheet action for the file.
>>
>> Three, do the TransferSpreadsheet action to the interim table.
>>
>> Four, run an append query to copy the data from the interim table to the
>> permanent table, and add a calculated field to the append query that
>> provides the name of the file from which the data came (this can be done
>> by
>> building the SQL statement dynamically in code for the append query, and
>> then executing the append query's SQL statement via code).
>>
>> --
>>
>> Ken Snell
>> <MS ACCESS MVP>
>> http://www.accessmvp.com/KDSnell/
>>
>>
>> "GEORGIA" <(E-Mail Removed)> wrote in message
>> news72A4305-72C1-4D48-946B-(E-Mail Removed)...
>> > Hi I have this code that will import multiple excle files into one
>> > table:
>> >
>> > Private Sub Command3_Click()
>> > Dim strFile As String
>> > Dim strFolder As String
>> >
>> > strFolder = Me.txtfilename.Value
>> > strFile = Dir$(strFolder & "*.xls")
>> > Do While Len(strFile) > 0
>> >
>> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
>> > "tblTest",
>> > strFolder & strFile, True
>> >
>> > strFile = Dir$()
>> > Loop
>> >
>> > MsgBox "Finished", vbOKOnly
>> >
>> > End Sub
>> >
>> > There are over 50 excel files that are being imported. I have created
>> > a
>> > column called "File_Name" in a table and would like to update it with
>> > file
>> > name each time the excel is being imported so that I can refer back to
>> > where
>> > the data came from. How would I go about doing so?
>> >
>> > Thank you for help!!

>>
>>
>>



 
Reply With Quote
 
GEORGIA
Guest
Posts: n/a
 
      24th Sep 2009
i am getting an error message (too few parameters)

Dim strFile As String
Dim strFolder As String
Dim strSQL As String
Dim dbs As DAO.Database
strFolder = Me.txtfilename.Value
strFile = Dir$(strFolder & "*.xls")
Do While Len(strFile) > 0

DoCmd.RunSQL "DELETE tblTemp.* FROM tblTemp;"


DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTemp",
strFolder & strFile, True


Set dbs = CurrentDb

strSQL = "INSERT INTO [Accountpay] ( " & _
"FieldName1, FieldName2, FieldName3, FileNameField )" & _
" SELECT FieldName1, FieldName2, FieldName3, " & _
"'" & strFile & "' FROM [tblTemp]"

dbs.Execute strSQL, dbFailOnError
Set dbs = Nothing




strFile = Dir$()
Loop

"Ken Snell MVP" wrote:

> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTest",
> strFolder & strFile, True
>
> Dim strSQL As String
> Dim dbs As DAO.Database
> Set dbs = CurrentDb
> strSQL = "INSERT INTO [RealTableName] ( " & _
> "FieldName1, FieldName2, FieldName3, FileNameField )" & _
> " SELECT FieldName1, FieldName2, FieldName3, " & _
> "'" & strFile & "' FROM [InterimTableName]"
> dbs.Execute strSQL, dbFailOnError
> Set dbs = Nothing
>
> --
>
> Ken Snell
> <MS ACCESS MVP>
> http://www.accessmvp.com/KDSnell/
>
>
>
> "GEORGIA" <(E-Mail Removed)> wrote in message
> news:80F626DF-4313-4697-BE99-(E-Mail Removed)...
> > Hi,
> > on # 4, how would i do a calculated field to show the file name?
> > Thanks
> >
> >
> > "Ken Snell MVP" wrote:
> >
> >> You'll need a few extra steps.
> >>
> >> One, use an interim table to receive the imported data from one
> >> spreadsheet
> >> file.
> >>
> >> Two, run a delete query on that interim table to delete all records from
> >> it
> >> before you do the TransferSpreadsheet action for the file.
> >>
> >> Three, do the TransferSpreadsheet action to the interim table.
> >>
> >> Four, run an append query to copy the data from the interim table to the
> >> permanent table, and add a calculated field to the append query that
> >> provides the name of the file from which the data came (this can be done
> >> by
> >> building the SQL statement dynamically in code for the append query, and
> >> then executing the append query's SQL statement via code).
> >>
> >> --
> >>
> >> Ken Snell
> >> <MS ACCESS MVP>
> >> http://www.accessmvp.com/KDSnell/
> >>
> >>
> >> "GEORGIA" <(E-Mail Removed)> wrote in message
> >> news72A4305-72C1-4D48-946B-(E-Mail Removed)...
> >> > Hi I have this code that will import multiple excle files into one
> >> > table:
> >> >
> >> > Private Sub Command3_Click()
> >> > Dim strFile As String
> >> > Dim strFolder As String
> >> >
> >> > strFolder = Me.txtfilename.Value
> >> > strFile = Dir$(strFolder & "*.xls")
> >> > Do While Len(strFile) > 0
> >> >
> >> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
> >> > "tblTest",
> >> > strFolder & strFile, True
> >> >
> >> > strFile = Dir$()
> >> > Loop
> >> >
> >> > MsgBox "Finished", vbOKOnly
> >> >
> >> > End Sub
> >> >
> >> > There are over 50 excel files that are being imported. I have created
> >> > a
> >> > column called "File_Name" in a table and would like to update it with
> >> > file
> >> > name each time the excel is being imported so that I can refer back to
> >> > where
> >> > the data came from. How would I go about doing so?
> >> >
> >> > Thank you for help!!
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Ken Snell MVP
Guest
Posts: n/a
 
      24th Sep 2009
I used generic field names in the SQL statement. You must replace the
generic field names with the real field names -- and you must include all
the fields that are in the temporary and permanent table.

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


"GEORGIA" <(E-Mail Removed)> wrote in message
news:1C7EE6E7-88C4-4A3B-99B2-(E-Mail Removed)...
>i am getting an error message (too few parameters)
>
> Dim strFile As String
> Dim strFolder As String
> Dim strSQL As String
> Dim dbs As DAO.Database
> strFolder = Me.txtfilename.Value
> strFile = Dir$(strFolder & "*.xls")
> Do While Len(strFile) > 0
>
> DoCmd.RunSQL "DELETE tblTemp.* FROM tblTemp;"
>
>
> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTemp",
> strFolder & strFile, True
>
>
> Set dbs = CurrentDb
>
> strSQL = "INSERT INTO [Accountpay] ( " & _
> "FieldName1, FieldName2, FieldName3, FileNameField )" & _
> " SELECT FieldName1, FieldName2, FieldName3, " & _
> "'" & strFile & "' FROM [tblTemp]"
>
> dbs.Execute strSQL, dbFailOnError
> Set dbs = Nothing
>
>
>
>
> strFile = Dir$()
> Loop
>
> "Ken Snell MVP" wrote:
>
>> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
>> "tblTest",
>> strFolder & strFile, True
>>
>> Dim strSQL As String
>> Dim dbs As DAO.Database
>> Set dbs = CurrentDb
>> strSQL = "INSERT INTO [RealTableName] ( " & _
>> "FieldName1, FieldName2, FieldName3, FileNameField )" & _
>> " SELECT FieldName1, FieldName2, FieldName3, " & _
>> "'" & strFile & "' FROM [InterimTableName]"
>> dbs.Execute strSQL, dbFailOnError
>> Set dbs = Nothing
>>
>> --
>>
>> Ken Snell
>> <MS ACCESS MVP>
>> http://www.accessmvp.com/KDSnell/
>>
>>
>>
>> "GEORGIA" <(E-Mail Removed)> wrote in message
>> news:80F626DF-4313-4697-BE99-(E-Mail Removed)...
>> > Hi,
>> > on # 4, how would i do a calculated field to show the file name?
>> > Thanks
>> >
>> >
>> > "Ken Snell MVP" wrote:
>> >
>> >> You'll need a few extra steps.
>> >>
>> >> One, use an interim table to receive the imported data from one
>> >> spreadsheet
>> >> file.
>> >>
>> >> Two, run a delete query on that interim table to delete all records
>> >> from
>> >> it
>> >> before you do the TransferSpreadsheet action for the file.
>> >>
>> >> Three, do the TransferSpreadsheet action to the interim table.
>> >>
>> >> Four, run an append query to copy the data from the interim table to
>> >> the
>> >> permanent table, and add a calculated field to the append query that
>> >> provides the name of the file from which the data came (this can be
>> >> done
>> >> by
>> >> building the SQL statement dynamically in code for the append query,
>> >> and
>> >> then executing the append query's SQL statement via code).
>> >>
>> >> --
>> >>
>> >> Ken Snell
>> >> <MS ACCESS MVP>
>> >> http://www.accessmvp.com/KDSnell/
>> >>
>> >>
>> >> "GEORGIA" <(E-Mail Removed)> wrote in message
>> >> news72A4305-72C1-4D48-946B-(E-Mail Removed)...
>> >> > Hi I have this code that will import multiple excle files into one
>> >> > table:
>> >> >
>> >> > Private Sub Command3_Click()
>> >> > Dim strFile As String
>> >> > Dim strFolder As String
>> >> >
>> >> > strFolder = Me.txtfilename.Value
>> >> > strFile = Dir$(strFolder & "*.xls")
>> >> > Do While Len(strFile) > 0
>> >> >
>> >> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
>> >> > "tblTest",
>> >> > strFolder & strFile, True
>> >> >
>> >> > strFile = Dir$()
>> >> > Loop
>> >> >
>> >> > MsgBox "Finished", vbOKOnly
>> >> >
>> >> > End Sub
>> >> >
>> >> > There are over 50 excel files that are being imported. I have
>> >> > created
>> >> > a
>> >> > column called "File_Name" in a table and would like to update it
>> >> > with
>> >> > file
>> >> > name each time the excel is being imported so that I can refer back
>> >> > to
>> >> > where
>> >> > the data came from. How would I go about doing so?
>> >> >
>> >> > Thank you for help!!
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
GEORGIA
Guest
Posts: n/a
 
      25th Sep 2009
now I am getting a sytax error on Insert INTO statement..:

Private Sub Command3_Click()
Dim strFile As String
Dim strFolder As String

strFolder = Me.txtfilename.Value
strFile = Dir$(strFolder & "*.xls")
Do While Len(strFile) > 0


DoCmd.OpenQuery "query1", acViewNormal, acEdit
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTemp",
strFolder & strFile, True

Dim strSQL As String
Dim dbs As DAO.Database
Set dbs = CurrentDb

strSQL = "INSERT INTO [AccountPay] ( " & _
"ChkRoutingNum, ChkAccountNum, Points, Escrow, Cost, Maintenance, Other,
VendorId, VendorName, ContractId, Description, CITAppNo, CustName, InvoiceNo
)" & _
"SELECT ChkRoutingNum, ChkAccountNum, Points, Escrow, Cost, Maintenance,
Other, VendorId, VendorName, ContractId, Description, CITAppNo, CustName,
InvoiceNo)" & _
"'" & strFile & "' FROM [tblTemp]"

dbs.Execute strSQL, dbFailOnError


Set dbs = Nothing


strFile = Dir$()

Loop


End Sub

I can't figured out. Help!
Thanks!

"Ken Snell MVP" wrote:

> I used generic field names in the SQL statement. You must replace the
> generic field names with the real field names -- and you must include all
> the fields that are in the temporary and permanent table.
>
> --
>
> Ken Snell
> <MS ACCESS MVP>
> http://www.accessmvp.com/KDSnell/
>
>
> "GEORGIA" <(E-Mail Removed)> wrote in message
> news:1C7EE6E7-88C4-4A3B-99B2-(E-Mail Removed)...
> >i am getting an error message (too few parameters)
> >
> > Dim strFile As String
> > Dim strFolder As String
> > Dim strSQL As String
> > Dim dbs As DAO.Database
> > strFolder = Me.txtfilename.Value
> > strFile = Dir$(strFolder & "*.xls")
> > Do While Len(strFile) > 0
> >
> > DoCmd.RunSQL "DELETE tblTemp.* FROM tblTemp;"
> >
> >
> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTemp",
> > strFolder & strFile, True
> >
> >
> > Set dbs = CurrentDb
> >
> > strSQL = "INSERT INTO [Accountpay] ( " & _
> > "FieldName1, FieldName2, FieldName3, FileNameField )" & _
> > " SELECT FieldName1, FieldName2, FieldName3, " & _
> > "'" & strFile & "' FROM [tblTemp]"
> >
> > dbs.Execute strSQL, dbFailOnError
> > Set dbs = Nothing
> >
> >
> >
> >
> > strFile = Dir$()
> > Loop
> >
> > "Ken Snell MVP" wrote:
> >
> >> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
> >> "tblTest",
> >> strFolder & strFile, True
> >>
> >> Dim strSQL As String
> >> Dim dbs As DAO.Database
> >> Set dbs = CurrentDb
> >> strSQL = "INSERT INTO [RealTableName] ( " & _
> >> "FieldName1, FieldName2, FieldName3, FileNameField )" & _
> >> " SELECT FieldName1, FieldName2, FieldName3, " & _
> >> "'" & strFile & "' FROM [InterimTableName]"
> >> dbs.Execute strSQL, dbFailOnError
> >> Set dbs = Nothing
> >>
> >> --
> >>
> >> Ken Snell
> >> <MS ACCESS MVP>
> >> http://www.accessmvp.com/KDSnell/
> >>
> >>
> >>
> >> "GEORGIA" <(E-Mail Removed)> wrote in message
> >> news:80F626DF-4313-4697-BE99-(E-Mail Removed)...
> >> > Hi,
> >> > on # 4, how would i do a calculated field to show the file name?
> >> > Thanks
> >> >
> >> >
> >> > "Ken Snell MVP" wrote:
> >> >
> >> >> You'll need a few extra steps.
> >> >>
> >> >> One, use an interim table to receive the imported data from one
> >> >> spreadsheet
> >> >> file.
> >> >>
> >> >> Two, run a delete query on that interim table to delete all records
> >> >> from
> >> >> it
> >> >> before you do the TransferSpreadsheet action for the file.
> >> >>
> >> >> Three, do the TransferSpreadsheet action to the interim table.
> >> >>
> >> >> Four, run an append query to copy the data from the interim table to
> >> >> the
> >> >> permanent table, and add a calculated field to the append query that
> >> >> provides the name of the file from which the data came (this can be
> >> >> done
> >> >> by
> >> >> building the SQL statement dynamically in code for the append query,
> >> >> and
> >> >> then executing the append query's SQL statement via code).
> >> >>
> >> >> --
> >> >>
> >> >> Ken Snell
> >> >> <MS ACCESS MVP>
> >> >> http://www.accessmvp.com/KDSnell/
> >> >>
> >> >>
> >> >> "GEORGIA" <(E-Mail Removed)> wrote in message
> >> >> news72A4305-72C1-4D48-946B-(E-Mail Removed)...
> >> >> > Hi I have this code that will import multiple excle files into one
> >> >> > table:
> >> >> >
> >> >> > Private Sub Command3_Click()
> >> >> > Dim strFile As String
> >> >> > Dim strFolder As String
> >> >> >
> >> >> > strFolder = Me.txtfilename.Value
> >> >> > strFile = Dir$(strFolder & "*.xls")
> >> >> > Do While Len(strFile) > 0
> >> >> >
> >> >> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
> >> >> > "tblTest",
> >> >> > strFolder & strFile, True
> >> >> >
> >> >> > strFile = Dir$()
> >> >> > Loop
> >> >> >
> >> >> > MsgBox "Finished", vbOKOnly
> >> >> >
> >> >> > End Sub
> >> >> >
> >> >> > There are over 50 excel files that are being imported. I have
> >> >> > created
> >> >> > a
> >> >> > column called "File_Name" in a table and would like to update it
> >> >> > with
> >> >> > file
> >> >> > name each time the excel is being imported so that I can refer back
> >> >> > to
> >> >> > where
> >> >> > the data came from. How would I go about doing so?
> >> >> >
> >> >> > Thank you for help!!
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Ken Snell MVP
Guest
Posts: n/a
 
      25th Sep 2009
You have syntax errors in the way you've formatted the statement.

When I use your strSQL = step in the VBA Immediate Window, and use
"test.xls" for the strFile variable, I get this:

INSERT INTO [AccountPay] ( ChkRoutingNum, ChkAccountNum, Points, Escrow,
Cost, Maintenance, Other, VendorId, VendorName, ContractId, Description,
CITAppNo, CustName, InvoiceNo )SELECT ChkRoutingNum, ChkAccountNum, Points,
Escrow, Cost, Maintenance, Other, VendorId, VendorName, ContractId,
Description, CITAppNo, CustName, InvoiceNo)'test.xls' FROM [tblTemp]


See the )SELECT in the middle? Needs to have a space between ) and SELECT.

Also see how you've got the ) to the left of 'test.xls'? It needs to be ,
'test.xls') instead.

So try this:

strSQL = "INSERT INTO [AccountPay] ( " & _
"ChkRoutingNum, ChkAccountNum, Points, Escrow, " & _
"Cost, Maintenance, Other, VendorId, VendorName, ContractId, " & _
"Description, CITAppNo, CustName, InvoiceNo ) " & _
"SELECT ChkRoutingNum, ChkAccountNum, Points, Escrow, " & _
"Cost, Maintenance, Other, VendorId, VendorName, ContractId, " & _
"Description, CITAppNo, CustName, InvoiceNo, '" & _
strFile & "' FROM [tblTemp]"

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/



"GEORGIA" <(E-Mail Removed)> wrote in message
news:9714A9E9-620F-4253-869A-(E-Mail Removed)...
> now I am getting a sytax error on Insert INTO statement..:
>
> Private Sub Command3_Click()
> Dim strFile As String
> Dim strFolder As String
>
> strFolder = Me.txtfilename.Value
> strFile = Dir$(strFolder & "*.xls")
> Do While Len(strFile) > 0
>
>
> DoCmd.OpenQuery "query1", acViewNormal, acEdit
> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTemp",
> strFolder & strFile, True
>
> Dim strSQL As String
> Dim dbs As DAO.Database
> Set dbs = CurrentDb
>
> strSQL = "INSERT INTO [AccountPay] ( " & _
> "ChkRoutingNum, ChkAccountNum, Points, Escrow, Cost, Maintenance, Other,
> VendorId, VendorName, ContractId, Description, CITAppNo, CustName,
> InvoiceNo
> )" & _
> "SELECT ChkRoutingNum, ChkAccountNum, Points, Escrow, Cost, Maintenance,
> Other, VendorId, VendorName, ContractId, Description, CITAppNo, CustName,
> InvoiceNo)" & _
> "'" & strFile & "' FROM [tblTemp]"
>
> dbs.Execute strSQL, dbFailOnError
>
>
> Set dbs = Nothing
>
>
> strFile = Dir$()
>
> Loop
>
>
> End Sub
>
> I can't figured out. Help!
> Thanks!
>
> "Ken Snell MVP" wrote:
>
>> I used generic field names in the SQL statement. You must replace the
>> generic field names with the real field names -- and you must include all
>> the fields that are in the temporary and permanent table.
>>
>> --
>>
>> Ken Snell
>> <MS ACCESS MVP>
>> http://www.accessmvp.com/KDSnell/
>>
>>
>> "GEORGIA" <(E-Mail Removed)> wrote in message
>> news:1C7EE6E7-88C4-4A3B-99B2-(E-Mail Removed)...
>> >i am getting an error message (too few parameters)
>> >
>> > Dim strFile As String
>> > Dim strFolder As String
>> > Dim strSQL As String
>> > Dim dbs As DAO.Database
>> > strFolder = Me.txtfilename.Value
>> > strFile = Dir$(strFolder & "*.xls")
>> > Do While Len(strFile) > 0
>> >
>> > DoCmd.RunSQL "DELETE tblTemp.* FROM tblTemp;"
>> >
>> >
>> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
>> > "tblTemp",
>> > strFolder & strFile, True
>> >
>> >
>> > Set dbs = CurrentDb
>> >
>> > strSQL = "INSERT INTO [Accountpay] ( " & _
>> > "FieldName1, FieldName2, FieldName3, FileNameField )" & _
>> > " SELECT FieldName1, FieldName2, FieldName3, " & _
>> > "'" & strFile & "' FROM [tblTemp]"
>> >
>> > dbs.Execute strSQL, dbFailOnError
>> > Set dbs = Nothing
>> >
>> >
>> >
>> >
>> > strFile = Dir$()
>> > Loop
>> >
>> > "Ken Snell MVP" wrote:
>> >
>> >> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
>> >> "tblTest",
>> >> strFolder & strFile, True
>> >>
>> >> Dim strSQL As String
>> >> Dim dbs As DAO.Database
>> >> Set dbs = CurrentDb
>> >> strSQL = "INSERT INTO [RealTableName] ( " & _
>> >> "FieldName1, FieldName2, FieldName3, FileNameField )" & _
>> >> " SELECT FieldName1, FieldName2, FieldName3, " & _
>> >> "'" & strFile & "' FROM [InterimTableName]"
>> >> dbs.Execute strSQL, dbFailOnError
>> >> Set dbs = Nothing
>> >>
>> >> --
>> >>
>> >> Ken Snell
>> >> <MS ACCESS MVP>
>> >> http://www.accessmvp.com/KDSnell/
>> >>
>> >>
>> >>
>> >> "GEORGIA" <(E-Mail Removed)> wrote in message
>> >> news:80F626DF-4313-4697-BE99-(E-Mail Removed)...
>> >> > Hi,
>> >> > on # 4, how would i do a calculated field to show the file name?
>> >> > Thanks
>> >> >
>> >> >
>> >> > "Ken Snell MVP" wrote:
>> >> >
>> >> >> You'll need a few extra steps.
>> >> >>
>> >> >> One, use an interim table to receive the imported data from one
>> >> >> spreadsheet
>> >> >> file.
>> >> >>
>> >> >> Two, run a delete query on that interim table to delete all records
>> >> >> from
>> >> >> it
>> >> >> before you do the TransferSpreadsheet action for the file.
>> >> >>
>> >> >> Three, do the TransferSpreadsheet action to the interim table.
>> >> >>
>> >> >> Four, run an append query to copy the data from the interim table
>> >> >> to
>> >> >> the
>> >> >> permanent table, and add a calculated field to the append query
>> >> >> that
>> >> >> provides the name of the file from which the data came (this can be
>> >> >> done
>> >> >> by
>> >> >> building the SQL statement dynamically in code for the append
>> >> >> query,
>> >> >> and
>> >> >> then executing the append query's SQL statement via code).
>> >> >>
>> >> >> --
>> >> >>
>> >> >> Ken Snell
>> >> >> <MS ACCESS MVP>
>> >> >> http://www.accessmvp.com/KDSnell/
>> >> >>
>> >> >>
>> >> >> "GEORGIA" <(E-Mail Removed)> wrote in message
>> >> >> news72A4305-72C1-4D48-946B-(E-Mail Removed)...
>> >> >> > Hi I have this code that will import multiple excle files into
>> >> >> > one
>> >> >> > table:
>> >> >> >
>> >> >> > Private Sub Command3_Click()
>> >> >> > Dim strFile As String
>> >> >> > Dim strFolder As String
>> >> >> >
>> >> >> > strFolder = Me.txtfilename.Value
>> >> >> > strFile = Dir$(strFolder & "*.xls")
>> >> >> > Do While Len(strFile) > 0
>> >> >> >
>> >> >> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
>> >> >> > "tblTest",
>> >> >> > strFolder & strFile, True
>> >> >> >
>> >> >> > strFile = Dir$()
>> >> >> > Loop
>> >> >> >
>> >> >> > MsgBox "Finished", vbOKOnly
>> >> >> >
>> >> >> > End Sub
>> >> >> >
>> >> >> > There are over 50 excel files that are being imported. I have
>> >> >> > created
>> >> >> > a
>> >> >> > column called "File_Name" in a table and would like to update it
>> >> >> > with
>> >> >> > file
>> >> >> > name each time the excel is being imported so that I can refer
>> >> >> > back
>> >> >> > to
>> >> >> > where
>> >> >> > the data came from. How would I go about doing so?
>> >> >> >
>> >> >> > Thank you for help!!
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
GEORGIA
Guest
Posts: n/a
 
      25th Sep 2009

oh I GOT IT! THANK YOU SO MUCH!
"Ken Snell MVP" wrote:

> You have syntax errors in the way you've formatted the statement.
>
> When I use your strSQL = step in the VBA Immediate Window, and use
> "test.xls" for the strFile variable, I get this:
>
> INSERT INTO [AccountPay] ( ChkRoutingNum, ChkAccountNum, Points, Escrow,
> Cost, Maintenance, Other, VendorId, VendorName, ContractId, Description,
> CITAppNo, CustName, InvoiceNo )SELECT ChkRoutingNum, ChkAccountNum, Points,
> Escrow, Cost, Maintenance, Other, VendorId, VendorName, ContractId,
> Description, CITAppNo, CustName, InvoiceNo)'test.xls' FROM [tblTemp]
>
>
> See the )SELECT in the middle? Needs to have a space between ) and SELECT.
>
> Also see how you've got the ) to the left of 'test.xls'? It needs to be ,
> 'test.xls') instead.
>
> So try this:
>
> strSQL = "INSERT INTO [AccountPay] ( " & _
> "ChkRoutingNum, ChkAccountNum, Points, Escrow, " & _
> "Cost, Maintenance, Other, VendorId, VendorName, ContractId, " & _
> "Description, CITAppNo, CustName, InvoiceNo ) " & _
> "SELECT ChkRoutingNum, ChkAccountNum, Points, Escrow, " & _
> "Cost, Maintenance, Other, VendorId, VendorName, ContractId, " & _
> "Description, CITAppNo, CustName, InvoiceNo, '" & _
> strFile & "' FROM [tblTemp]"
>
> --
>
> Ken Snell
> <MS ACCESS MVP>
> http://www.accessmvp.com/KDSnell/
>
>
>
> "GEORGIA" <(E-Mail Removed)> wrote in message
> news:9714A9E9-620F-4253-869A-(E-Mail Removed)...
> > now I am getting a sytax error on Insert INTO statement..:
> >
> > Private Sub Command3_Click()
> > Dim strFile As String
> > Dim strFolder As String
> >
> > strFolder = Me.txtfilename.Value
> > strFile = Dir$(strFolder & "*.xls")
> > Do While Len(strFile) > 0
> >
> >
> > DoCmd.OpenQuery "query1", acViewNormal, acEdit
> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTemp",
> > strFolder & strFile, True
> >
> > Dim strSQL As String
> > Dim dbs As DAO.Database
> > Set dbs = CurrentDb
> >
> > strSQL = "INSERT INTO [AccountPay] ( " & _
> > "ChkRoutingNum, ChkAccountNum, Points, Escrow, Cost, Maintenance, Other,
> > VendorId, VendorName, ContractId, Description, CITAppNo, CustName,
> > InvoiceNo
> > )" & _
> > "SELECT ChkRoutingNum, ChkAccountNum, Points, Escrow, Cost, Maintenance,
> > Other, VendorId, VendorName, ContractId, Description, CITAppNo, CustName,
> > InvoiceNo)" & _
> > "'" & strFile & "' FROM [tblTemp]"
> >
> > dbs.Execute strSQL, dbFailOnError
> >
> >
> > Set dbs = Nothing
> >
> >
> > strFile = Dir$()
> >
> > Loop
> >
> >
> > End Sub
> >
> > I can't figured out. Help!
> > Thanks!
> >
> > "Ken Snell MVP" wrote:
> >
> >> I used generic field names in the SQL statement. You must replace the
> >> generic field names with the real field names -- and you must include all
> >> the fields that are in the temporary and permanent table.
> >>
> >> --
> >>
> >> Ken Snell
> >> <MS ACCESS MVP>
> >> http://www.accessmvp.com/KDSnell/
> >>
> >>
> >> "GEORGIA" <(E-Mail Removed)> wrote in message
> >> news:1C7EE6E7-88C4-4A3B-99B2-(E-Mail Removed)...
> >> >i am getting an error message (too few parameters)
> >> >
> >> > Dim strFile As String
> >> > Dim strFolder As String
> >> > Dim strSQL As String
> >> > Dim dbs As DAO.Database
> >> > strFolder = Me.txtfilename.Value
> >> > strFile = Dir$(strFolder & "*.xls")
> >> > Do While Len(strFile) > 0
> >> >
> >> > DoCmd.RunSQL "DELETE tblTemp.* FROM tblTemp;"
> >> >
> >> >
> >> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
> >> > "tblTemp",
> >> > strFolder & strFile, True
> >> >
> >> >
> >> > Set dbs = CurrentDb
> >> >
> >> > strSQL = "INSERT INTO [Accountpay] ( " & _
> >> > "FieldName1, FieldName2, FieldName3, FileNameField )" & _
> >> > " SELECT FieldName1, FieldName2, FieldName3, " & _
> >> > "'" & strFile & "' FROM [tblTemp]"
> >> >
> >> > dbs.Execute strSQL, dbFailOnError
> >> > Set dbs = Nothing
> >> >
> >> >
> >> >
> >> >
> >> > strFile = Dir$()
> >> > Loop
> >> >
> >> > "Ken Snell MVP" wrote:
> >> >
> >> >> DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
> >> >> "tblTest",
> >> >> strFolder & strFile, True
> >> >>
> >> >> Dim strSQL As String
> >> >> Dim dbs As DAO.Database
> >> >> Set dbs = CurrentDb
> >> >> strSQL = "INSERT INTO [RealTableName] ( " & _
> >> >> "FieldName1, FieldName2, FieldName3, FileNameField )" & _
> >> >> " SELECT FieldName1, FieldName2, FieldName3, " & _
> >> >> "'" & strFile & "' FROM [InterimTableName]"
> >> >> dbs.Execute strSQL, dbFailOnError
> >> >> Set dbs = Nothing
> >> >>
> >> >> --
> >> >>
> >> >> Ken Snell
> >> >> <MS ACCESS MVP>
> >> >> http://www.accessmvp.com/KDSnell/
> >> >>
> >> >>
> >> >>
> >> >> "GEORGIA" <(E-Mail Removed)> wrote in message
> >> >> news:80F626DF-4313-4697-BE99-(E-Mail Removed)...
> >> >> > Hi,
> >> >> > on # 4, how would i do a calculated field to show the file name?
> >> >> > Thanks
> >> >> >
> >> >> >
> >> >> > "Ken Snell MVP" wrote:
> >> >> >
> >> >> >> You'll need a few extra steps.
> >> >> >>
> >> >> >> One, use an interim table to receive the imported data from one
> >> >> >> spreadsheet
> >> >> >> file.
> >> >> >>
> >> >> >> Two, run a delete query on that interim table to delete all records
> >> >> >> from
> >> >> >> it
> >> >> >> before you do the TransferSpreadsheet action for the file.
> >> >> >>
> >> >> >> Three, do the TransferSpreadsheet action to the interim table.
> >> >> >>
> >> >> >> Four, run an append query to copy the data from the interim table
> >> >> >> to
> >> >> >> the
> >> >> >> permanent table, and add a calculated field to the append query
> >> >> >> that
> >> >> >> provides the name of the file from which the data came (this can be
> >> >> >> done
> >> >> >> by
> >> >> >> building the SQL statement dynamically in code for the append
> >> >> >> query,
> >> >> >> and
> >> >> >> then executing the append query's SQL statement via code).
> >> >> >>
> >> >> >> --
> >> >> >>
> >> >> >> Ken Snell
> >> >> >> <MS ACCESS MVP>
> >> >> >> http://www.accessmvp.com/KDSnell/
> >> >> >>
> >> >> >>
> >> >> >> "GEORGIA" <(E-Mail Removed)> wrote in message
> >> >> >> news72A4305-72C1-4D48-946B-(E-Mail Removed)...
> >> >> >> > Hi I have this code that will import multiple excle files into
> >> >> >> > one
> >> >> >> > table:
> >> >> >> >
> >> >> >> > Private Sub Command3_Click()
> >> >> >> > Dim strFile As String
> >> >> >> > Dim strFolder As String
> >> >> >> >
> >> >> >> > strFolder = Me.txtfilename.Value
> >> >> >> > strFile = Dir$(strFolder & "*.xls")
> >> >> >> > Do While Len(strFile) > 0
> >> >> >> >
> >> >> >> > DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
> >> >> >> > "tblTest",
> >> >> >> > strFolder & strFile, True
> >> >> >> >
> >> >> >> > strFile = Dir$()
> >> >> >> > Loop
> >> >> >> >
> >> >> >> > MsgBox "Finished", vbOKOnly
> >> >> >> >
> >> >> >> > End Sub
> >> >> >> >
> >> >> >> > There are over 50 excel files that are being imported. I have
> >> >> >> > created
> >> >> >> > a
> >> >> >> > column called "File_Name" in a table and would like to update it
> >> >> >> > with
> >> >> >> > file
> >> >> >> > name each time the excel is being imported so that I can refer
> >> >> >> > back
> >> >> >> > to
> >> >> >> > where
> >> >> >> > the data came from. How would I go about doing so?
> >> >> >> >
> >> >> >> > Thank you for help!!
> >> >> >>
> >> >> >>
> >> >> >>
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>

>
>
>

 
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
Append csv or xls file to existing table RitchieJHicks Microsoft Access External Data 3 4th Nov 2008 12:29 PM
Use DIR command to append file names to table =?Utf-8?B?Z2RvbmFsZDIw?= Microsoft Access Form Coding 1 18th Dec 2006 08:20 PM
import and append excel file to main table and sub table socasteel21 via AccessMonster.com Microsoft Access Queries 9 26th Jun 2006 03:32 PM
re:Help to append flat file table to two tables Vika Microsoft Access Queries 1 20th Sep 2005 08:46 PM
Append text (.txt) file to an existing table =?Utf-8?B?RWQ=?= Microsoft Access 2 4th Feb 2005 12:06 PM


Features
 

Advertising
 

Newsgroups
 


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