Form that will create a cross tab query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • govind73
    New Member
    • Jun 2009
    • 8

    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!
    Code:
    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];
    Last edited by NeoPa; Jun 11 '09, 01:43 PM. Reason: Please use the [CODE] tags provided.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32668

    #2
    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.

    Comment

    • govind73
      New Member
      • Jun 2009
      • 8

      #3
      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:
      Code:
      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.

      Comment

      • govind73
        New Member
        • Jun 2009
        • 8

        #4
        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 :-)

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32668

          #5
          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 :
          Code:
          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)

          Comment

          • govind73
            New Member
            • Jun 2009
            • 8

            #6
            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:

            Code:
            strSQL = Replace(strSQL, "%X", crosstabs.cboX)
            strSQL = Replace(strSQL, "%Y", crosstabs.cboY)
            right? If so, this gives a run-time error '424', object required.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32668

              #7
              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 :)

              Comment

              • govind73
                New Member
                • Jun 2009
                • 8

                #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!!

                Code:
                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

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32668

                  #9
                  That's purely and simply due to muppetitis. I seem to be suffering from it today :(

                  The code should have been Forms("CrossTabs").c boX etc.

                  Comment

                  • govind73
                    New Member
                    • Jun 2009
                    • 8

                    #10
                    Do I need a

                    Code:
                    DoCmd.RunSQL
                    type expression to actually execute this. If so, how would I insert it in that code?

                    Thanks!!

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32668

                      #11
                      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.

                      Comment

                      • govind73
                        New Member
                        • Jun 2009
                        • 8

                        #12
                        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.

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32668

                          #13
                          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.

                          Comment

                          • govind73
                            New Member
                            • Jun 2009
                            • 8

                            #14
                            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.

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32668

                              #15
                              I have a stub query (called qryStub) consisting of the following SQL :
                              Code:
                              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 :
                              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

                              Comment

                              Working...