473,588 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export search results from Form to CSV (macro)

9 New Member
I have a search form that returns records to another form, rather than a table. Here are my questions:

1) Is it possible to export the form results (as opposed to query results) to CSV? I've been successful at exporting a table of results before, but not a form.

2) If so, can this be done via macro?

3) If a macro is possible, can the macro further allow the user to choose location and filename when exporting?

I've tried a straightforward approach of pointing the macro to the form, but it appears that the macro only wants a table or query. My guess is that I will have to build a further query 'behind the scenes' in order to export...

Thanks,

-Scott
Feb 18 '07 #1
13 6277
ADezii
8,834 Recognized Expert Expert
I have a search form that returns records to another form, rather than a table. Here are my questions:

1) Is it possible to export the form results (as opposed to query results) to CSV? I've been successful at exporting a table of results before, but not a form.

2) If so, can this be done via macro?

3) If a macro is possible, can the macro further allow the user to choose location and filename when exporting?

I've tried a straightforward approach of pointing the macro to the form, but it appears that the macro only wants a table or query. My guess is that I will have to build a further query 'behind the scenes' in order to export...

Thanks,

-Scott
1. Form results cannot be transfered to a CSV File. In order to Export data to a CSV File, you use the TransferText() Method and this Method requires either a Table or Query Argument, not an SQL Statement. You must first create a query and then specify the name of the query in the Table Name argument. All is not lost however, because if your Form results are generated by changing the RecordSource of the Form, you can programmaticall y create the CSV File by the following code which creates a Recordset based on the Form's RecordSource and writes selected Fields in a CSV Format to CSV.txt in the Test Directory. Field names are my own for testing purposes.
Expand|Select|Wrap|Line Numbers
  1. Dim MySQL As String, MyDB As Database, MyRS As Recordset
  2.  
  3. MySQL = Me.RecordSource
  4.  
  5. Open "C:\Test\CSV.txt" For Output As #1
  6.  
  7. Set MyDB = CurrentDb()
  8. Set MyRS = MyDB.OpenRecordset(MySQL, dbOpenDynaset)
  9.   Do While Not MyRS.EOF
  10.     Write #1, MyRS![EmployeeID], MyRS![FirstName], MyRS![LastName]
  11.     MyRS.MoveNext
  12.   Loop
  13. MsgBox MyRS.RecordCount
  14.  
  15. Close #1
  16. MyRS.Close
