There are also other ways like working with a recordset.....
The simplest and fastest way is to do it with the append query directly, which is fine if you have limited amount of work to do... Though the replace method like I said gets messy because of all the replaces you need to do. I gave you this method because it is the first step to do when you have very limited experience with this type of thing... simply becuase you need not know anything about SQL and/or coding much...
This basicaly is "version 2.0" where we are writing the actual SQL "on the fly" and replacing in the values you need.
Keep in mind for future use the line continuations ( _ ) are maxed at 25 lines, if you go over 25 lines you stop your continuations and either make a new variable or continue by appending to the existing one by doing something like:
Your import code now looks like....
I hope you can see what is going on here and can work with this...
This is the way you work with text variables.
For Dates: " , #" & strFile & "# AS FileName " & _
For numbers: " , " & strFile & " AS FileName " & _
The simplest and fastest way is to do it with the append query directly, which is fine if you have limited amount of work to do... Though the replace method like I said gets messy because of all the replaces you need to do. I gave you this method because it is the first step to do when you have very limited experience with this type of thing... simply becuase you need not know anything about SQL and/or coding much...
This basicaly is "version 2.0" where we are writing the actual SQL "on the fly" and replacing in the values you need.
Keep in mind for future use the line continuations ( _ ) are maxed at 25 lines, if you go over 25 lines you stop your continuations and either make a new variable or continue by appending to the existing one by doing something like:
Code:
strSQL = strSQL & "More sql here"
Your import code now looks like....
Code:
Function import()
Dim strFile As String
Dim strSQL As String
Dim rsC As dao.Recordset
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM DataStaging;"
DoCmd.RunSQL "DELETE * FROM Data;"
' Set file directory for files to be imported
strPath = "C:\test\"
' Tell it to import all Excel files from the file directory
strFile = Dir(strPath & "*.xlsx*")
' Start loop
Do While strFile <> ""
' Import file
DoCmd.TransferSpreadsheet transfertype:=acImport, tablename:="DataStaging", FileName:=strPath & strFile, HasFieldNames:=False
Set rsC = CurrentDb.OpenRecordset("Select F1, F2, F3 from DataStaging where F1 = ""Customer""")
strSQL = " INSERT INTO Data ( F1, F2, F3, filename, customername ) " & _
" SELECT DataStaging.F1 " & _
" , DataStaging.F2 " & _
" , DataStaging.F3 " & _
" , """ & strFile & """ AS FileName " & _
" , """ & rsC!F2 & """ AS CustomerName " & _
" FROM DataStaging;"
CurrentDb.Execute strSQL
' Loop to next file in directory
strFile = Dir
rsc.close
set rsc = nothing
Loop
End Function
I hope you can see what is going on here and can work with this...
This is the way you work with text variables.
For Dates: " , #" & strFile & "# AS FileName " & _
For numbers: " , " & strFile & " AS FileName " & _