473,398 Members | 2,393 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,398 software developers and data experts.

Transfer to Excel from a form

I can transfer from a query with

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Filenam",
CPath, True

I would like to use a form for the user to select and order data then export
th result. How do I set about this?
Nov 17 '06 #1
3 4094
I assume you are using the form to let users specify some of the parameters
of a query, which your code then builds the SQL string for.

You can either save the SQL string as a new queryDef and then hand that
querydef
name to the DoCmd.TransferSpreadsheet - or - Change the SQL string to
create a
new table and hand the table name to the DoCmd.TransferSpreadsheet method.

TransferSpreadsheet works only with saved queries and tables.

Kevin C
"JohnM" <jo**@jmawer.demon.co.ukwrote in message
news:ej*******************@news.demon.co.uk...
>I can transfer from a query with

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Filenam",
CPath, True

I would like to use a form for the user to select and order data then
export th result. How do I set about this?

Nov 17 '06 #2
Thanks ... it does make sense, but a bit of a pity, because I'm working on a
network where the users don't have save rights, one of the reasons I'm doing
this is so that they can take out chunks of data and analyse it in excel in
their own area, but aren't able to alter the database. It all works a treat
up to this selection bit.
Any ideas would be gratefully received.
"Kc-Mass" <co********@comcast.netwrote in message
news:6P******************************@comcast.com. ..
>I assume you are using the form to let users specify some of the parameters
of a query, which your code then builds the SQL string for.

You can either save the SQL string as a new queryDef and then hand that
querydef
name to the DoCmd.TransferSpreadsheet - or - Change the SQL string to
create a
new table and hand the table name to the DoCmd.TransferSpreadsheet
method.

TransferSpreadsheet works only with saved queries and tables.

Kevin C
"JohnM" <jo**@jmawer.demon.co.ukwrote in message
news:ej*******************@news.demon.co.uk...
>>I can transfer from a query with

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Filenam",
CPath, True

I would like to use a form for the user to select and order data then
export th result. How do I set about this?


Nov 18 '06 #3
You could use code to transfer a recordset (see below - nice code, not mine)
or you could pull the data from the excel side with MS Query.

'put this code in a form event
Private Sub button_Click()
Dim sql as String
Dim rs as ADODB.Recordset
'change the query to select from your database
sql = "SELECT Drivers.* FROM Drivers WHERE Drivers.DriverId 0;"
set rs = KeySet_Rs(sql)
ExportExcel "c:\excelfiles", "filename.xls", rs
rs.close
set rs = nothing

End Sub
'************************************************* *****************
'put this code below in a module

'If your using only access use CurrentProject.Connection as the connection
string
Public Function Connect() As String
Connect = Application.CurrentProject.Connection
End Function
' Returns Disconnected Keyset Recordset
Public Function KeySet_Rs(sql As String) As ADODB.recordset
Dim rs As ADODB.recordset
Set rs = New ADODB.recordset
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.CursorType = adOpenKeyset
rs.Open sql, Connect()
rs.ActiveConnection = Nothing
Set KeySet_Rs = rs
End Function
'Exports a recordset to Excel
Public Sub ExportExcel(path As String, filename As String, rs As
ADODB.recordset)
Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
objXL.Visible = False 'Set to true if you want to see it
objXL.Workbooks.Add 'creates new empty worksheet in excel

Dim row As Long, column As Long
column = 1
row = 1
With objXL.Application
Do While Not (rs.EOF Or rs.BOF)
If row = 1 Then
For column = 0 To rs.Fields.count - 1 'Get Header Row with
Field Names
.ActiveSheet.cells(row, column + 1).value =
rs.Fields(column).Name
.ActiveSheet.cells(row, column + 1).Borders.Weight =
xlMedium
.ActiveSheet.cells(row, column + 1).Interior.ColorIndex
= 15

.ActiveSheet.cells(row, column + 1).Interior.Pattern = 1
.ActiveSheet.cells(row, column + 1).Interior.PatternColorIndex =
xlAutomatic
Next
row = row + 1
End If
For column = 0 To rs.Fields.count - 1 'Loop through
recordset and insert all the records into excel
Dim str As String
str = IIf(IsNull(rs.Fields(column)), "", rs.Fields(column))
If Len(str) 0 Then
If IsDate(str) Then
.ActiveSheet.cells(row, column + 1).NumberFormat =
"mm/dd/yyyy hh:mm:ss AM/PM"
End If
End If
.ActiveSheet.cells(row, column + 1).value =
rs.Fields(column)
.ActiveSheet.cells(row, column + 1).Borders.Weight = xlThin
Next
row = row + 1
rs.MoveNext
Loop
.ActiveSheet.Columns("A:ZZ").AutoFit
End With

If Not PathExists(path) Then
MkDir path
End If
objXL.ActiveWorkbook.SaveAs path & "\" & filename
objXL.Quit 'Remove this if you want to leave Excel Open
Set objXL = Nothing
End Sub
"JohnM" <jo**@jmawer.demon.co.ukwrote in message
news:ej*******************@news.demon.co.uk...
Thanks ... it does make sense, but a bit of a pity, because I'm working on
a network where the users don't have save rights, one of the reasons I'm
doing this is so that they can take out chunks of data and analyse it in
excel in their own area, but aren't able to alter the database. It all
works a treat up to this selection bit.
Any ideas would be gratefully received.
"Kc-Mass" <co********@comcast.netwrote in message
news:6P******************************@comcast.com. ..
>>I assume you are using the form to let users specify some of the
parameters
of a query, which your code then builds the SQL string for.

You can either save the SQL string as a new queryDef and then hand that
querydef
name to the DoCmd.TransferSpreadsheet - or - Change the SQL string to
create a
new table and hand the table name to the DoCmd.TransferSpreadsheet
method.

TransferSpreadsheet works only with saved queries and tables.

Kevin C
"JohnM" <jo**@jmawer.demon.co.ukwrote in message
news:ej*******************@news.demon.co.uk...
>>>I can transfer from a query with

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Filenam",
CPath, True

I would like to use a form for the user to select and order data then
export th result. How do I set about this?



Nov 19 '06 #4

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

Similar topics

1
by: DataB | last post by:
Hi Everyone! I have, essentially, two table structures in the same ms access database file. Both are of the parent-child format, one parent table with personal details, then multiple child...
0
by: Larry Jones | last post by:
I am pulling Excel single cell information from a workbook to a textbox in a VB.net form. Most of the information is transferring without a problem, but some cells with function calculations are...
6
by: ad | last post by:
I wnat to load a file in an .resx file and transfer the file to a stream. How can I do?
10
by: scoopthis | last post by:
Hi, I have an application where I transfer an excel file to a table on SQL through an adp file. The excel file does change so I pass the name parameter from a cmd line. The excel file has a specific...
5
by: hmiller | last post by:
Hey there folks: I have been trying to get this work for about a week now. I'm new to VBA... I am trying to transfer a populated table in Access to an existing, but blank, Excel worksheet. I...
6
by: okan | last post by:
Hi everyone. My question is that i have a form which includes subform,too. in this form for example i choose a country and in the subform its capital, currency,president are shown. and i have a...
0
by: blainegray | last post by:
Greetings This is one of those Access is not closing Excel problems. The first time through the code works fine. The second time there is a problem. After lots of combinations, I finally...
1
by: chuch0117291 | last post by:
Hi, i have a form with the main details of a vendor and and contract description and a budget reference(PK). Within this form, i have a sub form which includes cost details for that specific vendor...
5
by: billa856 | last post by:
Hi I am totaly new to MS Access. I have one worksheet in excel in which I have data of company's inventory. I want to know can I transfer data from a form made in MS Access to the Excel worksheet?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.