Solved

copy data from EXCEL to Ms. ACCESS

Posted on 2002-04-08
6
147 Views
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.AddNew
rs!FullName = xlSheet.cells(1,1)


>>>>>>>>>>>>>>>>>> Ran until last coding it doesn't work, may I know what should I do next?
0
Comment
Question by:shliau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:Madmarlin
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.

eg

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

Madmarlin
0
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 50 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
    Else
        iExcel.Workbooks.Add
    End If
    i = iExcel.Workbooks.Count
    Set iExcelWB = iExcel.Workbooks(i)    
   
    Set iExcelWS = iExcelWB.Worksheets(j)
    'Set iExcelWS = iExcelWB.ActiveSheet

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

        iExcelWB.Save
        iExcelWB.Close
        iExcel.Quit
        Set iExcelWS = Nothing
        Set iExcelWB = Nothing
        Set iExcel = Nothing
End Sub

Cheers
0
 
LVL 44

Expert Comment

by:bruintje
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"
xlApp.ActiveWorkbook.Sheets(1).Cells(1,1)

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 = xlApp.Workbooks.open(<Excel path>)

HTH:O)Bruintje
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:bruintje
ID: 6924828
make that

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

0
 
LVL 49

Expert Comment

by:DanRollins
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
0
 
LVL 1

Expert Comment

by:kodiakbear
ID: 7673194
Moving to the PAQ

kb
Experts Exchange Moderator
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

624 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