Sub TransferAll(p_strDirectory As String, Optional p_strFile As String = "*.*")
Dim l_strFile As String
If Right(Trim(p_strDirectory), 1) <> "\" Then
p_strDirectory = Trim(p_strDirectory) & "\"
Else
p_strDirectory = Trim(p_strDirectory)
End If
l_strFile = Dir(p_strDirectory & p_strFile)
Do While l_strFile <> ""
DoCmd.TransferText acImportDelim, "", "tblTmpLoadXls", l_strFile, False
DoEvents
l_strFile = Dir()
DoEvents
Loop
End Sub
my question for you is...why does Access try to name the Access fields in the table as "F1", "F2", etc... when you try to import files like these into an existing table?????
Sub TransferAll(p_strDirectory As String, Optional p_strFile As String = "*.*") 'Pass in the directory you want to iterate through here. I used the default parameter of "*.*" so I could test on my system which has no .csv files.
Dim l_strFile As String 'A place to hold the results of the Dir command.
If Right(Trim(p_strDirectory), 1) <> "\" Then
'I just want to make sure that we build a file name string that is properly formed.
p_strDirectory = Trim(p_strDirectory) & "\" 'Used to dummy proof.
Else
p_strDirectory = Trim(p_strDirectory)
End If
l_strFile = Dir(p_strDirectory & p_strFile) 'Run the Dir() function to "prime" the Dir pipeline and get the first file name.
Do While l_strFile <> "" 'If the most recent call to Dir returns no result, exit the loop.
'Your code, I don't really know if it is written correctly
DoCmd.TransferText acImportDelim, "", "tblTmpLoadXls", l_strFile, False
DoEvents 'Just me being cautious in case you get into an infinite loop
l_strFile = Dir() 'For subsequent calls to Dir after the first "priming" call. See the help file for Dir.
DoEvents 'Again, just me being cautious in case you get into an infinite loop
Loop 'Try the loop again
'The loop is over and processing is complete.
End Sub
Private Sub Cmd_Upload_Data_Click()
On Error GoTo Err_Cmd_Upload_Data_Click
' Switch off all the WARNING msgs.
DoCmd.SetWarnings True
Dim strFileName As String
Dim cstrPath As String
DoCmd.OpenQuery "Query_del_temp_load_tab"
MsgBox "Temp Table Deleted"
cstrPath = txt_box_file_path.Value
'MsgBox " File Path " & cstrPath
strFileName = Dir(cstrPath & "*.csv")
MsgBox "Excel Name" & strFileName
Do While strFileName <> ""
DoCmd.TransferText acImportDelim, "", "tblTmpLoadXls", "C:\Users\gmassengale\Desktop\CSV Converts\", False, txt_box_file_path & strFileName = Dir()
Loop
MsgBox "Data upload Successful."
' Switch on the WARNING msgs.
DoCmd.SetWarnings True
Exit_Cmd_Upload_Data_Click:
Exit Sub
Err_Cmd_Upload_Data_Click:
MsgBox Err.Description
Resume Exit_Cmd_Upload_Data_Click
End Sub
I'm not sure either Gemma. That's why I think corruption is starting to creep in. The error message that I am getting doesn't make any sense. Not sure what to do...help??adam
isnt the F1/F2 headers something to do with whether you specify that the csv has field names in the first column
if you say not, and dont use an import spec, access will allocate F1, F2 etc as field names in the table
i dont understand how they would get used if you imported to an existing table though.