Get information from multiple tables using one query - ms-access

I haven't really found the answer I'm looking for anywhere, but I may not have looked hard enough.
Simply what I'm trying to do is run a VBA script that takes month and date from user input and from there runs two queries to gather the necessary information for a report.
Query = "SELECT * FROM Chemicals WHERE ReportDate >= #" & BeginDate & "# AND ReportDate < #" & EndDate & "#;" _
& "SELECT TotalWaterTreated FROM Flows WHERE ReportDate >= #" & BeginDate & "# AND ReportDate < #" & EndDate & "#;"
ChemsQuery.SQL = Query
DoCmd.OpenReport "Chemicals", acViewReport
This gives me an error and seems to be impossible to do it this way.
To help clarify my set up. The way I have my report designed is that it is set as a default to run to the query "ChemsQuery". This just seems to work well especially while working on the design of the report and printing reports.
If there is a different and better way to do this, I'm open to that. Specifically I'm just trying to figure out the best way to get all of this information into one report.
Also, I would prefer not to redesign my table set up in anyway seeing as there are already over 1000 lines of code that go into this project. Finding and changing all of that code is pretty much out of the question.

Do the tables have any fields in common? If they do then the command you are looking for is JOIN. You should probably read up on it before proceeding. You can practice with JOINs in the graphical access query builder by draggin one field from a table to a field containing at least some of the same values in the other table(s). If the tables/queries aren't related by any fields but you have to have stuff from each in the same table/query result set, then UNION could be the answer you are looking for. Which you will also need to read up on. Or you could also do subreports in the main report, where for each totalwatertreated value you could subreport all chemicals, or vice-versa.

Related

Store filtered employees in training record

Good afternoon,
I have a form used to create a "toolbox talk". A toolbox talk is not always applicable to every employee but might be limited to a specific department, gender, or competency (or any combination of the three). I have created a subform which using unbound text boxes allows me to filter my list of employees based on the toolbox talk criteria - great!. See screenshot below:
However what I cant work out now is how I can then store the filtered results (list of employees) as recipients against the toolbox talk record.
I know that I could include a control to select employee ID/Names line by line as per the filtered results, however I was hoping that a quicker option could be created.
Can someone please suggest a possible solution?
Thank you in advance.
If you want to 'batch' create records, one approach is an INSERT SELECT action SQL that uses the same criteria selected by the form controls. If your code applies this criteria to the form Filter property, that property can be referenced to incorporate filter criteria for SQL. I prefer to use CurrentDb.Execute because then don't have to deal with RunSQL popup warnings.
Private Sub Command16_Click()
Dim mySql As String
mySql = "INSERT INTO tblEmployeeToolboxTalk (EmployeeID, ToolboxTalkID) " & _
"SELECT EmployeeID," & Me.ToolboxTalkID & " AS TTID FROM tblEmployees WHERE " & Me.Filter
CurrentDb.Execute mySql
End Sub
If you need guidance on conditionally building filter criteria in VBA, review http://allenbrowne.com/ser-62.html

Access 2013 - Visual Basic issue

I am experiencing an issue with Access 2013 while creating some VBA code with a button. I would like my button to open an form using: DoCmd.OpenForm
With this command i would like to implement an WHILE condition which filters the form which is going to be opened.
I have 2 fields on the current form with:
- Start Date
- End Data
The WHILE condition must filter all records Where the column "Date" is between the 2 given dates in the previous form. So actually i would like to do something like this:
WHERE date >= Me.begin_date.value AND date <= Me.eind_date.value
I cant simply figure it out to use it in the WHILE condition of the VBA code. I can however do other things with filters in VBA like:
search_filter = "ItemID LIKE '*" & Me.search_bar.Value & "*' "
DoCmd.OpenForm "#3 search-result", acNormal, , search_filter
But now i want to have the first code sample, translated to the VBA code just like the above piece of code.
How can i achieve this?
Sorry for my bad English btw.
if i recall correctly;
generate a query based on those two values (in the query builer will do) then
forms!formname.recordsource = sql for that query here
My Question was answered on another forum (tweakers.net).
search_filter = "Date BETWEEN #" & Me.begin_date.Value & "# AND #" & Me.end_date.Value & "#"
It did the trick.

Query by Form for multiple fields in Access 2010

