read Excel file

I want to read an Excel file (given a cell range) to an Access table. Can somebody help me with that?

Thank you very much.
guosongAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dhwanilshahCommented:
'here is an example to help u out

'first refrence the excel library into the vb or access project
Public Sub TransferData1()
On Error GoTo ErrorH

Dim xlapp As Excel.Application
Dim datasheet As Excel.Worksheet
Dim mRng As Excel.Range

Dim curdb As ADODB.Connection
Dim rsCases As New ADODB.Recordset

Dim mRow As Long

Set xlapp = GetObject(, "Excel.Application")
If xlapp Is Nothing Then
    Set xlapp = New Excel.Application
End If

xlapp.Visible = True

xlapp.Workbooks.Open ("C:\WINDOWS\Profiles\Shreyas\Desktop\Case Register\dees_Mact_Cases.xls")

Set datasheet = xlapp.ActiveWorkbook.Sheets("data sheet")

'here u can connect to another database also, it is just an connection obj. I made in access so i have used this statement
Set curdb = CurrentProject.Connection

'open the destination recordset
rsCases.Open "cases", curdb, adOpenDynamic, adLockPessimistic

'specify from where do u want to start
'i.e the first row where the data starts
mRow = 2

'spcify upto where it should continue reading data
Do While mRow <> 319
   
    rsCases.AddNew

'read data into respective colums    
    rsCases(0) = datasheet.Range("a" & mRow)
    rsCases(1) = datasheet.Range("b" & mRow)
    rsCases(2) = datasheet.Range("c" & mRow)
    rsCases(3) = datasheet.Range("d" & mRow)
    rsCases(4) = datasheet.Range("e" & mRow)
    rsCases(5) = datasheet.Range("f" & mRow)
    rsCases(6) = datasheet.Range("g" & mRow)
    rsCases(7) = datasheet.Range("h" & mRow)
    rsCases(8) = datasheet.Range("i" & mRow)
    rsCases(9) = datasheet.Range("j" & mRow)
    rsCases(10) = datasheet.Range("k" & mRow)
    rsCases(11) = datasheet.Range("l" & mRow)
    rsCases(12) = datasheet.Range("m" & mRow)
    rsCases(13) = datasheet.Range("n" & mRow)
    rsCases(14) = datasheet.Range("o" & mRow)
    rsCases(15) = datasheet.Range("p" & mRow)
    rsCases(16) = datasheet.Range("q" & mRow)
    rsCases(17) = datasheet.Range("r" & mRow)
    rsCases(18) = datasheet.Range("s" & mRow)
    rsCases(19) = datasheet.Range("t" & mRow)
    rsCases(20) = datasheet.Range("u" & mRow)
    rsCases(21) = datasheet.Range("v" & mRow)
    rsCases(22) = datasheet.Range("w" & mRow)
    rsCases(23) = datasheet.Range("x" & mRow)
    rsCases(24) = datasheet.Range("y" & mRow)
    rsCases(25) = datasheet.Range("z" & mRow)
    rsCases(26) = datasheet.Range("aa" & mRow)
    rsCases(27) = datasheet.Range("ab" & mRow)
    rsCases(28) = datasheet.Range("ac" & mRow)
    rsCases(29) = datasheet.Range("ad" & mRow)
    rsCases(30) = datasheet.Range("ae" & mRow)
    rsCases(31) = datasheet.Range("af" & mRow)
   
    rsCases.Update
   
    Debug.Print mRow
   
    mRow = mRow + 1
   
Loop

Exit Sub

ErrorH:
    If Err.Number = 429 Then
        Resume Next
    Else
        MsgBox Err.Description, vbInformation, "Error - " & Err.Number
        Stop
        Resume
    End If

End Sub

'this proc. was written by me to migrate data from an excel file to the access db, it was never intended to be a generic app. so it is a little crude but it sure did the wrok for me.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
guosongAuthor Commented:

I cannot get this segment code to work.

I got a compile error for the statement:

Set xlapp = GetObject(, "Excel.Application")

also, what is the "data sheet" in statement:

 Set datasheet =xlapp.ActiveWorkbook.Sheets("data sheet")

The excel files I want to read vary from file to file in terms of number of rows. How can I detect the end of the record when it is being read?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.