If you are further interested, please let me know. I'm sure the other Experts/ Moderators will come up with other solutions, probably better. Good Luck!
Feb 19 '07 #2
MMcCarthy
14,534 Recognized Expert Moderator MVP
Actually, that is an excellent solution, one variation I would try (borrowing ADezii's code) is ...

Expand|Select|Wrap|Line Numbers
  1. Dim MyRS As Recordset
  2.  
  3. Open "C:\Test\CSV.txt" For Output As #1
  4.  
  5. Set MyRS = Me.RecordsetClone
  6. MyRS.MoveFirst
  7. Do While Not MyRS.EOF
  8.     Write #1, MyRS![EmployeeID], MyRS![FirstName], MyRS![LastName]
  9.     MyRS.MoveNext
  10.   Loop
  11. MsgBox MyRS.RecordCount
  12.  
  13. Close #1
  14. MyRS.Close
I'm not sure if RecordsetClone will return the filtered records but I think it will.

Mary
Feb 19 '07 #3
ADezii
8,834 Recognized Expert Expert
Actually, that is an excellent solution, one variation I would try (borrowing ADezii's code) is ...

Expand|Select|Wrap|Line Numbers
  1. MyRS As Recordset
  2.  
  3. Open "C:\Test\CSV.txt" For Output As #1
  4.  
  5. Set MyRS = Me.RecordsetClone
  6. MyRS.MoveFirst
  7. Do While Not MyRS.EOF
  8.     Write #1, MyRS![EmployeeID], MyRS![FirstName], MyRS![LastName]
  9.     MyRS.MoveNext
  10.   Loop
  11. MsgBox MyRS.RecordCount
  12.  
  13. Close #1
  14. MyRS.Close
I'm not sure if RecordsetClone will return the filtered records but I think it will.

Mary
You were right on with the RecordsetClone approach, thanks. No sense playing around with the original, when you can use a copy!
Feb 19 '07 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
You were right on with the RecordsetClone approach, thanks. No sense playing around with the original, when you can use a copy!
LOL - My thoughts exactly.
Feb 19 '07 #5
lightning
9 New Member
Trying Mary's mods, I am getting an error: File Already Open. The code is attached to a button on the results form.

I've checked to see if the CSV file actually is open, but it ain't. This happens even after closing and reopening the db. I've also tried changing the location and just deleting the file, but that results in an Object Required notice as expected.

I also have a problem with the As Database declaration throwing another error about invalid user-defined type. Commenting it out yields the above results. Did I misunderstand how to declare that variable?
Feb 19 '07 #6
MMcCarthy
14,534 Recognized Expert Moderator MVP
Trying Mary's mods, I am getting an error: File Already Open. The code is attached to a button on the results form.

I've checked to see if the CSV file actually is open, but it ain't. This happens even after closing and reopening the db. I've also tried changing the location and just deleting the file, but that results in an Object Required notice as expected.

I also have a problem with the As Database declaration throwing another error about invalid user-defined type. Commenting it out yields the above results. Did I misunderstand how to declare that variable?
It's not included in my mods

try adding this line of code to the beginning of the code before the 'Open' statement.

Expand|Select|Wrap|Line Numbers
  1. Close #1
Feb 19 '07 #7
lightning
9 New Member
Sorry Mary - my response implied incorrectly that it was part of your example. I discovered it wasn't appropriate to the code, so have since removed it.

I've also tried using Close in the beginning of the code, but that now gives a Type Mismatch error. I guess that means there's something wrong with the variable declaration, but I don't see how using RecordsetClone would violate strict typing. Here's my current code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPrintToCSV_Click()
  2. On Error GoTo Err_cmdPrintToCSV_Click
  3.  
  4. Dim MyRS As Recordset
  5.  
  6. Close #1
  7.  
  8. Open "e:\Documents and Settings\profile\Desktop\CSV.txt" For Output As #1
  9.  
  10. Set MyRS = Me.RecordsetClone
  11. MyRS.MoveFirst
  12.   Do While Not MyRS.EOF
  13.     Write #1, MyRS![cardTitle]
  14.     MyRS.MoveNext
  15.   Loop
  16. MsgBox MyRS.RecordCount
  17.  
  18. Close #1
  19. MyRS.Close
  20.  
  21. Exit_cmdPrintToCSV_Click:
  22.     Exit Sub
  23.  
  24. Err_cmdPrintToCSV_Click:
  25.     MsgBox Err.Description
  26.     Resume Exit_cmdPrintToCSV_Click
  27.  
  28. End Sub
  29.  
Note that I am currently only trying to record one element from the form, [cardTitle]. Eventually, there will be about 10 elements.
Feb 19 '07 #8
MMcCarthy
14,534 Recognized Expert Moderator MVP
Sorry Mary - my response implied incorrectly that it was part of your example. I discovered it wasn't appropriate to the code, so have since removed it.

I've also tried using Close in the beginning of the code, but that now gives a Type Mismatch error. I guess that means there's something wrong with the variable declaration, but I don't see how using RecordsetClone would violate strict typing. Here's my current code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPrintToCSV_Click()
  2. On Error GoTo Err_cmdPrintToCSV_Click
  3.  
  4. Dim MyRS As Recordset
  5.  
  6. Close #1
  7.  
  8. Open "e:\Documents and Settings\profile\Desktop\CSV.txt" For Output As #1
  9.  
  10. Set MyRS = Me.RecordsetClone
  11. MyRS.MoveFirst
  12.   Do While Not MyRS.EOF
  13.     Write #1, MyRS![cardTitle]
  14.     MyRS.MoveNext
  15.   Loop
  16. MsgBox MyRS.RecordCount
  17.  
  18. Close #1
  19. MyRS.Close
  20.  
  21. Exit_cmdPrintToCSV_Click:
  22.     Exit Sub
  23.  
  24. Err_cmdPrintToCSV_Click:
  25.     MsgBox Err.Description
  26.     Resume Exit_cmdPrintToCSV_Click
  27.  
  28. End Sub
  29.  
Note that I am currently only trying to record one element from the form, [cardTitle]. Eventually, there will be about 10 elements.
The code is fine. At a guess I would say you have no DAO library ticked. In the VBA Editor window go to Tools - References and make sure there is a Microsoft DAO Library ticked on the list.

Mary
Feb 19 '07 #9
lightning
9 New Member
Thanks again, Mary, but still getting the type mismatch error. I did not have a DAO reference, so I tried each of the 3 available in turn. None gave me a different result. In desperation, I closed everything and recompiled, but of course that did nothing.

Could the error have something to do with my form or tables? Or is it most likely something in the VBA script?
Feb 19 '07 #10

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

Similar topics

5
11019
by: Tim Eliot | last post by:
Just wondering if anyone has hit the following issue and how you might have sorted it out. I am using the command: DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True after setting stDataSource and stFileName to the desired values. Most of the time it works, but occasionally, typically as code changes are being made to the module, the following message appears:
0
1523
by: Makuhari1998 | last post by:
Is it possible to create a macro that will open 7 reports at once and automatically export the results of each report into Excel? Can the results be exported into the same Excel workbook, or will they be in 7 different workbooks? Can I create a button on the main form of my database to run this macro? Also, is it possible to write a macro that can open several *queries* at once and export them into Excel? If so, will the data labels of...
7
8496
by: Jean | last post by:
Hello, I have a form that performs a search, according to criteria that a user enters into the text boxes. When the user clicks on „Search", a SQL string (say strSQL) is built up with the criteria. Then a list box RowSource property is set to this strSQL, to display the results of the search. StrSQL is a global variable within this Form's code module, i.e. in the class module.
8
3204
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled 'search' may be clicked on by the user and the user can then search all records by postcode. I want to do this to prevent duplicate data entry.
11
5629
by: Not Me | last post by:
Hi, I'm trying to export from a gridview control, to an excel file using code intended for a datagrid control (it's all over the web, can post if requested) I get the error. Control 'gvSearch' of type 'GridView' must be placed inside a form tag with runat=server.
9
10903
by: cassey14 | last post by:
Hi everyone! I really need your help.. I have a search form..And in that search form I have a subform..When they search, the results will display in the subform..I want to export to excel the result in the subform based on the query they search.. Thank you in advance -cassey
3
1822
by: robertoathome | last post by:
Hello, I successully adapted a search form from a microsoft example into my own db. MS Example I type search parameters in 2 boxes and the results are returned in a new, basic query window. I created: 2 text boxes 1 Command button ( search ) 1 Macro to open the query
2
4029
by: Marisol2 | last post by:
I have some queries in Access 2003 db that I have setup to display as pivots. I can go into design pivot table view and click on and then click on Export to Office MicroSoft Excel. The problem is I have about 6 queries that produce 48 reports (based on parameters). I don't want to manually save each pivot query results. The first macro I created to export the queries I used TransferSpreadsheet but the export results are regular xls...
7
5980
ollyb303
by: ollyb303 | last post by:
Hi, I am having a bit of a problem with TransferText macro. I am using TransferText, Export Delimited (no field names) to export the results of a query as a .csv file. The query is based on a linked table and uses a CDate() expression to convert the timestamp field (Nom_Date) from the table to a date type and the criteria: Between (Now()-183) And Now() to narrow results to the last 6 months. The query works just fine on its own and...
0
7929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7862
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8223
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6634
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5729
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3847
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.