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

Home Posts Topics Members FAQ

How to Build and export a crosstab query in VBA?

31 New Member
Hi all,

What I'm trying to do is create a crosstab query (based on a temp table) in VBA, then export it to Excel. The temp table is built off a form where the user enters a parameter(s) in a textbox(es).

I created the crosstab query using the wizard and copied the code into my VBA (button click), but it's not an action query, so DoCmd.RunSQL won't work.

Then I tried:
Expand|Select|Wrap|Line Numbers
  1. Dim rst as Recordset
  2. Set rst = CurrentDb.OpenRecordset(strSQL)
But where do I go from there?

I guess, really, I don't need to put it into a table if I'm just going to export it to Excel anyway, and it need not be a crosstab query: How do I create a SQL SELECT statement in VBA and display the results?
Nov 12 '10 #1
4 8311
tetsuo2030
31 New Member
Guys, I got it:

Built a bogus query called "qry_index" (one field from one table).

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2.     Dim qdf As DAO.QueryDef
  3.     Dim As String
  4.     Dim rst As Recordset
  5.  
  6.     Set db = CurrentDb
  7.     Set qdf = db.QueryDefs("qry_index")
  8.     strSQL = "TRANSFORM.....;"
  9.     qdf.SQL = strSQL
  10.     DoCmd.OpenQuery "qry_index"
  11.     Set qdf = Nothing
  12.     Set db = Nothing
Nov 12 '10 #2
tetsuo2030
31 New Member
I LOVE this stuff!
Nov 12 '10 #3
mwgass
1 New Member
This was quite helpful. Thanks!
Aug 21 '12 #4
twinnyfo
3,653 Recognized Expert Moderator Specialist
tetsuo2030,

If you already have the query built (which you can build it in the query builder to reference the controls on your form), there is also a much more easier way to export to Excel:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferSpreadsheet acExport, , "qry_index", "ExcelPathAndFileName", True, "ExcelWorksheetName"
  2.  
Aug 22 '12 #5

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

Similar topics

8
7560
by: Donna Sabol | last post by:
First, I should start by saying I am creating a database to be used by some very impatient, non-computer literate people. It needs to be seameless in it's operation from their point of view. I want them to do as little as possible when they run their reports. I have a crosstab query that displays usage of items for each month. It looks...
0
1285
by: zilvia | last post by:
I know this question has already been asked to this group, but I haven't understood the answers... I need to build a crosstab query that results in more than 255 columns. I cannot switch rows and columns because rows are even more. I have read that the 255 limit is only for display, not for the actual creation of the query, so I should be...
1
1618
by: Richard Hollenbeck | last post by:
I'm building a table of grades report based on a crosstab query where students are in the rows and the activities are in the columns. The grades are in the body where the rows and columns intersect. The problem is that the activities will change from semester to semester. Will I need to redesign a new report every semester to reflect the...
4
8114
by: Judy | last post by:
I'm using Access 2003 and was wondering if it is possible to have a paramater selection within a crosstab query so that I wouldn't need to build a new table. I have a select query that I'm using to build the crosstab query from. In the select query I'm prompting for a specific quarter but when I go to run the crosstab it doesn't seem to...
3
3412
by: Bryan | last post by:
I've been looking over this newsgroup, but I can't find an answer to my problem. I see that a few others have posted this issue over the years, but none of them got a response. I'll give it another shot. I have a report that is based on a crosstab query. The crosstab query is based off of another query, and that has a parameter in the...
14
3480
by: Tina | last post by:
My employer tracks productivity/performance of clinicians (how much they bill) each week, its averages for the month, and the 6 months. These averages are compared to their expected productivity. However, the expectation changes - it may be 60% for a while, then change to 50%. Initially, I was averaging the expectation, along with the...
2
3345
by: Mike | last post by:
Hi everyone, I found the Access reports too limited to do what i wanted so i created a module to export a crosstab query to an excel file, and then i modify it as i want. My problem is that i created a unique query for every "sector", saved them, and select the right one according to the user's choice. What i'd like to do is to create...
4
6465
by: m.wanstall | last post by:
I have a crosstab query that compiles data for Months of the year. I have a stacked select query on top of that crosstab query that uses the latest 2 months data and exports it to a fixed length flat file. Ideally I would like to be able to just select the Last 2 Columns of the Crosstab query as inputs to the Select query WITHOUT having to go...
4
36198
by: Newbie12345 | last post by:
I have a crosstab query which gathers data from a regular table and creates columns for the months in the year with volume for each month. I want to be able export/send the data to a brand new table, which would keep the new formating with the months as fields. So basicall i want to create a table from that crosstab query that looks identical...
2
3395
by: Jim Devenish | last post by:
I wish to create a crosstab query as the record source for a report. It needs to count data between selected dates which are entered by the user in a popup window. The following Select query works: SELECT Tasks.EnquirySourceID, Tasks.BusinessUnitID, Count(Tasks.TaskID) AS CountOfTaskID FROM Tasks WHERE (((Tasks.TaskDate)>=!!)) GROUP BY...
0
7843
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...
0
8206
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. ...
0
8220
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...
1
5713
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...
0
5392
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
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...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.