|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Displaying recordset in Excel
I need to display data(s) from Access to Excel with 2 blank rows between each specified bank name. Eg. In db, I have a table with BANK_NAME field. There are 5 bank names in the field (2 ING and 3 RoyalBank). I need to display in Excel 2 ING from row 1 to row 2, then from row 5 to row 7 will display RoyalBank. I wrote the following code, but it just print out RoyalBank while I asked in the query ORDER BY BANK_NAME. Meaning, it just display the last bank name's data(s). What I did was I first use a query to find the bank name without repeatation. Then use the bank name to get all the data. Why it won't work?
================== Dim cn As ADODB.Connection Dim rs As New ADODB.Recordset Dim getReport As String Dim excelFileName As String Dim xlsApp As Excel.Application Dim newWorkBook As Excel.Workbook Dim newWorkSheet As Excel.Worksheet Set cn = New ADODB.Connection Set xlsApp = CreateObject("Excel.application") cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Persist Security Info=False;" & _ "Data Source=" & App.Path & "\account_db.mdb" getReport = "SELECT DISTINCT(BANK_NAME) FROM REPORT" Set rs = cn.Execute(getReport) Set xlsApp = New Excel.Application If xlsApp Is Nothing Then MsgBox "Cannot Open Excel Application" Exit Sub End If excelFileName = App.Path & "\Report_Summary_ByCategory.xls" 'Delete summary report for updated summary report If Dir(excelFileName) <> "" Then Kill (excelFileName) End If Set newWorkSheet = xlsApp.Workbooks.Add.Worksheets.Add With newWorkSheet .Range(.Cells(1, 1), .Cells(3, 8)).Select xlsApp.Selection.Columns.AutoFit .Range(.Cells(1, 1), .Cells(3, 8)).RowHeight = 20 .Rows(1).Font.Bold = True .Rows(2).Font.Bold = True .Rows(3).Font.Bold = True .Cells(1, 4).Value = "Andy Tey" .Cells(2, 4).Value = "Financial Report Summary" .Cells(3, 4).Value = "****************************************************************" .Rows(1).HorizontalAlignment = xlHAlignCenter .Rows(2).HorizontalAlignment = xlHAlignCenter .Rows(3).HorizontalAlignment = xlHAlignCenter .Cells(4, 1).Value = "Date / Time" ' cells (2, 1) means row 2 and column 1 .Cells(4, 2).Value = "Bank Name" .Cells(4, 3).Value = "Bank Type" .Cells(4, 4).Value = "Amount $" .Cells(4, 5).Value = "Deposit / Withdrawal" .Cells(4, 6).Value = "Category" .Cells(4, 7).Value = "Comments" 'Header settings .Rows(4).HorizontalAlignment = xlHAlignCenter ' set header in center .Rows(4).Font.Bold = True ' set first row - header as font bold .Rows(4).Font.ColorIndex = 5 ' set header words in blue .Range(.Cells(4, 1), .Cells(4, 1)).RowHeight = 30 Dim p As Integer Dim getTT As String Do While Not rs.EOF For p = 0 To rs.EOF getTT = rs.Fields(p) Dim rs2 As New ADODB.Recordset Dim getReport2 As String getReport2 = "SELECT * FROM REPORT WHERE BANK_NAME='" & getTT & "' ORDER BY BANK_NAME" Set rs2 = cn.Execute(getReport2) Dim k As Integer For k = 0 To rs2.EOF .Cells(i + 6, 1).CopyFromRecordset rs2 'Copy recordset to Excel file Next ' Make every columns autofit to the data(s) .Range(.Cells(4, 1), .Cells(50, 8)).Select xlsApp.Selection.Columns.AutoFit Next ' Call insert4rows rs.MoveNext Loop End With newWorkSheet.SaveAs App.Path & "\Report_Summary_ByCategory.xls" MsgBox " File has been saved to '" & excelFileName & "' " xlsApp.Visible = True 'Show the Excel file - Report_Summary.xls 'after the excel closed, close the excel application If xlsApp.Visible = False Then xlsApp.Quit End If Set newWorkSheet = Nothing Set xlsApp = Nothing rs.Close Set rs = Nothing cn.Close Set cn = Nothing End Sub |
|
#2
|
||||
|
||||
|
it won't display more than one bank name because you're using ...
Select Distinct Bank_Name in your query statement. If you want to use the distinct keyword, then you will have to differentiate your statement a little bit, with something like... Select Distinct Bank_Name, Bank_Location... Distinct is kind of a funny keyword. It can seem like it does the exact opposite of what you want.... in your case, there are five records with 2 distinct names, so the SQL Parser will pull the first one of each that has a distinct name, so you get two records
__________________
Fisherman "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Displaying recordset in Excel |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|