473,398 Members | 2,404 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,398 software developers and data experts.

Run Query based off Table selected from Combo Box

12
Ok, so here's the situation:
I have a series of tables that have monthly reporting data in them (1 table for each month). In these tables is a 'Total Views' column. What I need to do is calculate the difference between the Total Views of each item from 2 separate months (something like: [9-September]![Total Views]-[8-August]![Total Views]).

I can manually create a query and set the Total Views Field to the aforementioned formula and get a table with the data I want on it.

Here's where it gets tricky: I need to be able to create that query 'on the fly' (form button click) and have the tables it's reaching out to based on what was selected from a combo box on the form. I have the form (frmDataPull) all set up to give the user a list of tables to select from for month 1 (cboMonth1) and month 2 (cboMonth2)

I cannot for the life of me figure out how to setup a query to pull the Total Views data from the cboMonth1 selection/table and subtract it from the cboMonth2 selection/table. What do I need to type into the Field and/or Criteria boxes to get this to work? Is there any coding that needs to take place in the background?

Any help would be greatly appreciated; this issue has been quite vexing...
Oct 10 '14 #1

✓ answered by jforbes

Instead of explaining multiple ways it could be done, there are a lot of options available to you right now, I figured I would bang out a sample and post the results so you can see how you can approach this adventure. This is quick and dirty. You will want to make it your own.

Here is a sample Schema:


It's mostly the same as one of your tables with the addition of ExtractNumber and ExtractName. For this example ExtractNumber is a number that you would increment each time you added a monthly extract to the table. This is the column that will link the months together. ExtractName is just a User Friendly Name for the Extract. You could include Month and Year here or to confuse the issue, make it FK lookup to another table with the long name (don't let that muddy the water yet)

Here is some Sample Data that I put in there to test with:



Here is a quick and dirty Query linking the tables together:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   [KnowledgebaseHistory].[Total Views]-[Previous].[Total Views] AS ViewsThisGoRound
  3. , Previous.ExtractNumber
  4. , KnowledgebaseHistory.[Total Views]
  5. , KnowledgebaseHistory.[Article Number]
  6. , KnowledgebaseHistory.ExtractNumber
  7. , KnowledgebaseHistory.ExtractName
  8. , KnowledgebaseHistory.Title
  9. , KnowledgebaseHistory.[Version Number]
  10. , KnowledgebaseHistory.[Created Date]
  11. , KnowledgebaseHistory.[Last Modified Date]
  12. , KnowledgebaseHistory.[Created By Full Name]
  13. , KnowledgebaseHistory.[Last Modified By Full Name]
  14. FROM KnowledgebaseHistory 
  15. LEFT JOIN KnowledgebaseHistory AS Previous 
  16. ON (KnowledgebaseHistory.ExtractNumber-1 = Previous.ExtractNumber) AND (KnowledgebaseHistory.[Article Number] = Previous.[Article Number]);
An here is some Sample Output of that Query:



I'm hurrying to get this post completed as I need to go, so if everything looks good, you should that the first two rows have Nulls for [ViewsThisGoRound] as there is no previous history, where the 3rd and 4th rows have the delta.

Hope it helps.

10 2343
jforbes
1,107 Expert 1GB
Would love to help, but from what is provided it would be all very speculative. If you could provide particulars about your tables it would help a lot.
Oct 11 '14 #2
zmbd
5,501 Expert Mod 4TB
You are using the old workbook>Worksheet mindset here and that will cause you a lot of issues!

I highly recommend that you absolutely stop what you are doing and read the following:[*]> Database Normalization and Table Structures.

I also have to agree with jforbes, you've not provided enough information on the table structures and their relationships; however, I suspect, once you normalize your data the issues you are having will solve themselves. Along with the details of your table structure, the version of Access and your level of Access knowledge will be most helpful.
Oct 11 '14 #3
AmDigPC
12
So here's a bit more information on the tables:
They contain records of knowledgebase article views for each month and have a PK of 'Article Number' that is shared/linked in each month (table). I attached a screenshot of the relationships. What needs to happen is a query needs to take the data from the 'Article Views' column in 1 table and subtract those numbers from the 'Article Views' data in a second table (both tables selected from a form) and report the difference.

