Results 1 to 3 of 3
  1. #1
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664

    Can find file

    I changed the path in the VBA code for the export of an Access table to Excel. It has the correct path. I also changed the prepared a file to the name
    testbook-rec.xlsm.



    But when I export it to Excel it says that it cannot find the file testbook-rec.xlsm. It is in the Desktop directory where it should be.

    Also since I am now selecting the file from a list how do I integrate that into the VBA code so I can d away with the path?

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed
    Attached Files Attached Files

  2. #2
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I modified your browse code. You have "AllowMultiSelect" set to FALSE, so there is no need to loop to get the path/file name selected. I also added the WITH...END WITH construct:
    Code:
    Private Sub btnBrowse_Click()
        Dim diag As Office.FileDialog
        Dim item As Variant
    
        Set diag = Application.FileDialog(msoFileDialogFilePicker)
        With diag
            .AllowMultiSelect = False
            .Title = "Please select an Excel Spreadsheet"
            .Filters.Clear
            .Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx, *.xlsm"
    
            If .Show Then
                Me.txtFileName = .SelectedItems(1)
            End If
        End With
    
    End Sub
    Then I modified your export code. I added a check to ensure an export path/file name was selected. Since the export path/file name is in the text box control, I changed the workbooks open line to use the control Me.txtFileName.
    Last, I closed the record set and destroyed the objects created in the code.
    Code:
    Private Sub ExportTabletoExcel_Click()
        On Error GoTo ExportError
    
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim xl As Excel.Application
        Dim ws As Worksheet
        Dim fld As Field, FieldCount As Integer, J As Integer
        Dim rownum As Long
    
        If Len(Me.txtFileName & vbNullString) > 0 Then
    
            Set xl = New Excel.Application
            xl.Visible = False
    
            xl.Workbooks.Open (Me.txtFileName)
            Set ws = xl.Worksheets("Expanded Tracker")
            ws.Select
    
            Set db = CurrentDb
    
            Set rs = db.OpenRecordset(Me!TableList)
            rownum = 5
    
            rs.MoveFirst
    
            ' FieldCount contains the number of fields in the recordset
            FieldCount = rs.Fields.Count
    
            '
            ' first row of the spreadsheet contains the fieldnames, making it a header row
            For J = 1 To FieldCount
                ws.Cells(rownum, J).Value = rs.Fields(J - 1).Name   'Use j-1 because recordset field index is zero-based
            Next J
    
            '
            ' Now copy the data from the recordset to the Excel spreadshheet rows...
            Do While Not rs.EOF
                rownum = rownum + 1
                For J = 1 To FieldCount
                    ws.Cells(rownum, J).Value = rs.Fields(J - 1).Value  ' Use j-1 because recordset field index is zero-based
                Next J
    
                rs.MoveNext
            Loop
            MsgBox "Finished"
    
        Else
            MsgBox "Error ..... No Export Excel path/file selected."
        End If
    
    ExportError_Exit:
        'clean up before exiting
        On Error Resume Next
    
        xl.ActiveWorkbook.Close (True)
        xl.Quit
        Set xl = Nothing
    
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    
        Exit Sub
    
    ExportError:
        If Err.Number = 3078 Then
            MsgBox "The selected table " & Me!TableList & " is not valid"
        Else
            MsgBox "Error " & Err.Number & ":   " & vbCrLf & Err.Description & vbCrLf & "occurred on export of table " & Me!TableList
        End If
    
        Resume ExportError_Exit
    
    End Sub

  3. #3
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Thanks, I will give it a try. Thanks you very much.

    Respectfully,

    Lou Reed

    PS I am always amazed at my ignorance of basic programing. I have programmed for years and such things
    as what you pointed have little to do with VBA, and db programming. They are basic programming concepts.

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

Similar Threads

  1. VBA cannot find Excel file
    By Lou_Reed in forum Access
    Replies: 1
    Last Post: 12-01-2017, 01:13 PM
  2. "Could not find file <file path." - Error 3024
    By jmitchelldueck in forum Access
    Replies: 4
    Last Post: 07-30-2015, 02:15 PM
  3. Replies: 2
    Last Post: 01-13-2015, 07:16 AM
  4. could not find file due split pasted file
    By alex_raju in forum Access
    Replies: 1
    Last Post: 07-18-2011, 11:27 AM
  5. Find file
    By forestgump in forum Programming
    Replies: 1
    Last Post: 05-15-2011, 10:44 AM

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