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

    problem: run-time error 1004

    Hello everyone,



    I have a problem related to exporting an Excel spreadsheet to an Access form.

    I am using a module:

    Code:
    Option Compare Database
    
    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
    and a button which is located in my form whose function is to look up an excel file I previously saved and transfer all the excel data which is at the second sheet.

    Code:
    Private Sub cmdNavegar_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").Select
    'Me.codigoRFQ = xlHoja.Range("A2").Value
    xlHoja.Range("B2").Select
    Me.solicitante = xlHoja.Range("B2").Value
    .
    .
    .
    
    xlHoja.Range("AJ2").Select
    Me.comentarios = xlHoja.Range("AJ2").Value
     
    
    xlLibro.Close
            
    Set xlHoja = Nothing
    Set xlLibro = Nothing
    Set xlApp = Nothing
    
    End Sub

    There are some times that the export action works as intended. But quite often I receive an error called: "run-time error 1004"
    When I check the error it shows me that the Range is wrong but it isn´t.
    If i go back to my excel file and click on a different cell and then save it. Go then to my Access application I try again, usually it is solved.

    I don´t why this happen. Is there any solution?

    Thank you in advance.

  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 don't see anything in your code where you are exporting anything to Excel.

    Maybe this link will help you with the code http://www.accessmvp.com/KDSnell/EXC...m#WriteRstFile


    The main page for the Excel examples is Ken's ACCESS Examples and Information for Working With EXCEL Workbook Files

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

    Is there some different approach to solve this problem?
    I create a new Excel file, It seems to be ok, but then It corrupts. It is a little bit frustating.

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Code:
    Set xlApp = Excel.Application
    Actually I don't care for your method of setting the excel object. You don't invoke Create or Create New but rather just seem to rely on it being open.
    Suggest you take a look at http://www.accessmvp.com/KDSnell/EXC...m#WriteRstFile
    and decide on whether or not you want to implement the Get test in that code or not. There's a ton of threads on how to use Automation within Access, and many of them point to Ken's site.

    Not that the following affects your situation, you can simplify using any of these (don't think you need the () either):

    vArchivo = buscaArchivo
    If IsNull(vArchivo) Or vArchivo = "" Then Exit Sub
    Dim xlApp As Excel.Application

    Or
    vArchivo = buscaArchivo
    If Nz(vArchivo,"") = "" Then Exit Sub
    Dim xlApp As Excel.Application

    Or
    If Nz(buscaArchivo,"") = "" Then Exit Sub
    Dim xlApp As Excel.Application
    Last edited by Micron; 03-13-2019 at 12:52 PM. Reason: code correction
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    mar7632 is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Feb 2019
    Location
    Spain - Madrid
    Posts
    72
    Hi everyone,

    I think that I have finally solved the problem! hurra!

    I eliminated this part of the code: xlHoja.Range("A2").Select

    So, it will be like this:
    Code:
    Private Sub cmdNavegar_Click()
    Dim vArchivo As String
    
    vArchivo = buscaArchivo
    
    If IsNull(vArchivo) Or vArchivo = "" Then
    Exit Sub
    Else
    Me.Archivo.Value = vArchivo
     
    End If
    'B)----------------------------------------------------
     
     
    Dim xlLibro As Excel.Workbook
    Dim xlHoja As Excel.Worksheet
     
    
    Set xlLibro = Workbooks.Open(vArchivo)
    Set xlHoja = xlLibro.Sheets(2)
    xlHoja.Visible = True
     
    
     
    
    Me.solicitante = xlHoja.Range("B2").Value
    Me.fecha_consulta = xlHoja.Range("C2").Value
    Me.planta_gonvarri = xlHoja.Range("D2").Value
    .
    .
    .
    Me.comentarios = xlHoja.Range("AJ2").Value
     
    
    xlLibro.Close
            
    Set xlHoja = Nothing
    Set xlLibro = Nothing
     
    End Sub
    Thank you.
    Last edited by mar7632; 03-15-2019 at 02:30 AM. Reason: spelling

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

Similar Threads

  1. Error 1004: 'Range' of object '_Worksheet' failed
    By lccrews in forum Programming
    Replies: 6
    Last Post: 10-08-2018, 09:47 AM
  2. Runtime Error 1004 PLEASE HELP
    By jamesgarf in forum Access
    Replies: 8
    Last Post: 10-12-2012, 03:37 PM
  3. Replies: 0
    Last Post: 07-16-2012, 05:42 AM
  4. Replies: 1
    Last Post: 07-13-2012, 07:58 PM
  5. Run-time error 1004
    By Kirsti in forum Programming
    Replies: 10
    Last Post: 04-01-2012, 09:58 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