Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy Excel and Access Question

Status
Not open for further replies.

adamr99

Programmer
Jun 25, 2001
59
US
Hey,

basically what I need to do is copy over some data (not much) from an excell sheet into a access table.

does anyone have any idea how to do this??

Here is the code I am using:

Dim objXL As Object
Dim xlWB As Object
Dim xlWS As Object
'some objects to refer to Excel


Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld As DAO.Field

Dim x As Integer
'Just a counter

Set db = CurrentDb
Set rs = db.OpenRecordset("sampletable")
'open our recordset


Set objXL = CreateObject("Excel.Application")
Set xlWB = objXL.workbooks.Open("C:\adam\sample.xls")
Set xlWS = xlWB.worksheets("52")
'set references to our excel worksheet


'Set qd = db.TableDefs("sampletable")
'db.QueryDefs ("QrySelectDepartment")


'this is the manipulation with the excel table
With xlWS
.range("B2").select
For x = 0 To 2
rs.AddNew
rs(x) = .ActiveCell.Offset(0, x) 'IT DOESN"T LIKE THIS LINE
rs.Update
Next x
'move down a row on spreadsheet

End With


xlWB.Close
objXL.Close

Set xlWS = Nothing
Set xlWB = Nothing
Set objXL = Nothing

rs.Close
Set rs = Nothing
Set db = Nothing
'tidy up time
 
adamr99:

As I see it, the rs(x) is meaningless to Access. First off, the expression rs(x) implies an array. But you've already defined rs as a recordset. If you were to substitute "sampletable" for rs, the logic would appear as, "sampletable"(x).

x what: record? field? index?

Access doesn't know what you want. If you are attempting to put the Excel data into a field in the table, then the code should look something like:

rs.field(either the field name in quotes, or the field's index number in the table).value = .ActiveCell.Offset(0, x)

This will ensure that the sequence of the Excel data is preserved in the Access table, i.e., one value for each record in the same column.

Hope this helps,

Vic

 
Your writeup seems to imply this is a one-time or short time requirement. If so then merely link to the Excel sheet and use and update/append query to update the data tables. For a one-time requirement don't spend the time to resolve these issues. Also, you are using automation rather than a simple link to an Excel file.




-------------------------------------
scking@arinc.com
Try to resolve problems independently
Then seek help among peers or experts
But TEST recommended solutions
-------------------------------------
 
Yes, you are right, it is a very short term assigment.. But, I figured it out another way.

thanks for your post.

adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top