Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old October 2nd, 2003, 01:28 AM
hbcontract hbcontract is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 59 hbcontract User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 36 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old October 2nd, 2003, 08:14 AM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,169 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 8 h 34 m 4 sec
Reputation Power: 110
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Displaying recordset in Excel


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway