473,385 Members | 1,752 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,385 software developers and data experts.

How do you export an access table/query to an excel file (several sheets)

hello,


does anyone know how to write certain information from a table / query using VB into a new excel sheet

for example; i press a button and a printable excel file is made with the name "random [day month year].xls", including 2 sheets, both with different strings of text

id only need the basic codes for it, if anyone knows them?



thank you in advance
Oct 24 '07 #1
5 2054
muddasirmunir
284 100+
if you use crystal report 10 it has a default option to export report
to excel


hello,


does anyone know how to write certain information from a table / query using VB into a new excel sheet

for example; i press a button and a printable excel file is made with the name "random [day month year].xls", including 2 sheets, both with different strings of text

id only need the basic codes for it, if anyone knows them?



thank you in advance
Oct 24 '07 #2
the thing is, im using visual basic express edition (the 2005 one)

any thoughts?
Oct 25 '07 #3
9815402440
180 100+
hi

use following code


Public Sub SaveInExcelSheet(rstRST As Recordset, fileNameWithPath As String)
Dim lngReccount As Long
lngReccount = rstRST.RecordCount
If lngReccount = 0 Then
MsgBox "No data found.", , "Information!"
Exit Sub
End If
Dim objExcel As New Excel.Application
If objExcel Is Nothing Then
MsgBox "Could not start Excel."
Screen.MousePointer = vbNormal
Exit Sub
End If
Dim wb As Workbook
Dim ws As Worksheet
Set wb = objExcel.Workbooks.Add
Set ws = objExcel.Worksheets.Add
' make column headings
' CurRow = 1
' CurCol = 1
' For ctr = 0 To rstRST.Fields.Count - 1
' ws.Cells(CurRow, CurCol).Value = rstRST.Fields.Item(ctr).Name
' CurCol = CurCol + 1
' Next
ws.Cells(1, 1).Value = "Heading"

ws.Cells(2, 1).Value = "Sub Heading"

ws.Cells(4, 1).Value = "Party Name"
ws.Cells(4, 2).Value = "Address"
ws.Cells(4, 3).Value = "Bill No"
ws.Cells(4, 4).Value = "Bill Date"
ws.Cells(4, 5).Value = "Basic Amount"
ws.Cells(4, 6).Value = "TCS"
ws.Cells(4, 7).Value = "Surcharge"
ws.Cells(4, 8).Value = "Edu Cess"
' make col headings bold
Dim cellRange As String
cellRange = ws.Cells(1, 1).Address
cellRange = cellRange & ":" & ws.Cells(4, rstRST.Fields.Count).Address
On Error Resume Next
ws.Range(cellRange).Select
With Selection
.Font.Bold = True
End With
On Error GoTo 0
Err.Clear
rstRST.MoveFirst
Dim lngRow As Long
lngRow = 5
While Not rstRST.EOF
'Replace field names
ws.Cells(lngRow, 1).Value = rstRST.Fields("strPartyName").Value
ws.Cells(lngRow, 2).Value = rstRST.Fields("strAddress").Value
ws.Cells(lngRow, 3).Value = rstRST.Fields("strBillNo").Value
ws.Cells(lngRow, 4).Value = rstRST.Fields("datBillDate").Value
ws.Cells(lngRow, 5).Value = rstRST.Fields("numSubTotal").Value
ws.Cells(lngRow, 6).Value = rstRST.Fields("numTCS").Value
ws.Cells(lngRow, 7).Value = rstRST.Fields("numSurcharge").Value
ws.Cells(lngRow, 8).Value = rstRST.Fields("numEducationCessAmt").Value
lngRow = lngRow + 1
DoEvents
rstRST.MoveNext
Wend
ws.Cells(lngRow, 4).Value = "Total"
ws.Cells(lngRow, 5).Value = "=SUM(E5:E" & CStr(lngRow - 1) & ")"
ws.Cells(lngRow, 6).Value = "=SUM(F5:F" & CStr(lngRow - 1) & ")"
ws.Cells(lngRow, 7).Value = "=SUM(G5:G" & CStr(lngRow - 1) & ")"
ws.Cells(lngRow, 8).Value = "=SUM(H5:H" & CStr(lngRow - 1) & ")"
ws.Columns.AutoFit
wb.SaveAs fileNameWithPath
objExcel.Quit
Set objExcel = Nothing
Set wb = Nothing
Set ws = Nothing
End Sub


regards

manpreet singh dhillon hoshiarpur
Oct 25 '07 #4
thank you, thisll really help me a lot
Oct 25 '07 #5
JustJim
407 Expert 256MB
thank you, thisll really help me a lot
And me! Here's some spaces to make up 20 chars

Jim
Oct 29 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Gary Wright | last post by:
I have an Access 2K database split into front and back. Quite often the users want to do some data analysis that I have not created a report for so they want to export some subset of the data into...
4
by: sunilkeswani | last post by:
I need help with exporting data from 2 access tables, into 2 existing spreadsheets in a single Excel file. Currently, I am using this code: DoCmd.TransferSpreadsheet acExport, 8, "Table1",...
3
by: excyauseme | last post by:
Hi guys! Do you know what is the best way to export a text file, this one is a log file that is already comma delimited thru a module run by my access database, to an excel spreadsheet? I need to...
7
by: Vanessa | last post by:
hi Everyone, I have two questions on exporting data to Excel using ASP (w/o converting formatted excel file into web page and then plug in the dynamic data): 1. Can we export data into...
5
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
0
by: sandervanee | last post by:
Hello, I am trying to export several Access 2003 select queries to Excel 2003 using an Access macro. I'm using the macro command "TransferSpreadsheet" to export the queries. This going quit well,...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
1
by: DennisBetten | last post by:
First of all, I need to give some credit to Mahesh Chand for providing me with an excellent basis to export data to excel. What does this code do: As the title says, this code is capable of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.