I want to create a query which takes field parameters through a form. For this, I created a form with combo boxes and drop down options to select the values from, this populates a text value in the respective invisible text fields whose default value I have set to null. Now in my query I give criteria for column as iif(isNull([Forms]![Conditions]![text_on_form]), [column_in_table], [Forms]![Conditions]![text_on_form]). I have done this for all the columns on which the where clause comes from the form. I have tried running this. The results seem to be random. It worked for three columns, but when I played around with it, it was giving me empty result set. Can anyone tell me what I am doing wrong? Or if there is a better way to implement query by form in Access.
It sounds like you are trying to create dynamic SQL. Here is the method in vba I generally prefer:
Dim SQL As String
SQL = "SELECT tblName.* From tblName WHERE (1=1)"
If Not IsNull(Me.combo1) Then
SQL = SQL & " And ([Field1] Like ""*" & Me.combo1 & "*"")" ' I am using like statements here, but that is because this is a search tool.
End If
If Not IsNull(Me.combo2) Then
SQL = SQL & " And ([Feild2] Like ""*" & Me.combo2 & "*"")"
End If
Docmd.RunSQL SQL
End Sub
Basically, add on to the SQL statement only if the user has put a value into your text box/ combo box or whatever. The "Where (1=1)" is to account for a situation where all fields are null.
Play with this concept to create your SQL statements. Avoid using invisible text boxes to store data, it generally means you are doing something wrong and will get mixed results (someone else on this forum can explain better than me why that is).
Just use the Like operator. Put this in your criteria field in the query Like "\*" & Forms![Form_Name]![Form_Field] & "\*" -- This tells it to get anything if the field is blank (or null) and matches whatever you have in the field. This may not be what you want. It should be noted that it will return anything with the text string in it. For example: if you type "the" it will return tether, these, theses, thermometer (anything with the word "the" in it. It works best for multi word or longer strings that can be matched more accurately, however it works for a search query because there is usually a set of human eyes looking for the result and erroneous results is not a huge problem.

MS Access Parameter Query inside Subform Won't Print

Here's what I've done:
I created a Parameter Query in which one of the crtiterias is Between [Start Date] and [End Date]. I then put that Parameter Query along with other bound fields and created a subform. So far, no problems and it works great.
But I'm having issues when it comes to printing or trying to convert the form into a PDF. As soon as I ask it to print, for example, the pop-ups from the parameter query will pop up asking me the dates again and even after I enter them in (again), it keeps asking me multiple times and cancels the print job.
How do I keep the query from essentially running when I'm trying to print what's on the screen? The same thing happens if I'm trying to create a PDF.
In my not so humble opinion, parameters are handled badly in MS Access.
I think having to enter the values in the query whenever it is run (unless it is a one off experimental query) is just wrong headed. It would be so much easier to automate reports like that if you could just pass the parameters.
Typically, I create a report without the parameters in the where clause of the query, and then pass in your own where condition which gets added on Remou's answer here
You could also alter the query in the report before you call it, but that's pretty hackey.
-- Edit --
I see the confusion. I interpreted what you were doing as a report (not a form).
What's likely happening is you that when it tries to render/format the print job, it's having to make multiple calls to form's record source. And that why it keeps asking you for that data.
From what I understand in your question, you have a query that looks like this:
select foo
from bar
where
yaddah_date between [Start Date] and [End Date]
And then you've used that query as the record source for a form that you are trying to latter print as a PDF. First of all you should probably create a report that is an analogue of the form. And then open the report for printing with a filter on it:
DoCmd.OpenReport "myReport", , , , _
"yaddah_date between " & txtStartDate & _
" and " & txtEndDate
(the last part is basically the filter/where clause that report will apply to the results of the query that's generating its data).
If you MUST print the form, you can do something similar
DoCmd.OpenForm "foo", acNormal, , _
"yaddah_date between " & txtStartDate & _
" and " & txtEndDate
Or you can set the filter property of the form/subform.

How to find broken queries in Access?

How do I find broken queries in access.
i.e. Queries that might have broken because the underlying table was deleted or the name of the column in the table changed?
Is there an easy way -- rather than just opening each query running and checking if something has gone wrong?
Here are a few notes that may be of interest, depending on your version of Access.
See: GetDependencyInfo Method [Access 2003 VBA Language Reference]
Do not forget that Track name AutoCorrect info is not a good thing, for the most part, but can be useful in certain circumstances.
Dim dinf As DependencyInfo
For j = 0 To CurrentData.AllQueries.Count - 1
Set dinf = CurrentData.AllQueries(j).GetDependencyInfo
For i = 0 To dinf.Dependencies.Count - 1
''Missing alias, query or table, as far as I can tell
If dinf.Dependencies.Item(i).Name Like "MISSING:*" Then
Debug.Print CurrentData.AllQueries(j).Name _
& " " & dinf.Dependencies.Item(i).Name
End If
Next
Next
You may need to update dependencies:
Application.CurrentProject.UpdateDependencyInfo
This will require a save.