473,385 Members | 1,769 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,385 software developers and data experts.

Form that will create a cross tab query

Hi all,

I would like help on the following problem. I would like to create a form that will have two drop downs (combo or list box), one of which will be a row and the other will be a column. The column values will be a count based on each row value. I've reproduced an example from the Northwind data below. Basically, I would like someone to be able to select one variable (field) and then another to compare and hit SUBMIT and generate a report based on a cross tab query. Any assistance is much appreciated!
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Count(Products.[Target Level]) AS [CountOfTarget Level]
  2. SELECT Products.[Supplier IDs].[Value], Count(Products.[Target Level]) AS [Total Of Target Level]
  3. FROM Products
  4. GROUP BY Products.[Supplier IDs].[Value]
  5. PIVOT Products.[Target Level];
Jun 11 '09 #1
14 3398
NeoPa
32,556 Expert Mod 16PB
Where in this example SQL are the two values that you want to select from the ComboBox controls?

I'm afraid you haven't explained the problem very well so it's hard to help.
Jun 11 '09 #2
I read what I wrote and perhaps I didn't make it too clear. I would like to create a form where I can provide a list of fields. The user will select one field, say X which is how the information is sorted and then can select another field in another drop down, say Y that would be the column headings for a cross tab query. That Y will also be the calculated count for each column and row intersection. Hence:
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Count(dataset.[Y]) AS CountOfY
  2. SELECT dataset.[X], Count(dataset.[Y]) AS [Total Of Y]
  3. FROM dataset
  4. GROUP BY dataset.[X]
  5. PIVOT dataset.[Y];
I'm very comfortable using Access, but I cannot for the life of me figure out how to produce a list of fields within a form.
Jun 11 '09 #3
X is one of X1...Xi in a list of fields
Y is one of Y1...Yi in a second list of fields

Depending on what the user chooses, the particular X and Y will then be filled into the SQL, creating a crosstab query. I might note that the SQL in the example is not as important to me as trying to figure out how to insert a field name into X and Y based on input from a form.

Hope I'm making sense :-)
Jun 11 '09 #4
NeoPa
32,556 Expert Mod 16PB
Starting to (but remember the [ CODE ] tags if you will).

Is the CrossTab query to produced in a VBA string variable, or did you have any other usage for it?

I'll lay out the sort of thing you need for that first and if you need something different just say.

Assuming a form [YourForm] with ComboBox controls [cboX] & [cboY] then :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As string
  2.  
  3. strSQL = "TRANSFORM Count([%Y]) " & _
  4.          "AS [CountOf%Y] " & _
  5.          "SELECT [%X]," & _
  6.          "Count([%Y]) AS [TotalOf%Y] " & _
  7.          "FROM [Dataset] " & _
  8.          "GROUP BY [%X] " & _
  9.          "PIVOT [%Y]"
  10. strSQL = Replace(strSQL, "%X", Me.cboX)
  11. strSQL = Replace(strSQL, "%Y", Me.cboY)
Jun 11 '09 #5
That is super helpful. The cross tab query has no other use, so using it in VBA will be fine, but...

The only other thing that I am struggling with is getting the values in the combo box into query. So, I have a form called [crosstabs] and two combo boxes called [cboX] and [cboY], so the last two lines ought to be:

Expand|Select|Wrap|Line Numbers
  1. strSQL = Replace(strSQL, "%X", crosstabs.cboX)
  2. strSQL = Replace(strSQL, "%Y", crosstabs.cboY)
right? If so, this gives a run-time error '424', object required.
Jun 11 '09 #6
NeoPa
32,556 Expert Mod 16PB
If the code is running from within [CrossTabs] then Me.cboX & Me.cboY will work.

Otherwise, assuming that the form is actually open, use Form("CrossTabs").cboX & Form("CrossTabs").cboX.

Good luck :)
Jun 11 '09 #7
If I use Me.cboX & Me.cboY, it doesn't do anything. But, when I use Form("crosstabs").cboX & Form("crosstabs").cboX, it produces the error below:

Run-time error '2465':
Microsoft Office Access can't find the field 'crosstabs' referred to in your expression.

This is from a command button. Here is the full code of the command button. Thanks again for your helpful assistance!!

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command9_Click()
  2.  
  3. Dim strSQL As String
  4.  
  5. strSQL = "TRANSFORM Count([%Y]) " & _
  6.           "AS [CountOf%Y] " & _
  7.           "SELECT [%X]," & _
  8.           "Count([%Y]) AS [TotalOf%Y] " & _
  9.           "FROM [dataset] " & _
  10.           "GROUP BY [%X] " & _
  11.           "PIVOT [%Y]"
  12. strSQL = Replace(strSQL, "%X", Form("crosstabs").cboX)
  13. strSQL = Replace(strSQL, "%Y", Form("crosstabs").cboY)
  14.  
  15.  
  16. End Sub
