By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,479 Members | 1,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,479 IT Pros & Developers. It's quick & easy.

Form that will create a cross tab query

P: 8
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
Share this Question
Share on Google+
14 Replies

Expert Mod 15k+
P: 31,709
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

P: 8
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

P: 8
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

Expert Mod 15k+
P: 31,709
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
  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

P: 8
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

Expert Mod 15k+
P: 31,709
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

P: 8
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()
  3. Dim strSQL As String
  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)
  16. End Sub
Jun 11 '09 #8

Expert Mod 15k+
P: 31,709
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

P: 8
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?

Jun 11 '09 #10

Expert Mod 15k+
P: 31,709
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

P: 8
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

Expert Mod 15k+
P: 31,709
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

P: 8
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

Expert Mod 15k+
P: 31,709
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

Post your reply

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