Results 1 to 8 of 8
  1. #1
    mar7632 is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2019
    Location
    Spain - Madrid
    Posts
    72

    Exporting many times to an access file gets overwritten


    Hi everyone,

    I would like to export an access record (located in a form) into a excel sheet which is previously prepared in order to send it to a client. The code I am using is similar to the one I am using to import all the data from an excel file (https://www.accessforums.net/showthread.php?t=76013)

    My idea is to send all the information which is in a form (in a certain record) to a excel model and being able to use the same excel model later for different records.

    The data goes to the second excel sheet and then By relationship I move the data through the excel.

    However, It gets overwritten the second time I use the excel file and gets corrupted.


    Here is the code I am using:

    Code:
    Private Sub cmdMandarExcel_Click()
    Dim vArchivo As String
    
    vArchivo = buscaArchivo()
    
    If IsNull(vArchivo) Or vArchivo = "" Then
    Exit Sub
    Else
    Me.Archivo.Value = vArchivo
     
    End If
    
    Dim xlApp As Excel.Application
    Dim xlLibro As Excel.Workbook
    Dim xlHoja As Excel.Worksheet
    Set xlApp = Excel.Application
    Set xlLibro = xlApp.Workbooks.Open(vArchivo)
    Set xlHoja = xlLibro.Sheets(2)
    
    xlHoja.Range("A2").Value = Me.codigoRFQ
    xlHoja.Range("B2").Value = Me.solicitante
    .
    .
    .
    .
    xlHoja.Range("AV2").Value = Me.Archivo
     
    xlLibro.Close
            
    Set xlHoja = Nothing
    Set xlLibro = Nothing
    Set xlApp = Nothing
    
    End Sub
    and the module for vArchivo:
    Code:
    Public Function buscaArchivo() As String
    Dim fDialog As Office.FileDialog
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
    .AllowMultiSelect = False
    .ButtonName = "Seleccionar"
    .Title = "Seleccionar el archivo"
    .InitialFileName = Application.CurrentProject.Path
    .InitialView = msoFileDialogViewDetails
    .Filters.Clear
    .Filters.Add "All Files", "*.*"
    If .Show = True Then
    buscaArchivo = .SelectedItems(1)
    Else
    MsgBox "Ha pulsado el botón <Cancelar>."
    End If
    End With
    End Function
    As expected the code works properly, but the problem is related to the excel file. Any idea or suggestion?

    Thank you in advance,

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,648
    Client needs an entire workbook for a single record? Or do you want to add more records to the same file later?

    Save the modified workbook to a new filename and leave the template unchanged.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    mar7632 is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2019
    Location
    Spain - Madrid
    Posts
    72
    Thank you June for your reply.

    Yes, a client needs an entire workbook for a single record.

    Where is the option to leave the template unchanged?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,648
    Open the template, do edits, then save to a new filename.

    xlLibro.SaveAs "filepath/filename"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    mar7632 is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2019
    Location
    Spain - Madrid
    Posts
    72
    But, after sending the excel file to a certain client I need the excel model with the structure I prepared before.

    Sorry for my ignorance, but where Do I write xlLibro.SaveAs "filepath/filename" ?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,648
    Before xlLibro.Close

    Need to substitute "filepath\filename" with your value. Maybe:

    xlLibro.SaveAs CurrentProject.Path & "\temp.xlsx"

    Then can delete the new file after sent to client.

    The original file will be unchanged.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    mar7632 is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2019
    Location
    Spain - Madrid
    Posts
    72
    Code:
    Private Sub cmdMandarExcel_Click()
    Dim vArchivo As String
    
    vArchivo = buscaArchivo()
    
    If IsNull(vArchivo) Or vArchivo = "" Then
    Exit Sub
    Else
    Me.Archivo.Value = vArchivo
     
    End If
    
    Dim xlApp As Excel.Application
    Dim xlLibro As Excel.Workbook
    Dim xlHoja As Excel.Worksheet
    Set xlApp = Excel.Application
    Set xlLibro = xlApp.Workbooks.Open(vArchivo)
    Set xlHoja = xlLibro.Sheets(2)
    
    xlHoja.Range("A2").Value = Me.codigoRFQ
    xlHoja.Range("B2").Value = Me.solicitante
    .
    .
    .
    .
    xlHoja.Range("AV2").Value = Me.Archivo
     
    xlLibro.SaveAs CurrentProject.Path & "\temp.xlsx"
    xlLibro.Close
            
    Set xlHoja = Nothing
    Set xlLibro = Nothing
    Set xlApp = Nothing
    
    End Sub
    Something like this?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,648
    Yes, like that.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Exporting from Access via CVS or XML file
    By aali in forum Import/Export Data
    Replies: 4
    Last Post: 09-19-2016, 11:38 AM
  2. Access shuts down when exporting to text file
    By mgrlvr in forum Import/Export Data
    Replies: 21
    Last Post: 11-16-2015, 09:59 AM
  3. Field Being Overwritten
    By leanbiz in forum Forms
    Replies: 4
    Last Post: 08-11-2014, 10:05 PM
  4. Exporting a Access Report to an Excel File
    By Coffee in forum Import/Export Data
    Replies: 2
    Last Post: 07-28-2014, 11:32 AM
  5. Exporting table from access to text file
    By narendrabr in forum Import/Export Data
    Replies: 3
    Last Post: 01-08-2013, 09:59 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