Results 1 to 7 of 7
  1. #1
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183

    how export and replace existing file

    Below is the code I have for Export function. It works perfectly fine. But if I don't rename the file after exporting on to my PC, the file does not get replaced when exporting the file for 2nd or 3rd time. What should I add to the below codes for it to work? Please help.

    Thanks




    Code:
    On Error GoTo Err_cmdTest_Click
    'Must 1st set a Reference to the Microsoft Office XX.X Object Library
    Dim dlgOpen As FileDialog
    Dim strExportPath As String
    Const conOBJECT_TO_EXPORT As String = "qryM"
    
    Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
    
    With dlgOpen
      .ButtonName = "Export To"
      .InitialView = msoFileDialogViewLargeIcons
      .InitialFileName = CurrentProject.Path
         If .Show = -1 Then
           'Allow for Root Directory selection: C:\, D:\, etc.
           strExportPath = Replace(.SelectedItems(1) & "\", "\\", "\")
     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
                                   TableName:=conOBJECT_TO_EXPORT, _
                                   FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
                                                                   
     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
                                   TableName:="qryBuilding", _
                                   FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
           
    
          
    
           MsgBox "[" & conOBJECT_TO_EXPORT & "] has been Exported to " & strExportPath & _
                   conOBJECT_TO_EXPORT & ".xls", vbInformation, "Export Complete"
         End If
         
         End With
    
    'Set the Object Variable to Nothing.
    Set dlgOpen = Nothing
    
    DoCmd.Close
    Exit_cmdTest_Click:
      Exit Sub
    
    Err_cmdTest_Click:
      MsgBox Err.Description, vbExclamation, "Error in cmdTest_Click()"
        Resume Exit_cmdTest_Click
    End Sub

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Without knowing what's going on your machine in particular (when I've run into this before it's usually been a permissions issue on a server but you seem to be working local drives, so as long as you've got administrator rights it *should* be overwriting) but you can do something fairly simple. Look at filesystemobject commands here:

    http://www.sqldts.com/292.aspx

  3. #3
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Hi rpeare,

    I do not have administrator rights as this is work computer.
    I also can't seem to open the link to Microsoft filesystemobject commands from http://www.sqldts.com/292.aspx
    Do you have any other links? or if you have any other suggestions?

    Thanks.
    Much appreciated.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    This is the delete file method:
    Code:
    ' Delete File 
    Option Explicit  
    Sub Main()      
    Dim oFSO     
    Dim sSourceFile      
    Set oFSO = CreateObject("Scripting.FileSystemObject")      
    sSourceFile = "C:\SourceFile.txt"      ' Check if file exists to prevent error     
    If oFSO.FileExists(sSourceFile) Then         
         oFSO.DeleteFile sSourceFile     
    End If      ' Clean Up     
    
    Set oFSO = Nothing      
    End

  5. #5
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    So sorry rpeare. I am not a pro in Access database. where do I add the codes you gave me?

    This is what I did and it is not working
    Option Compare Database
    ' Delete File
    Option Explicit
    Sub Main()
    Dim oFSO
    Dim sSourceFile
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sSourceFile = "C:\SourceFile.txt" ' Check if file exists to prevent error
    If oFSO.FileExists(sSourceFile) Then
    oFSO.DeleteFile sSourceFile
    End If ' Clean Up

    Set oFSO = Nothing
    End Function



    Private Sub Command4_Click()

    On Error GoTo Err_cmdTest_Click
    'Must 1st set a Reference to the Microsoft Office XX.X Object Library
    Dim dlgOpen As FileDialog
    Dim strExportPath As String
    Const conOBJECT_TO_EXPORT As String = "qryExportMetrics"

    Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)

    With dlgOpen
    .ButtonName = "Export To"
    .InitialView = msoFileDialogViewLargeIcons
    .InitialFileName = CurrentProject.Path
    If .Show = -1 Then
    'Allow for Root Directory selection: C:\, D:\, etc.
    strExportPath = Replace(.SelectedItems(1) & "\", "\\", "\")
    Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
    TableName:=conOBJECT_TO_EXPORT, _
    FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")

    Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
    TableName:="qryCapacityBuilding", _
    FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")




    MsgBox "[" & conOBJECT_TO_EXPORT & "] has been Exported to " & strExportPath & _
    conOBJECT_TO_EXPORT & ".xls", vbInformation, "Export Complete"
    End If

    End With

    'Set the Object Variable to Nothing.
    Set dlgOpen = Nothing

    DoCmd.Close
    Exit_cmdTest_Click:
    Exit Sub

    Err_cmdTest_Click:
    MsgBox Err.Description, vbExclamation, "Error in cmdTest_Click()"
    Resume Exit_cmdTest_Click
    End Sub

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    you just dumped the code in wherever, you'll have to adapt it to your code, declare the variables where you declare the rest of your variables, do the file existence check before you do your export, if the file is there the code will delete it and your export will work.

  7. #7
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    PERFECT!! IT'S WORKING! Thank you so much rpeare!

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

Similar Threads

  1. Replies: 3
    Last Post: 06-07-2012, 07:05 AM
  2. export query into existing workbook
    By hklein in forum Programming
    Replies: 3
    Last Post: 05-08-2012, 04:55 AM
  3. Export File to another Database
    By maintt in forum Import/Export Data
    Replies: 5
    Last Post: 11-25-2010, 01:07 PM
  4. File Export
    By Kencao in forum Import/Export Data
    Replies: 3
    Last Post: 02-01-2010, 05:27 PM
  5. How do you export a file with a unique file name
    By Budman42 in forum Import/Export Data
    Replies: 1
    Last Post: 10-15-2006, 06:10 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