I'm not having issues with the data itself, I just don't know how to do what needs to be accomplished. I have a pretty good understanding of Access & Databases (it's just been a while since I worked with them). Also, I am using Access 2013.


Attached Images
File Type: jpg MaintDB-Relationships.jpg (38.2 KB, 611 views)
Oct 11 '14 #4
jforbes
1,107 Expert 1GB
I agree with ZMBD, you will run into trouble with your data structured the way it currently is.

A place you will have trouble is setting up a one-to-one relationship between two different months. This type of query could easily have orphaned records if there are Articles in one table an not the other. In this case your calculations will either be off or missing altogether. You will probably have this trouble regardless, but it's usually easier to trap when you are referencing a single table.

It would probably be best to create a Main Table with all the fields along with either Month and Year Fields, or a field to represent both Month and Year. Once this is done, you can you can setup a static query and just supply the Month/Year as parameters.

If you have no choice to continue on with your current structure, you will need to create a text string like the following and replace the actual tables with your users selection.
Expand|Select|Wrap|Line Numbers
  1. SELECT M1.*, M2.[Total Views] as [M2 Total Views]
  2. FROM [8-August2014] AS M1
  3. INNER JOIN [9-September2014] AS M2
  4. ON M1.[Article Number]=M2.[Article Number]
You never really mention if you are creating a Report off of this or if you are showing it on a Form. Either way, you would then set the Form or Reports RecordSource equal to the string.
Oct 13 '14 #5
AmDigPC
12
Thanks for the response!

So you are saying that I should just have a single table that I import the data into each month with the added field of month/year? Would I still have the individual month tables? If not, how would I write the query to take the user selected month/year from the form and subtract the 2 Article Views numbers?

It sounds like I'm going about this the wrong way & what I was trying to do isn't really possible... In the end, all we need to happen is the following:
1. Import spreadsheet report (for month) from knowledgebase system
2. User opens form and select the 2 months they want to calculate
3. Query calculates the difference in views from Month 1 to Month 2
4. User gets a report showing the total article views for desired month (Month 2)

The whole drive behind this is our KB system only stores the Total Views for the lifetime of the article (does not have the ability to calculate monthly views). As a result, we are doing a data capture at the end of each month so we can have historical article usage data (mainly for the purpose of knowing which articles are not being used & can be archived).

With that being said, is there a better way to approach this?
Oct 13 '14 #6
jforbes
1,107 Expert 1GB
Instead of explaining multiple ways it could be done, there are a lot of options available to you right now, I figured I would bang out a sample and post the results so you can see how you can approach this adventure. This is quick and dirty. You will want to make it your own.

Here is a sample Schema:


It's mostly the same as one of your tables with the addition of ExtractNumber and ExtractName. For this example ExtractNumber is a number that you would increment each time you added a monthly extract to the table. This is the column that will link the months together. ExtractName is just a User Friendly Name for the Extract. You could include Month and Year here or to confuse the issue, make it FK lookup to another table with the long name (don't let that muddy the water yet)

Here is some Sample Data that I put in there to test with:



Here is a quick and dirty Query linking the tables together:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   [KnowledgebaseHistory].[Total Views]-[Previous].[Total Views] AS ViewsThisGoRound
  3. , Previous.ExtractNumber
  4. , KnowledgebaseHistory.[Total Views]
  5. , KnowledgebaseHistory.[Article Number]
  6. , KnowledgebaseHistory.ExtractNumber
  7. , KnowledgebaseHistory.ExtractName
  8. , KnowledgebaseHistory.Title
  9. , KnowledgebaseHistory.[Version Number]
  10. , KnowledgebaseHistory.[Created Date]
  11. , KnowledgebaseHistory.[Last Modified Date]
  12. , KnowledgebaseHistory.[Created By Full Name]
  13. , KnowledgebaseHistory.[Last Modified By Full Name]
  14. FROM KnowledgebaseHistory 
  15. LEFT JOIN KnowledgebaseHistory AS Previous 
  16. ON (KnowledgebaseHistory.ExtractNumber-1 = Previous.ExtractNumber) AND (KnowledgebaseHistory.[Article Number] = Previous.[Article Number]);
