Solved

Run-time error '91': Object variable or With block variable not set

Posted on 2014-09-26
6
3,036 Views
Last Modified: 2014-09-26
I have a run-time error when I try to import an excel file into ms access table.
I want excel and ms access in the same folder no matter where the location is.

Sub ImportExcelToAccess()

      Dim xlsht As Excel.Worksheet
      Dim xlWrkBk As Excel.Workbook
      Dim myRec As DAO.Recordset
      Dim xlApp As Excel.Application
      Dim xlWrksht As Excel.Worksheet
      Dim i As Long
      Dim sPathAndFile As String
      Dim sPath As String
      Dim sFile As String

    Set myRec = CurrentDb.OpenRecordset("tblImport_Main")
    Set xlApp = CreateObject("Excel.Application")

    sFile = "Import_Main.xlsx"
    sPath = ActiveWorkbook.Path & "\" & sFile
    
    sPathAndFile = sPath & sFile
    Set xlWrkBk = Workbooks.Open(sPathAndFile)
    
    Set xlWrksht = xlWrkBk.Sheets(1)
       
     For i = 2 To 100
      
        myRec.AddNew
        On Error Resume Next
        myRec.Fields(0) = xlWrksht.Cells(i, "A")
        myRec.Fields(1) = xlWrksht.Cells(i, "B")
        myRec.Fields(2) = xlWrksht.Cells(i, "C")
        myRec.Fields(3) = xlWrksht.Cells(i, "D")
        myRec.Fields(4) = xlWrksht.Cells(i, "E")
        myRec.Fields(5) = xlWrksht.Cells(i, "F")
        myRec.Fields(6) = xlWrksht.Cells(i, "G")
        myRec.Fields(7) = xlWrksht.Cells(i, "H")
        myRec.Fields(8) = xlWrksht.Cells(i, "I")
        myRec.Fields(9) = xlWrksht.Cells(i, "J")
        myRec.Update

    Next
    Set myRec = Nothing
    Set xlApp = Nothing
       
   Exit Sub
            
End Sub

Open in new window


Thank you for your help.
0
Comment
Question by:Queennie L
  • 3
  • 3
6 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40346380
change this

sPath = ActiveWorkbook.Path & "\" & sFile

to

sPath = currentproject.Path & "\" & sFile
0
 

Author Comment

by:Queennie L
ID: 40346395
@Rey Obrero:

I have a run-time error '1004': Sorry, we couldn't find the file. Is it possible it was moved, renamed or deleted?

It stops right here: >>>Set xlWrkBk = Workbooks.Open(sPathAndFile)

Thank you for your help.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40346433
where is the excel file located?
i assume you are calling the codes from Access..

where is the Access app located?

is the Excel file and access app in the same folder?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:Queennie L
ID: 40346443
Yes. Both Excel and Access are in the same folder.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 40346508
change this

Set xlWrkBk = Workbooks.Open(sPathAndFile)

to

Set xlWrkBk = Workbooks.Open(sPath)
0
 

Author Closing Comment

by:Queennie L
ID: 40346600
This solved the error.

You are smart!

Thank you for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HasData 9 37
Direct Mail software 4 45
Sub Reports 8 23
converting visio 2010 to powerpoint 2010  - formatting issues 5 39
We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now