Jun 11 '09 #8
NeoPa
32,556 Expert Mod 16PB
That's purely and simply due to muppetitis. I seem to be suffering from it today :(

The code should have been Forms("CrossTabs").cboX etc.
Jun 11 '09 #9
Do I need a

Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL
type expression to actually execute this. If so, how would I insert it in that code?

Thanks!!
Jun 11 '09 #10
NeoPa
32,556 Expert Mod 16PB
No. That is only for action queries.

It's hard to know what to suggest for you without knowing what you're hoping to achieve.
Jun 11 '09 #11
well, at the moment, when i click on the command button based on the help i've received from you, it does nothing (no error either so i think that issue was resolved). what i really want is to be able to generate the cross tab query as a subform (or subreport) below the form. but, i'd be happy just for the crosstab query to pop up.
Jun 11 '09 #12
NeoPa
32,556 Expert Mod 16PB
A subform makes sense, except I don't know if you can, or if it would make practical sense at least to, bind a form to a Cross-Tab query.

Assuming this is possible though, you could set the .RecordSource property of the subform in the code. This makes more sense to do within code in the module for the main form of course, but can work from outside as you seem to have it now if that's what's required.

Changing the design of a query before opening it is not a good project design. Otherwise you could create a temporary query using the SQL prepared.
Jun 11 '09 #13
Well-- all I really want is get the crosstab query to actually open in some form, but the vba code provided doesn't actually do anything so I'm trying to find a way to actually open the query results from the vba code that was provided earlier. However it can be done is fine with me, but I cannot get it to open.
Jun 11 '09 #14
NeoPa
32,556 Expert Mod 16PB
I have a stub query (called qryStub) consisting of the following SQL :
Expand|Select|Wrap|Line Numbers
  1. SELECT 'An error has occurred in this process.  Please get the design checked by {Your name here} (IT).' AS Message
This is used in the following VBA code :
Expand|Select|Wrap|Line Numbers
  1. 'TempQuery creates a temp QueryDef, or a new one to be deleted later.
  2. Public Function TempQuery(ByVal strName As String, strSQL As String) As QueryDef
  3.     'Create unique name (using date/time & user) so it never clashes
  4.     strName = IIf(Left(strName, 3) = "qry", "", "qry") & _
  5.               strName & _
  6.               Format(Now(), "YYYYMMDDHHNNSS") & _
  7.               Replace(GetUser(), ".", "")
  8.     Call DoCmd.CopyObject(NewName:=strName, _
  9.                           SourceObjectType:=acQuery, _
  10.                           SourceObjectName:="qryStub")
  11.     Set TempQuery = CurrentDb.QueryDefs(strName)
  12.     TempQuery.SQL = strSQL
  13. End Function
Jun 11 '09 #15

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

Similar topics

3
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my...
25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
14
by: alwayshouston | last post by:
Hi All! I am working on this very small database and I am confused in the designing a simple form. I only have three tables in the database. First Table: tblExpense Columns: ExpenseID ;...
1
by: hmoulding | last post by:
I'm not talking about a bound control. That's something I've already figured out. I have a form that I use to add and update items in a table. Each item can be of one or more types, so there's a...
0
by: badboybrown | last post by:
I'm sorry to ask this, but I just can't figure it out and it's probably because it's not possible. I have a Cross-tab query that shows me the sum of items received by brand(Columns) for every...
2
by: fstenoughsnoopy | last post by:
I have a customer order database and I need to pull a customers information, ie first name, last name, address, city, state, zip, phone, etc, into the oder table. i don't know how to go about...
5
by: superjacent | last post by:
Hope someone can help. I have a saved parent form containing an unbound sub-form. I set the SourceObject (form) after the Parent Form opens/loads. The sub-form (datasheet view) basically...
13
by: salad | last post by:
Operating in A97. I didn't receive much of a response conserning Pivot tables in Access. Pivot tables are nice, but a CrossTab will work for me too. Using a Pivot table, one is actually...
6
by: Rosie | last post by:
Hi -- I have a main form attached to a table (Invoice header info). When the form loads I use acNewRec to take us to the end of a table, and I get a clean, empty form. The user, who knows...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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...

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.