473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using DoCmd.OutputTo to Export Query of the Current Record Only

3 New Member
I'm trying to use a few posts from this forum, including use DoCmd.OutputTo to export a PDF of the current record on a report? to send a single record displayed in a form of my potential client data to a text file to use with Outlook and QuickBooks. I have a button on the form and the following code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Export_Potential_Client_Data_Click()
  2.     On Error GoTo Err_Export_Potential_Client_Data_Click
  3.     Dim stDocName As String
  4.     Dim strOriginalRecordSource As String
  5.     Dim strNewRecordSource As String
  6.  
  7.     stDocName = "Export Potential Client"
  8.     strOriginalRecordSource = Me.RecordSource
  9.     strNewRecordSource = "SELECT * FROM [Potential Clients] WHERE [Number] = " & Me![Number]
  10.     Me.RecordSource = strNewRecordSource
  11.     DoCmd.OutputTo ObjectType:=acOutputReport, ObjectName:="Export Potential Client Report", OutputFormat:=acFormatTXT, Outputfile:="C:\Clients\Client Data.csv", AutoStart:=True
  12.     Me.RecordSource = strOriginalRecordSource
I've spent hours playing with it, but at this point it runs and opens an Excel spreadsheet (I'll turn the AutoStart off once I get everything up and running) with ALL the records displayed. I cannot get it to export the current record only!

I'm sure its something simple, but I can't seem to figure it out. Any ideas?

Thanks

Chip Stewart
Aug 23 '18 #1
9 4121
NeoPa
32,556 Recognized Expert Moderator MVP
Hi Chip.

You're confusing the currently open version of your Report object with the one referred to in DoCmd.OutputTo().

One is open already. This is simply an instance, or copy, of the original design held as [Export Potential Client Report]. The other is a simple reference to the design itself - NOT your open instance.

So, what you see on the screen is affected by your changing its RecordSource. The one that you want DoCmd.OutputTo() to sned as a file is certainly not.

Let's get back to a simpler approach and remember that the Report object is based off a table or query and that's the data you want saved - not anything in the report itself - just the data. That's best done using the following template code :
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.TransferText(TransferType:=acExportDelim _
  2.                       , TableName:={Name of table or query} _
  3.                       , FileName:={Name of file to hold data})
You'll have noticed that this doesn't filter the results for you any more than trying to do it by Report did.

I don't think there's an easy way to do that in code but you can create a Query (QueryDef) that filters based on a value stored in one of those database variables whose name I just can't dig up ATM.

You can also create a new QueryDef in code if you need to but I'll get to that another time.
Aug 23 '18 #2
zmbd
5,501 Recognized Expert Moderator Expert
I agree with NeoPa on the method for simple text-file export; however, it begs asking: is there something within the report that is important to have in the text-file?

A parameter based query using either the open form or a tempvars.collection might be another route to take as the underlying recordset for the report would be "filtered" based on the passed value.
Aug 24 '18 #3
NeoPa
32,556 Recognized Expert Moderator MVP
That's the word I was looking for - TempVars!!!
Aug 24 '18 #4
zmbd
5,501 Recognized Expert Moderator Expert
TempVars - Wonderful little creatures!
The 2010-navigation-control has a few quirks, on of which is that clicking on one of the navigation-command-buttons (NCB)reloads the associated form from scratch into the navigation-subform-control and discards everything from the previous form so passing information becomes a bit more combersom. Even the WHERE property of the call NCB isn't always appropriate or robust enough - especially if the value needs to be passed several times - the tempvars collection works like a charm and is error-resistant which allows me to use my new/old favorite method err.raise().

However, me thinks to go farther would be off topic ;)
Aug 24 '18 #5
ChipStewart
3 New Member
I'm afraid I'm a bit lost here - could you dumb it down? I'm just starting out with this stuff. Any good resources you could recommend to get up to speed fast?

Thanks.
Aug 24 '18 #6
NeoPa
32,556 Recognized Expert Moderator MVP
Hi Chip.

You may find TempVars Object (Access) helpful to work with. I'd say ignore any use of macros unless you like problems.

TempVars is essentially a collection of values that can be referenced by name from within your code as well as from within SQL and The Expression Service. IE. pretty well anywhere from within your project. Their scope is global so you needn't worry about where they're set and where used. Very simple.

Once you've set a value in there and given it a name you can reference it anywhere until you close your project or set it to a different value. A reference consists very simply of saying :
Expand|Select|Wrap|Line Numbers
  1. TempVars!{ValueName}
Where {ValueName} is the name you've assigned to that value.

It's very handy for just the sort of work you're doing where you don't have a good way of filtering a QueryDef, for instance.

PS. It's available only from Access 2007 and beyond as it was only introduced then.
Aug 24 '18 #7
ChipStewart
3 New Member
Okay . . . Here's what I have now:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Export_Client_Data_Click()
  2.  
  3. On Error GoTo Err_Export_Client_Click
  4.  
  5.     Dim stDocName As String
  6.     stDocName = "Export_Client_Data"
  7.     DoCmd.OpenQuery stDocName, acNormal, acEdit    
  8.     DoCmd.OutputTo ObjectType:=acOutputTable, ObjectName:="Client_Data", Outputfile:="c:\Clients\Client Data.xlsx", AutoStart:=True
  9.  
  10. Exit_Export_Client_Click:
  11.     Exit Sub
  12.  
  13. Err_Export_Client_Click:
  14.     MsgBox Err.Description
  15.     Resume Exit_Export_Client_Click
  16.  
  17. End Sub
  18.  

I have it opening the new table and exporting until I can get the exporting right. The new table is exactly what I want, but when it opens in Excel, all I get are the headers without the current record.

I'm still trying to figure out how to use TempVars here. Any suggestions?
Sep 18 '18 #8
NeoPa
32,556 Recognized Expert Moderator MVP
I'm away for a few days. I'll check in again next week once I've caught up with my work backlog.
Sep 21 '18 #9
NeoPa
32,556 Recognized Expert Moderator MVP
Hi Chip.

Holler if you're still interested in assistance on this one. My absence has been extended but I'll keep it in my list for when I'm back and more available if you LMK you're still active with this question.
Oct 8 '18 #10

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
1
by: Michael Israel | last post by:
I am new to Access. I have a form with a single record. The keyed field is PropID (number). I want to run a query from the form that will have the current value of PropID be the variable in the...
1
by: simonmarkjones | last post by:
Hi can someone please help me with a query. I would like to view only the current record with a query so i can create a report to be used as a customer receipt. How can i do this? Many...
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...
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...
4
by: Busbait | last post by:
Hi, I have the following code which works fine in MS Access 2007. It export current access form records to an excel sheet: DoCmd.OutputTo acOutputQuery, "RunQuery", acFormatXLSX, , True Is...
2
by: Tim F | last post by:
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...
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
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...
1
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.