Question : Method "CopyFromRecordset" of object 'Range' failed

I am getting an error that reads: Method "CopyFromRecordset" of object 'Range' failed when using the following code.

Private Sub Command12_Click()

On Error GoTo Err_Command12_Click

Dim rs As Recordset, ss
Dim wb As Excel.Workbook
Dim datsql As String
Dim salesmanid As String

datsql = "SELECT [name] FROM [tblinformation]"
salesmanid = "SELECT [date] FROM [tblinformation]"

Set rs = CurrentDb.OpenRecordset(datsql)
Set ss = CurrentDb.OpenRecordset(salesmanid)

Set wb = GetObject("C:\Documents and Settings\getalong007\My Documents\Test.xls")
wb.Sheets("Request Sheet").Select
wb.Sheets("Request Sheet").Range("B3").CopyFromRecordset rs
wb.Sheets("Request Sheet").Range("D4").CopyFromRecordset ss

wb.Save

wb.Parent.Windows("ExcelTest").Visible = True

wb.Close SaveChanges:=True

Set wb = Nothing
Set rs = Nothing
Set ss = Nothing

Exit_Command12_Click:
   Exit Sub

Err_Command12_Click:
   MsgBox Err.Description
   Resume Exit_Command12_Click


End Sub

Answer : Method "CopyFromRecordset" of object 'Range' failed

Try this one:

Private Sub Command12_Click()

On Error GoTo Err_Command12_Click

Dim rs As Recordset, ss As Recordset
Dim wb As Excel.Workbook
Dim datsql As String
Dim salesmanid As String

datsql = "SELECT [name] FROM [tblinformation]"
salesmanid = "SELECT [date] FROM [tblinformation]"

Set rs = CurrentDb.OpenRecordset(datsql)
Set ss = CurrentDb.OpenRecordset(salesmanid)
Set wb = GetObject("C:\Documents and Settings\getalong007\My Documents\Test.xls")
wb.Sheets("Request Sheet").Range("B3").CopyFromRecordset rs
wb.Sheets("Request Sheet").Range("D4").CopyFromRecordset ss
wb.Save
wb.Parent.Windows("ExcelTest").Visible = True
wb.Close SaveChanges:=True
Set wb = Nothing
Set rs = Nothing
Set ss = Nothing
Exit_Command12_Click:
  Exit Sub

Err_Command12_Click:
  MsgBox Err.Description
  Resume Exit_Command12_Click
End Sub

Best Regards

Ben
Random Solutions  
 
programming4us programming4us