473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help! Populating Excel multiple worksheets from recordsets

B
Using Access2000, the sample code below is what I have been modifying and
working on since the past week and I could not get it to work properly.

What I wanted to accomplish:
1) read from a recordset and export to Excel
2) Excel is populated based from an ID (may possible be one or multiple) and
renames the worksheet based from the ID
3) the code also format the fields

The sample database may be downloaded at:
http://www.geocities.com/mgtulips/sample_db.mdb

TIA!
Bob
Function post_export_excel()

Dim db As Database 'used to reference the current database
Dim rs As Recordset, rsID As Recordset
Dim qDef As QueryDef
Dim xlApp As Object, wkb As Object, wks As Object
Dim sCarrier As String, sMPC As String
Dim i As Integer, intSPID As Integer
Dim strSQL As String, strSQL2 As String
Dim iWorksheets As Integer, x As Integer
Set db = CurrentDb() 'initialize the database variable to this
database
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True

Set rsID = db.OpenRecordset("qryID", dbOpenDynaset)
Set wkb = xlApp.Workbooks.Add

iWorksheets = DCount("*", "qryID")
Set wks = wkb.ActiveSheet

Do While Not rsID.EOF
For x = 1 To iWorksheets
'Go to Next Worksheet
Worksheets(x).Activate

strSQL2 = "SELECT rank, desc, inv_current, inv_previous, inv_variance,"
strSQL2 = strSQL2 & " ytd_current, ytd_previous, ytd_variance,"
strSQL2 = strSQL2 & " market_share_current, market_share_previous,
share_vs_ly"
strSQL2 = strSQL2 & " FROM tmp_result_all"
strSQL2 = strSQL2 & " WHERE id = '" & rsID("id").Value & "'"
Set rs = db.OpenRecordset(strSQL2, dbOpenDynaset)

'format the excel report
wks.Range("A1:A3").Font.Bold = True
wks.Range("A1:A3").Font.Size = 10
wks.Range("A1:A3").Font.Color = RGB(255, 0, 0)
wks.Range("A1:A3").HorizontalAlignment = xlLeft

wks.Range("A5:K5").Font.Bold = True
wks.Range("A5:K5").Font.Size = 10
wks.Range("A5:K5").Font.Name = "Arial"
wks.Range("A5:K5").Interior.ColorIndex = 15
wks.Range("A5:K5").WrapText = True
wks.Range("A5:K5").HorizontalAlignment = xlCenter
wks.Range("A5:A60").HorizontalAlignment = xlCenter
wks.Range("E5:E60").HorizontalAlignment = xlCenter
wks.Range("H5:K60").HorizontalAlignment = xlCenter
wks.Range("B57:K57").Font.Bold = True
wks.Rows(5).RowHeight = 35
'wks.Range("B4:B55").HorizontalAlignment = xlLeft
'wks.Range("D:E").HorizontalAlignment = xlCenter
'rank
wks.Range("A5:A60").ColumnWidth = 15
'desc
wks.Range("B5:B60").ColumnWidth = 30
'inv
wks.Range("C5:E60").ColumnWidth = 13
'ytd_current
wks.Range("F5:F60").ColumnWidth = 12
'ytd_previous, ytd_variance
wks.Range("G5:I60").ColumnWidth = 13

wks.Range("A1").Value = "Test1"
wks.Range("A2").Value = "Test2"
wks.Range("A3").Value = "Test3"
wks.Range("A5").Value = "RANK"
wks.Range("B5").Value = "DESC"
wks.Range("C5").Value = "INV CURRENT"
wks.Range("D5").Value = "INV PREVIOUS"
wks.Range("E5").Value = "INV VARIANCE"
wks.Range("F5").Value = "YTD CURRENT"
wks.Range("G5").Value = "YTD PREVIOUS"
wks.Range("H5").Value = "YTD VARIANCE"
wks.Range("I5").Value = "MARKET SHARE CURRENT"
wks.Range("J5").Value = "MARKET SHARE PREVIOUS"
wks.Range("K5").Value = "SHARE VS LY"

wks.Range("A1").NumberFormat = "mmm-yy"
wks.Range("C6:D60").NumberFormat = "#,##0_);-#,##0"
wks.Range("E6:E60").NumberFormat = "#,##0.00%_);-#,##0.00%"
wks.Range("F6:G60").NumberFormat = "#,##0_);-#,##0"
wks.Range("E6:E60").NumberFormat = "#,##0%_);-#,##0%"
wks.Range("H6:H60").NumberFormat = "#,##0%_);-#,##0%"
wks.Range("I6:J60").NumberFormat = "#,##0.0%_);-#,##0.0%"
wks.Range("K6:K60").NumberFormat = "#,##0%_);-#,##0%"

wks.Range("A6").CopyFromRecordset rs
'insert columns
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
wks.Range("C:C").ColumnWidth = 0.5

Columns("G:G").Select
Selection.Insert Shift:=xlToRight
wks.Range("G:G").ColumnWidth = 0.5

Columns("K:K").Select
Selection.Insert Shift:=xlToRight
wks.Range("K:K").ColumnWidth = 0.5

Rows(57).EntireRow.Insert
Range("B58:N58").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone

'delete 51 & 52 number description from RANK field in Excel
wks.Range("A56:A58").Value = ""

wks.Name = rsID("id").Value
rsID.MoveNext
Next x
Loop

rs.Close
rsID.Close

Set rs = Nothing
Set rsID = Nothing
Set qDef = Nothing
Set wkb = Nothing
Set wks = Nothing
Set xlApp = Nothing
Set db = Nothing
End Function
Nov 13 '05 #1
0 2059

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
2
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.