An here is some Sample Output of that Query:



I'm hurrying to get this post completed as I need to go, so if everything looks good, you should that the first two rows have Nulls for [ViewsThisGoRound] as there is no previous history, where the 3rd and 4th rows have the delta.

Hope it helps.
Oct 13 '14 #7
zmbd
5,501 Expert Mod 4TB
Thank You J, I was thinking of doing the same thing; however, just didn't get a chance!!

AmDigPC; we just had another member go thru this same growing pain with design. I think that you would really benefit by reading thru the following thread (and maybe the back-story too (^_^) ) http://bytes.com/topic/access/answer...-layout-tables
One of the great things about this site is that we can benefit from the efforts put forth by others!

I should also send you my boilerplate for new access application developers... check you bytes.com inbox (^_^)


--- A little house keeping here ---
WE do ask that threads be kept to one question.
IMHO, J gave what is most likely one of the more workable answers in post#5
If you need help with the database design once you have worked thru J's last post, the thread I've linked you to, and my boiler plate, please start a new thread.

Thnx
Z
Oct 13 '14 #8
AmDigPC
12
jforbes,
Thank you so much for the detailed answer!

I was able to follow/adapt your instructions and get a report showing what we needed. Funny thing is, I was already playing around with having the data on a single table (thinking it might be easier) :-)

I do have one final question though: how would I tweak that SQL query to pull the Article Views for the last 3 months?

zmdb,
Thank you for the additional information; I will be sure to go through those links you sent! Sorry if we went a little off-topic...
Oct 14 '14 #9
AmDigPC
12
After looking into my last question/problem further, it seems like I need to have the first query (the one calculating the monthly views) dump the data onto a new table then run a second query that adds the 3 months of views together. Here's what I've accomplished:
- I have the working SQL that calculates the monthly views (tweaked version of what jforbes posted)
- Wrote a query to create the new table
- Wrote a query to put the monthly view data on the new table

Where I'm stuck is, I don't know how I would write the query that would total the last 3 months. Can anyone help on this?

Should I post this as a new topic/question?
Oct 15 '14 #10
jforbes
1,107 Expert 1GB
Yeah, One Question per Thread. Also, if you can, provide what you have accomplished so far.
Oct 16 '14 #11

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

Similar topics

2
by: JDJones | last post by:
Using PHP and MySQL. Trying to put a list of categories into a drop down select option of a form like: <form name="form" action="<? print $_SERVER?>" method="get"> <select name="subject">...
1
by: Matthew | last post by:
Hey, I have built a form that has certain combo and text boxes on it, which a user specifies his criteria and then clicks on a search button to run a query based on that criteria. To build to...
2
by: Tony A. | last post by:
The application I'm working on has three tables State, Customer, Orders. In the first combo box the State is selected. Based on the State selected the second combo box will be populated with the...
3
by: somethings.amiss | last post by:
I searched google groups for an answer but found none that I could completely understand. I have a table. It contains fields such as 'client,date,hours,project,employee.' I have a form that...
2
by: billelev | last post by:
I have a form with two combo boxes. The first, cboAccount, allows a user to select an account. Ideally, the second combo box, cboSubAccount, will allow the user to select a Sub Account from a list...
7
by: billelev | last post by:
I have a query that selects data based on a particular date. This date is selected from a combo box on a form. The data from the query is then used to generate a report. This report can be...
4
by: banderson | last post by:
Hello amazing vba writers, I am trying to make a combo box return a value based on a combo box selection. I have tried a number of the codes posted here and am still having problems. I think it is...
2
by: scolivas | last post by:
I would like to make a form that creates a query. I know that I could make a query for each radio button instance but that would take forever! So basically what i have is 10 Fields in a query ...
6
gnawoncents
by: gnawoncents | last post by:
I have a form (FRMsurveyReports) with several combo boxes (e.g. Class, ClassUnique, SurveyID, etc.) which use an SQL query based on a table (TBLSurveyResponses) for their record source. The...
1
by: Yasmeen Pannu | last post by:
HI, I am new to MS Access and stuck in a problem from few days. My Question: I have 3 combo box (Submitter, date from and date to) on my form and i want that when i select values from each of...
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.