I am attempting to insert values in an SQL server 2000 database table into an Excel spreadsheet. I've tried the same code on an Access database (with the connection string modified) and it works fine, but when I run it on my SQL server table it falls over with a Run time error - 1004 Application defined or object defined error. The data type of the column it seems to have exception to is of type decimal(10,2). It seems fine if the column is a varchar. Can anyone explain why this is happening?
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
Dim strconn As String
Dim strSQL As String
Dim sumCnt As Integer
Dim sumTrn As Long, sumCst As Long
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDatabase;Data Source=ExpServer"
strSQL = "SELECT * FROM dbo.Final ORDER BY ReportCategoryID"
cnn.Open
rst.Open strSQL, cnn
Dim ExcelAp As Excel.Application
Dim ExcelWb As Excel.Workbook
Dim ExcelWs As Excel.Worksheet
Set ExcelAp = CreateObject("Excel.Application"
Set ExcelWb = ExcelAp.Workbooks.Open("d:\project\Reports\Test.xls"
Set ExcelWs = ExcelWb.Worksheets(1)
Dim strWSName As String
strWSName = InputBox("Please enter the Worksheet title", "Worksheet Title Inputbox"
ExcelWs.Name = strWSName
ExcelAp.Visible = True
sumCnt = 0
sumTrn = 0
sumCst = 0
Row = 8
rst.MoveFirst
Do While Not (rst.EOF Or rst.BOF)
ExcelWs.Cells(Row, 10).Value = rst.Fields(2).Value
ExcelWs.Cells(Row, 2).Value = rst.Fields(3).Value
ExcelWs.Cells(Row, 6).Value = rst.Fields(4).Value
sumCst = sumCst + rst.Fields(2).Value
sumCnt = sumCnt + rst.Fields(3).Value
sumTrn = sumTrn + rst.Fields(4).Value
Row = Row + 1
rst.MoveNext
Loop
ExcelWs.Cells(17, 2).Value = sumCnt
ExcelWs.Cells(17, 1).Value = sumTrn
ExcelWs.Cells(17, 6).Value = sumCst
'Save the sheet to C:\Test.xls directory.
ExcelWs.SaveAs ("d:\temp\TestNew.xls" 'this is not case sensitive
'close workbooks
ExcelWb.Close
ExcelAp.Quit
'Close all objects
Set ExcelWs = Nothing
Set ExcelWb = Nothing
Set ExcelAp = Nothing
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
Dim strconn As String
Dim strSQL As String
Dim sumCnt As Integer
Dim sumTrn As Long, sumCst As Long
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDatabase;Data Source=ExpServer"
strSQL = "SELECT * FROM dbo.Final ORDER BY ReportCategoryID"
cnn.Open
rst.Open strSQL, cnn
Dim ExcelAp As Excel.Application
Dim ExcelWb As Excel.Workbook
Dim ExcelWs As Excel.Worksheet
Set ExcelAp = CreateObject("Excel.Application"
Set ExcelWb = ExcelAp.Workbooks.Open("d:\project\Reports\Test.xls"
Set ExcelWs = ExcelWb.Worksheets(1)
Dim strWSName As String
strWSName = InputBox("Please enter the Worksheet title", "Worksheet Title Inputbox"
ExcelWs.Name = strWSName
ExcelAp.Visible = True
sumCnt = 0
sumTrn = 0
sumCst = 0
Row = 8
rst.MoveFirst
Do While Not (rst.EOF Or rst.BOF)
ExcelWs.Cells(Row, 10).Value = rst.Fields(2).Value
ExcelWs.Cells(Row, 2).Value = rst.Fields(3).Value
ExcelWs.Cells(Row, 6).Value = rst.Fields(4).Value
sumCst = sumCst + rst.Fields(2).Value
sumCnt = sumCnt + rst.Fields(3).Value
sumTrn = sumTrn + rst.Fields(4).Value
Row = Row + 1
rst.MoveNext
Loop
ExcelWs.Cells(17, 2).Value = sumCnt
ExcelWs.Cells(17, 1).Value = sumTrn
ExcelWs.Cells(17, 6).Value = sumCst
'Save the sheet to C:\Test.xls directory.
ExcelWs.SaveAs ("d:\temp\TestNew.xls" 'this is not case sensitive
'close workbooks
ExcelWb.Close
ExcelAp.Quit
'Close all objects
Set ExcelWs = Nothing
Set ExcelWb = Nothing
Set ExcelAp = Nothing
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing