Greetings...first off, I am a novice when it comes to programming. The code I am using someone else wrote, and it works great except for the fact that when I first open Access it won't run. I get a Run-time error '3011': Access cannot find the object *.csv file. etc. However, if I actually manually import one csv file with the import specifications this code is calling...then it works. Any assistance would be greatly appreciated!!!
I am using Access 2010; Windows XP
Do While OurFile <> ""
'-- I created and saved the MetDataImportSpecifications earlier.
'-- I also created the tblMetTowerImports table with the same fields as in the csv files, did not import Sensor 2 or Sensor 6
DoCmd.TransferText acImportDelim, "MetDataImportSpecifications", "tblMetTowerImports", OurFile (Highlights this line)
Below is the full code...
'This code was originally written by Terry Kreft.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code courtesy of
'Terry Kreft
Option Compare Database
Option Explicit
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long
Private Const BIF_RETURNONLYFSDIRS = &H1
Public Function BrowseFolder1(szDialogTitle As String) As String
Dim X As Long, bi As BROWSEINFO, dwIList As Long
Dim szPath As String, wPos As Integer
With bi
.hOwner = hWndAccessApp
.lpszTitle = szDialogTitle
.ulFlags = BIF_RETURNONLYFSDIRS
End With
dwIList = SHBrowseForFolder(bi)
szPath = Space$(512)
X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
If X Then
wPos = InStr(szPath, Chr(0))
BrowseFolder1 = Left$(szPath, wPos - 1)
Else
BrowseFolder1 = vbNullString
End If
End Function
Public Sub GetMetTowersData()
'-- Import all of the Met Tower data to the tblMetTowerImports table using MetDataImportSpecifications.
Dim OurFolder As String
Dim OurFile As String
'-- First have the user select the folder
OurFolder = BrowseFolder1("Select the folder that contains the Met Tower data.")
'-- Dir returns a ZLS ("") when no more files match the criteria
OurFile = Dir(OurFolder & "\*.csv")
Do While OurFile <> ""
'-- I created and saved the MetDataImportSpecifications earlier.
'-- I also created the tblMetTowerImports table with the same fields as in the csv files, did not import Sensor 2 or Sensor 6
DoCmd.TransferText acImportDelim, "MetDataImportSpecifications", "tblMetTowerImports", OurFile
'-- Get the next *.csv filename
OurFile = Dir
Loop
ImportMultiple_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ImportMultiple"
End Sub
'This code was originally written by Terry Kreft.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code courtesy of
'Terry Kreft