Link to home
Start Free TrialLog in
Avatar of Domc
Domc

asked on

Problem using CopyFromRecordset command

Access Error: Class does not support automation or does not support expected interface

Sub PasteNewGeogData()

Dim xlWB As Object

Dim strPath As String
Dim strFileName As String

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer
Dim fieldValue As Long
Dim xLSheet As Variant
Dim qdf As DAO.QueryDef

On Error GoTo After_Error

strPath = "\\Mac\AAIM\Portfolios\"
strFileName = "GraphAccessGeogData.xlsx"
Set xlWB = Nothing

If IsValidOLELinkEstablished(xlWB, strPath, strFileName) = True Then
 
        'Set worksheet
        Set xLSheet = xlWB.Sheets("Sheet2")
        xlWB.Application.Calculation = xlManual
        xlWB.Application.Screenupdating = False
        'Get reference to database and relevant query
       
        Set db = CurrentDb
       
        Set qdf = CurrentDb.QueryDefs("qry_UnitsByCountyForSelectedHA")
        qdf.Parameters("TempVars!HASelected") = TempVars("HASelected").Value
        Set rs = qdf.OpenRecordset()

       
        xLSheet.range("A1").copyfromrecordset (rs)  'This gives the error
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
xlSheet should be declared as Object, not Variant.
Avatar of Domc
Domc

ASKER

Thanks for help. Changing xlSheet to Object had no effect.
Taking off the parentheses changes the error to
"No current record" - at exactly the same place
still, xlSheet should be declared as an object.

re:
qdf.Parameters("TempVars!HASelected") = TempVars("HASelected").Value

Open in new window

Are you sure you actually have a parameter in the query defined as: Tempvars!HASelected?

should this line be:
qdf.Parameters("[HASelected]") = TempVars!HASelected.Value

Open in new window


You might want to put a page break on the line after that, and check the value of Tempvars!HASelected.
Then run the subsequent line of code (F8) to create the recordset and in the immediate window try:
?rs.BOF
?rs.EOF

Open in new window

Then, if rs.EOF is true, then you know that your query is returning no records. iif rs.EOF = false then try
rs.MoveLast
?rs.RecordCount
rs.MoveFirst

Open in new window

and then F8 to the next line, the line that copies the recordset to cell A1 of the worksheet.
Avatar of Domc

ASKER

Yes rs definitely works. I'd actually already inserted a break point, copied the rs variable into the watch window and you can see it has a record count of 51 - which is exactly as it should be. So rs is a definite recordset - that's not the issue. I've attached a breakdown of its properties in case of help.
Screenshot-2019-08-09-at-18.34.35.jpg
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Domc

ASKER

Hi Daniel,
I think that's that was suggested above - i.e. just removing the brackets.
That just changes the error into - "no current record"

Thanks for the suggested function but would prefer to fix mine if at all possible as don't think it's far off.
Avatar of Domc

ASKER

My apologies - removing the brackets did solve it - although not sure I understand why as the documentation on that function showed brackets.

It must have been code on the next line that caused the no current record error. Thanks to all
You're welcome Domc!