473,385 Members | 1,355 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.

MS Access Crosstab query based on Listbox selection

66 64KB
Hi,

I am creating a reporting tool that will show two listbox in a form. Row and Column lists, user will select multiple ROW and Single COLUMN based on that a cross tab query should populate.

I googled and took some really helping code but I am not able to achieve what I need and I know I am near to it, But I am not able to complete it.

Please could you help me out on this.

Row list box name is - lstRowData
column list box name is - lstColData

I want to show the data in a subform as datasheet view, name of the subform is - QryDatasheet

Expand|Select|Wrap|Line Numbers
  1. Dim tst, clm As Variant
  2.  
  3. clm = Me.lstColData.Value
  4.  
  5. tst = "TRANSFORM Count(QryConsolidatedData.[Employee ID]) AS [Count]" _
  6. & " SELECT " & RowDatas & ", Count(QryConsolidatedData.[Employee ID]) AS [Total Of Employee ID]" _
  7. & " FROM QryConsolidatedData" _
  8. & " GROUP BY " & RowDatas & "" _
  9. & " PIVOT QryConsolidatedData.[" & clm & "]"
  10.  
  11.  
  12.  
  13. Function RowDatas()
  14.  
  15.    Dim varItem As Variant      'Selected items
  16.     Dim strWhere As String      'String to use as WhereCondition
  17.     Dim lngLen As Long          'Length of string
  18.     Dim strDelim As String      'Delimiter for this field type.
  19.     Dim strDescrip As String    'Description of WhereCondition
  20.  
  21.     strDelim = """"            'Delimiter appropriate to field type. See note 1.
  22.  
  23.     'Loop through the ItemsSelected in the list box.
  24.     With Form_Form1.lstRowDtls
  25.         For Each varItem In .ItemsSelected
  26.             If Not IsNull(varItem) Then
  27.                 'Build up the filter from the bound column (hidden).
  28.                 'strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim & ","
  29.                 strWhere = strWhere & "[" & .ItemData(varItem) & "]" & ", "
  30.                 'Build up the description from the text in the visible column. See note 2.
  31.                 strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
  32.             End If
  33.         Next
  34.     End With
  35.  
  36.     'Remove trailing comma. Add field name, IN operator, and brackets.
  37.     lngLen = Len(strWhere) - 2
  38.     If lngLen > 0 Then
  39.         'strWhere = "IN (" & left$(strWhere, lngLen) & ")"
  40.         strWhere = left$(strWhere, lngLen)
  41.         lngLen = Len(strDescrip) - 2
  42.         If lngLen > 0 Then
  43.             strDescrip = "Details: " & left$(strDescrip, lngLen)
  44.         End If
  45.     End If
  46.     RowDatas = strWhere
  47. End Function
  48.  
  49.  
Feb 17 '15 #1

✓ answered by jforbes

It looks like you are updating the SQL of the Query Definition and I don't think you need to go that far.

I would attempt to update the RecordSource of your SubForm. Maybe try the following around line 22 of your Second Post.:
Expand|Select|Wrap|Line Numbers
  1. Me.QryDatasheet.Form.RecordSource = strSQL

3 1487
johny6685
66 64KB
I did researched more in this site and corrected my code and it is working for one time and when I change the selections and refresh the query, the query doesn't show the updated data, instead it shows the old data only.

Please could you help me out how to refresh the data?

I even tried to create a new form and put the query on that, so that I can call the subform whenever the user want to refresh with new selection, but I am not getting the fresh data, instead the old data appearing.

Expand|Select|Wrap|Line Numbers
  1. Private Sub LbSubmit_Click()
  2. Dim clm As Variant
  3.  
  4. Dim db As DAO.Database
  5.     Dim qdf As DAO.QueryDef
  6.     Dim strSQL As String
  7.     Dim rst As Recordset
  8.  
  9.     Set db = CurrentDb
  10.     Set qdf = db.QueryDefs("qry_index")
  11.  
  12. Me.Form2.Visible = False
  13.  
  14.  
  15. clm = Me.lstColData.Value
  16.  
  17. strSQL = "TRANSFORM Count(QryConsolidatedData.[Employee ID]) AS [Counts]" _
  18. & " SELECT " & RowDatas & ", Count(QryConsolidatedData.[Employee ID]) AS [Total Of Employee ID]" _
  19. & " FROM QryConsolidatedData" _
  20. & " GROUP BY " & RowDatas & "" _
  21. & " PIVOT QryConsolidatedData.[" & clm & "]"
  22.  
  23.  
  24. qdf.SQL = strSQL
  25.     'DoCmd.OpenQuery "qry_index"
  26.  
  27.  
  28.     Set qdf = Nothing
  29.     Set db = Nothing
  30. Me.Form2.Visible = True
  31.  
  32. End Sub
Feb 18 '15 #2
jforbes
1,107 Expert 1GB
It looks like you are updating the SQL of the Query Definition and I don't think you need to go that far.

I would attempt to update the RecordSource of your SubForm. Maybe try the following around line 22 of your Second Post.:
Expand|Select|Wrap|Line Numbers
  1. Me.QryDatasheet.Form.RecordSource = strSQL
Feb 18 '15 #3
johny6685
66 64KB
Thanks for your response jforbes.

It worked the way you advised, but the rows and columns are not refreshed actually. The Previous rows still remains there and it says #Name? for the fields I haven't selected the next time.

But I have found another working around in this link which helped me to come out of this issue and it is working perfectly.
Feb 18 '15 #4

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

Similar topics

1
by: Matthew Wells | last post by:
I have a crosstab query based on anothe query. The base query resultset has no null values in its "Quantity" column. However, when I create the new crosstab query from the base query, the records...
2
by: Greg Busby | last post by:
I'm trying to get an Access Crosstab query to fill a VB.NET datagrid. I'm getting an error that states "not a valid file name".. the query runs fine when I run the same SQL in Access. Can a...
14
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....
6
by: tizmagik | last post by:
I am having a lot of difficulty generating a CrossTab Query based report. I have looked online for several tutorials and whatnot but I have not been able to really find what I'm looking for, nor...
10
by: hobbson | last post by:
I have a basic crosstab queries that counts the models of each type of server device in a data center. What I'm trying to do is restrict the crosstab query from returning counts for devices that are...
1
by: Shortstuff12345 | last post by:
I have a database that tracks design changes made by different groups of people who work for different managers. I currently have a form setup such that the user can select a range of dates and when...
2
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...
4
by: tetsuo2030 | last post by:
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...
1
by: sroseindy | last post by:
PLEASE HELP - I NEED THIS TO COMPLETE A CONTRACT!!! Hi all, I have a report based on a crosstab query. The number of columns is dynamic based on the selection criteria the user chooses. I’ve set up...
1
by: accessonion | last post by:
Hi, I have finally been converted to Access as the data set I’m using grew to a ridiculous size. I am analysing one years’ worth of data and the database itself has grown to be quite large at just...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.