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! - TRANSFORM Count(Products.[Target Level]) AS [CountOfTarget Level]
-
SELECT Products.[Supplier IDs].[Value], Count(Products.[Target Level]) AS [Total Of Target Level]
-
FROM Products
-
GROUP BY Products.[Supplier IDs].[Value]
-
PIVOT Products.[Target Level];
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.
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: - TRANSFORM Count(dataset.[Y]) AS CountOfY
-
SELECT dataset.[X], Count(dataset.[Y]) AS [Total Of Y]
-
FROM dataset
-
GROUP BY dataset.[X]
-
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.
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 :-)
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 : - Dim strSQL As string
-
-
strSQL = "TRANSFORM Count([%Y]) " & _
-
"AS [CountOf%Y] " & _
-
"SELECT [%X]," & _
-
"Count([%Y]) AS [TotalOf%Y] " & _
-
"FROM [Dataset] " & _
-
"GROUP BY [%X] " & _
-
"PIVOT [%Y]"
-
strSQL = Replace(strSQL, "%X", Me.cboX)
-
strSQL = Replace(strSQL, "%Y", Me.cboY)
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: - strSQL = Replace(strSQL, "%X", crosstabs.cboX)
-
strSQL = Replace(strSQL, "%Y", crosstabs.cboY)
right? If so, this gives a run-time error '424', object required.
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 :)
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!! - Private Sub Command9_Click()
-
-
Dim strSQL As String
-
-
strSQL = "TRANSFORM Count([%Y]) " & _
-
"AS [CountOf%Y] " & _
-
"SELECT [%X]," & _
-
"Count([%Y]) AS [TotalOf%Y] " & _
-
"FROM [dataset] " & _
-
"GROUP BY [%X] " & _
-
"PIVOT [%Y]"
-
strSQL = Replace(strSQL, "%X", Form("crosstabs").cboX)
-
strSQL = Replace(strSQL, "%Y", Form("crosstabs").cboY)
-
-
-
End Sub
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.
Do I need a
type expression to actually execute this. If so, how would I insert it in that code?
Thanks!!
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.
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.
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.
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.
NeoPa 32,556
Expert Mod 16PB
I have a stub query (called qryStub) consisting of the following SQL : - 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 : -
'TempQuery creates a temp QueryDef, or a new one to be deleted later.
-
Public Function TempQuery(ByVal strName As String, strSQL As String) As QueryDef
-
'Create unique name (using date/time & user) so it never clashes
-
strName = IIf(Left(strName, 3) = "qry", "", "qry") & _
-
strName & _
-
Format(Now(), "YYYYMMDDHHNNSS") & _
-
Replace(GetUser(), ".", "")
-
Call DoCmd.CopyObject(NewName:=strName, _
-
SourceObjectType:=acQuery, _
-
SourceObjectName:="qryStub")
-
Set TempQuery = CurrentDb.QueryDefs(strName)
-
TempQuery.SQL = strSQL
-
End Function
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 ;...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
| |