Results 1 to 7 of 7
  1. #1
    bdhFS is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    May 2010
    Posts
    32

    Error in import code when Access is first initialized

    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

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    According to Access Help, the filename must be the full name, including the path. You are only providing the file name.

    Try this modified code: (I added/changed the lines in blue)
    Code:
    Public Sub GetMetTowersData()
       On Error GoTo ItBroke
    
       '-- Import all of the Met Tower data to the tblMetTowerImports table using MetDataImportSpecifications.
       Dim OurFolder As String
       Dim OurFile As String
       Dim OurPathFile 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 <> ""
          OurPathFile = OurFolder & "\" & 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", OurFolder & "\" & OurFile
    
          '      Debug.Print OurPathFile
    
          '-- Get the next *.csv filename
          OurFile = Dir
       Loop
    
    Exit_ItBroke:
       Exit Sub
    
    
    ItBroke:
       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ImportMultiple"
       Resume Exit_ItBroke
    End Sub

  3. #3
    bdhFS is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    May 2010
    Posts
    32
    Thank you for your response Steve, I really appreciate it!
    I have made the changes have you have so graciously given. I am getting a Compile error: Label not defined @ On Error GoTO It Broke

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Label not defined @ On Error GoTO It Broke
    There should NOT be a space

    Should be:

    On Error GoTO ItBroke

  5. #5
    bdhFS is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    May 2010
    Posts
    32
    Click image for larger version. 

Name:	2013-04-26_1348_Error.png 
Views:	6 
Size:	26.6 KB 
ID:	12091

    Sorry, I typed in the thread that way, but in the code it has no spaces.

  6. #6
    bdhFS is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    May 2010
    Posts
    32
    This "Error" code worked in the other code you helped me with Steve, not sure why it is not working in this code.

  7. #7
    bdhFS is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    May 2010
    Posts
    32
    Found the problem...it was me! Fixed and working great!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Restricted Access Login Code Error
    By need_help12 in forum Programming
    Replies: 6
    Last Post: 04-27-2012, 08:48 AM
  2. Replies: 0
    Last Post: 12-08-2011, 09:12 AM
  3. Access 2010 - Error Code 438 Problem
    By Lexus350 in forum Access
    Replies: 5
    Last Post: 03-03-2011, 11:46 PM
  4. Import into SQL Server from Access Error
    By kaledev in forum Access
    Replies: 1
    Last Post: 02-16-2011, 03:43 PM
  5. Field returns error when I import Text Files to Access
    By geng in forum Import/Export Data
    Replies: 3
    Last Post: 06-01-2010, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums