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

Can I use DoCmd.OutputTo to export the current record only?

16
Hello all,

I would like to create a command button on a form that will allow the user to export the current record to .xls.

The following exports the entire table. Is there a way to export the current record being viewed only?

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acOutputForm, "frmInputView", acFormatXLS, "filename.xls", False
Many thanks,
Tim
Mar 4 '11 #1

✓ answered by ADezii

  1. Declare Variables to hold the Original and Modified Record Sources
    Expand|Select|Wrap|Line Numbers
    1. Dim strOriginalRecordSource As String
    2. Dim strNewRecordSource As String
  2. Retrieve the Original Record Source of the Form
    Expand|Select|Wrap|Line Numbers
    1. strOriginalRecordSource = Me.RecordSource
  3. Create a New Record Source based on the Primary Key displayed on the Form
    Expand|Select|Wrap|Line Numbers
    1. strNewRecordSource = "SELECT * FROM tblEmployees WHERE [ID] = " & Me![ID]
  4. Apply the Filtered Record Source to the Form
    Expand|Select|Wrap|Line Numbers
    1. Me.RecordSource = strNewRecordSource
  5. Output the Single, Form Record
    Expand|Select|Wrap|Line Numbers
    1. DoCmd.OutputTo acOutputForm, "frmInputView", acFormatXLS, "filename.xls", False
  6. Restore the Original Record Source
    Expand|Select|Wrap|Line Numbers
    1. Me.RecordSource = strOriginalRecordSource
  7. Complete Code with Comments stripped:
    Expand|Select|Wrap|Line Numbers
    1. Dim strOriginalRecordSource As String
    2. Dim strNewRecordSource As String
    3.  
    4. strOriginalRecordSource = Me.RecordSource
    5. strNewRecordSource = "SELECT * FROM tblEmployees WHERE [ID] = " & Me![ID]
    6.  
    7. Me.RecordSource = strNewRecordSource
    8.  
    9. DoCmd.OutputTo acOutputForm, "frmInputView", acFormatXLS, "filename.xls", False
    10.  
    11. Me.RecordSource = strOriginalRecordSource

2 12312
ADezii
8,834 Expert 8TB
Just subscribing for now...will return with an idea later.
Mar 5 '11 #2
ADezii
8,834 Expert 8TB
  1. Declare Variables to hold the Original and Modified Record Sources
    Expand|Select|Wrap|Line Numbers
    1. Dim strOriginalRecordSource As String
    2. Dim strNewRecordSource As String
  2. Retrieve the Original Record Source of the Form
    Expand|Select|Wrap|Line Numbers
    1. strOriginalRecordSource = Me.RecordSource
  3. Create a New Record Source based on the Primary Key displayed on the Form
    Expand|Select|Wrap|Line Numbers
    1. strNewRecordSource = "SELECT * FROM tblEmployees WHERE [ID] = " & Me![ID]
  4. Apply the Filtered Record Source to the Form
    Expand|Select|Wrap|Line Numbers
    1. Me.RecordSource = strNewRecordSource
  5. Output the Single, Form Record
    Expand|Select|Wrap|Line Numbers
    1. DoCmd.OutputTo acOutputForm, "frmInputView", acFormatXLS, "filename.xls", False
  6. Restore the Original Record Source
    Expand|Select|Wrap|Line Numbers
    1. Me.RecordSource = strOriginalRecordSource
  7. Complete Code with Comments stripped:
    Expand|Select|Wrap|Line Numbers
    1. Dim strOriginalRecordSource As String
    2. Dim strNewRecordSource As String
    3.  
    4. strOriginalRecordSource = Me.RecordSource
    5. strNewRecordSource = "SELECT * FROM tblEmployees WHERE [ID] = " & Me![ID]
    6.  
    7. Me.RecordSource = strNewRecordSource
    8.  
    9. DoCmd.OutputTo acOutputForm, "frmInputView", acFormatXLS, "filename.xls", False
    10.  
    11. Me.RecordSource = strOriginalRecordSource
Mar 5 '11 #3

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

Similar topics

1
by: Tim Graichen | last post by:
I currently populate a form based on a query. 1)How do I create a report that displays only the current record's fields and not all records in the table? Thanks Tim G
4
by: DBQueen | last post by:
I have a subform which is in Continuous Forms view. I have added a button to the bottom of the page to move to the next record using the button wizard (result: DoCmd.GoToRecord , , acNext). I...
2
by: OSMeier | last post by:
Hi Everyone, I am hoping you can help me. I am using MS Access 2000 for this. Here is the scenario: I have two tables: tblMain -------- ID (Primary Key)
0
by: user_5701 | last post by:
I have an Access 97 database front end (with some lookup tables there too) which also has a MS SQL Server 2000 back end, where all the rest of the larger tables are. I recently had some problems...
1
by: David | last post by:
Hi, I have a continuous form. For each record I have a field 'HeldDate' (Text Field from a table) Against each record I have a button which sets the visibility of this text box to 'True' and...
0
by: jennywhatley | last post by:
Hello I am using the following code to try to get a report to show a single record by clicking a command button in a sub form (perhaps it doesnt work because its a sub form not a form?) The...
1
by: Ryan | last post by:
Hello. I was hoping that someone may be able to assist with an issue that I am experiencing. I have created an Access DB which imports an Excel File with a particular layout and field naming. ...
0
by: Andy | last post by:
Hi ! I have a form that displays several records. The record contains a bound object frame. Onclick I execute some simple code to change teh object's appearance then I requery. The trouble...
2
easydoesit
by: easydoesit | last post by:
Hello all, I am looking for a way to be able to enter data into fields on a form, then be able to e-mail a report that shows only that record. This is what I have thus far: At the end of my...
2
by: jmartmem | last post by:
Greetings, I have several Access 2007 reports that I regularly export to individual PDF flat files on a web server. I've successfuly created a module sub to export the reports using the...
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: 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
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...
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...

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.