Improve company productivity with a Business Account.Sign Up


copy data from EXCEL to Ms. ACCESS

Posted on 2002-04-08
Medium Priority
Last Modified: 2010-05-02
Hi there... here is the question on how to copy the data from EXCEL to ACCESS's DB in VB coding.  I managed to establish both EXCEL & ACCESS in VB. Currently I plan to copy data from Excel, say first cell into ACCESS's DB.

I used this but it doesn't work.
rs!FullName = xlSheet.cells(1,1)

But then if I replaced xlSheet.cells,
rs!FullName = "GRACE", it works!!

ERROR >> Object variable or with block variable not set.


Dim cnnFind As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

cnnFind.ConnectionString = "Provider=......Data source... XXX.mdb"
Set rs = New ADODB.Recordset
rs.Open <tableName>
Set xlApp = New Excel.Application
Set xlSheet = New Excel.Worksheet
Set xlSheet = New Excel.Workbook
Set xlBook = GetObject(<Excel path>)

rs!FullName = xlSheet.cells(1,1)

>>>>>>>>>>>>>>>>>> Ran until last coding it doesn't work, may I know what should I do next?
Question by:shliau

Expert Comment

ID: 6924754

If your xlsheet.cells(1,1) property is popukated and seems to be referencing the correct values then you need an .Update method.


rs!FullName = xlSheet.cells(1,1)

LVL 56

Accepted Solution

Ryan Chong earned 200 total points
ID: 6924758
Hi shliau,

Set xlSheet = New Excel.Worksheet
Set xlSheet = New Excel.Workbook

Here is an example, customize it to see whether it works:

Private Sub example()
    Dim iExcel As New Excel.Application
    Dim iExcelWB As Excel.Workbook
    Dim iExcelWS As Excel.Worksheet
    'iExcel.Visible = True
    If Dir$(TargetFile) <> "" Then
        iExcel.Workbooks.Open TargetFile
    End If
    i = iExcel.Workbooks.Count
    Set iExcelWB = iExcel.Workbooks(i)    
    Set iExcelWS = iExcelWB.Worksheets(j)
    'Set iExcelWS = iExcelWB.ActiveSheet

    Msgbox iExcelWS.Cells(1, 1).Value '<< Here

        Set iExcelWS = Nothing
        Set iExcelWB = Nothing
        Set iExcel = Nothing
End Sub

LVL 44

Expert Comment

ID: 6924826
Hi shliau,

you could also walk the tree down the objectmodel which is easier in reading and maintanence

Set xlApp = New Excel.Application
xlApp.Workbooks.Open "Yourname.xls"

In the code above a little change is needed to tell excel which book and which sheet to use

Set xlApp = New Excel.Application
Set xlSheet = New Excel.Worksheet
Set xlSheet = New Excel.Workbook
Set xlBook =<Excel path>)

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 44

Expert Comment

ID: 6924828
make that

Set xlApp = New Excel.Application
Set xlBook =<Excel path>)
Set xlSheet = xlBook.Sheets(1)

LVL 49

Expert Comment

ID: 7643005
Hi shliau,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept ryancys's comment(s) as an answer.

shliau, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 7673194
Moving to the PAQ

Experts Exchange Moderator

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As a person who answers a lot of questions, I often see code that could be simplified, made easier to read, and perhaps most importantly made easier to maintain if the code was modified to use the Select Case statement. This article explains how to…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

580 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