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!

Excel Macro to retreive access data 1

Status
Not open for further replies.

JohnAcc

Technical User
Aug 13, 2003
143
GB
Hi There:

I get a run time error when i try and run the following code:

Sub GetTable()
Dim Db As Database
Dim Rs As Recordset
Dim Ws As Object
Dim i As Integer
Dim Path As String
Set Ws = Sheets("Sheet1")
Path = "c:\omnisxls\comp20031.mdb"
Ws.Activate
Range("A1").Activate
Selection.CurrentRegion.Select
Selection.ClearContents
Range("A1").Select
Set Db = Workspaces(0).OpenDatabase(Path, ReadOnly:=True)
Set Rs = Db.OpenRecordset("commissions")
For i = 0 To Rs.Fields.Count - 1
Ws.Cells(1, i + 1).Value = Rs.Fields(i).Name
Next i
Ws.Range(Ws.Cells(1, 1), Ws.Cells(1, Rs.Fields.Count)).Font.Bold = True
Ws.Range("A2").CopyFromRecordset Rs ' I get the error here
Sheets("Sheet1").Select
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Columns.AutoFit
Range("A1").Select
Rs.Close
Db.Close
End Sub


The error msg says:
Method 'copy from recorset' of object 'range' failed

The data was imported into access from an excel file using the import method.

Any ideas ?

Rgds

John

 
Hi John,

I ran you code, substituting one of my databases, and it ran fine.

The only thing that I can think of is that you are exceeding the ROW LIMIT for Excel. I reformatted your code, without Activate/Select. CHeck out the CopyFromRecordset additional parameter...
Code:
Sub GetTable()
    Dim Db As Database
    Dim Rs As Recordset
    Dim i As Integer
    Dim Path As String
    
    Path = "C:\Documents and Settings\metzgerc\My Documents\ExcludeList.mdb"
    With Sheet1
        .Range("A1").CurrentRegion.ClearContents
        
        Set Db = Workspaces(0).OpenDatabase(Path, ReadOnly:=True)
        Set Rs = Db.OpenRecordset("Usage")
        For i = 0 To Rs.Fields.Count - 1
           .Cells(1, i + 1).Value = Rs.Fields(i).Name
        Next i
        Range(.Cells(1, 1), .Cells(1, Rs.Fields.Count)).Font.Bold = True
        With .Range("A2")
            .CopyFromRecordset Rs, 65535 ' I get the error here
            .CurrentRegion.Columns.AutoFit
        End With
    End With
    Rs.Close
    Db.Close
End Sub
Hope it works!

Skip,
Skip@TheOfficeExperts.com
 
Cheers Skip! There was 120,000 rows!

Rgds, John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top