zimmer9
asked on
CopyFromRecordset Run-time error '-2147319779(8002801d')
I ran a stored procedure to create a recordset and now I wanted to quickly transfer the result set of the query to an Excel report format which is the format of choice requested by the user. I am using Excel 2002. I encountered a Runtime Error as follows:
Do you know how I can resolve this ?
With objWS
.Cells(2, 1).CopyFromRecordset rstQueryFS
.Columns(15).NumberFormat = "000-00-0000"
.Columns(18).NumberFormat = "#0.000"
.Columns(24).NumberFormat = "@"
.Cells.EntireColumn.AutoFi t
End with
I get a run time error as follows:
Run-time error '-2147319779(8002801d')
method 'CopyFromRecordset'
of object 'Range' failed
Do you know how I can resolve this ?
With objWS
.Cells(2, 1).CopyFromRecordset rstQueryFS
.Columns(15).NumberFormat = "000-00-0000"
.Columns(18).NumberFormat = "#0.000"
.Columns(24).NumberFormat = "@"
.Cells.EntireColumn.AutoFi
End with
I get a run time error as follows:
Run-time error '-2147319779(8002801d')
method 'CopyFromRecordset'
of object 'Range' failed
ASKER
The whole procedure is as follows:
Private Sub FallUDQuery()
Dim strNextFile As String
Dim rstQueryFS As ADODB.Recordset
Dim objXL As Excel.Application
Dim objWS As Excel.Worksheet
Dim fld As ADODB.Field
Dim intCol As Integer
Dim intRow As Integer
Dim strSQL As String
Dim intBonds As Integer
Dim com As ADODB.Command
Dim cn As ADODB.Connection
Dim P1 As New Parameter
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;" & _
"Data Source=B; Initial Catalog=XXX;" & _
"User ID =XXXClient;PWD="
Set com = New ADODB.Command
With com
Set .ActiveConnection = cn
.CommandType = adCmdStoredProc
.CommandText = "dbo.procUDFl"
.Parameters.Append .CreateParameter("@prmSQL" , adVarChar, adParamInput, 8000, strSQLFS)
.Parameters.Append .CreateParameter("@RptYear F", adInteger, adParamInput, 4, intYearSP)
'.Parameters.Append .CreateParameter("@RptYear S", adInteger, adParamInput, 4, intYearSPS)
Set rstQueryFS = .Execute
End With
Set objXL = New Excel.Application
objXL.Workbooks.Add
strNextFile = GetNextFileName("C:\USERDE F1.XLS")
objXL.ActiveWorkbook.SaveA s strNextFile
Set objWS = objXL.ActiveSheet
objXL.ActiveSheet.Protect UserInterfaceOnly:=True
For intCol = 0 To rstQueryFS.Fields.Count - 1
Set fld = rstQueryFS.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
intRow = 2
With objWS
.Cells(2, 1).CopyFromRecordset rstQueryFS
.Columns(15).NumberFormat = "000-00-0000"
.Columns(18).NumberFormat = "#0.000"
.Columns(24).NumberFormat = "@"
.Cells.EntireColumn.AutoFi t
End with
DoCmd.Hourglass False
objXL.Visible = True
'Set RptYear = .CreateParameter("@RptYear S", adInteger, adParamInput, 4, intYearSPS)
'.Parameters.Append RptYearS
End Sub
Private Sub FallUDQuery()
Dim strNextFile As String
Dim rstQueryFS As ADODB.Recordset
Dim objXL As Excel.Application
Dim objWS As Excel.Worksheet
Dim fld As ADODB.Field
Dim intCol As Integer
Dim intRow As Integer
Dim strSQL As String
Dim intBonds As Integer
Dim com As ADODB.Command
Dim cn As ADODB.Connection
Dim P1 As New Parameter
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;" & _
"Data Source=B; Initial Catalog=XXX;" & _
"User ID =XXXClient;PWD="
Set com = New ADODB.Command
With com
Set .ActiveConnection = cn
.CommandType = adCmdStoredProc
.CommandText = "dbo.procUDFl"
.Parameters.Append .CreateParameter("@prmSQL"
.Parameters.Append .CreateParameter("@RptYear
'.Parameters.Append .CreateParameter("@RptYear
Set rstQueryFS = .Execute
End With
Set objXL = New Excel.Application
objXL.Workbooks.Add
strNextFile = GetNextFileName("C:\USERDE
objXL.ActiveWorkbook.SaveA
Set objWS = objXL.ActiveSheet
objXL.ActiveSheet.Protect UserInterfaceOnly:=True
For intCol = 0 To rstQueryFS.Fields.Count - 1
Set fld = rstQueryFS.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
intRow = 2
With objWS
.Cells(2, 1).CopyFromRecordset rstQueryFS
.Columns(15).NumberFormat = "000-00-0000"
.Columns(18).NumberFormat = "#0.000"
.Columns(24).NumberFormat = "@"
.Cells.EntireColumn.AutoFi
End with
DoCmd.Hourglass False
objXL.Visible = True
'Set RptYear = .CreateParameter("@RptYear
'.Parameters.Append RptYearS
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried:
With objWS
.Range(.Cells(2, 1)).CopyFromRecordset rstQueryFS
.Columns(15).NumberFormat = "000-00-0000"
.Columns(18).NumberFormat = "#0.000"
.Columns(24).NumberFormat = "@"
.Cells.EntireColumn.AutoFi t
End With
I get the following:
Runtime error '1004';
Method 'Range' of object '_worksheet' failed
With objWS
.Range(.Cells(2, 1)).CopyFromRecordset rstQueryFS
.Columns(15).NumberFormat = "000-00-0000"
.Columns(18).NumberFormat = "#0.000"
.Columns(24).NumberFormat = "@"
.Cells.EntireColumn.AutoFi
End With
I get the following:
Runtime error '1004';
Method 'Range' of object '_worksheet' failed
For an example of CopyFromRecordset you can look at
http://www.mvps.org/access/modules/mdl0035.htm
Good Luck!
Gary