473,394 Members | 1,932 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,394 software developers and data experts.

Using query criteria to look at two different forms

283 100+
Hello,

I am having a problem with a query attatched to a report. I have two different forms that I want to be able to run the same report with.

Example: Form(A) is open I input a date range in box1 and box2 then press the Run Report Button. When the report goes to open its looking for the date range from box1 and box2 on Form(B)

If i just press ok and bypass the question boxes it will still run the report and work fine, but I would like to make it so that it will only look at the Criteria from the Form that is currently being used.

Thanks in advance
Aug 5 '11 #1

✓ answered by MMcCarthy

OK remove the criteria from the query and change the code that runs the report to something like the following...

Expand|Select|Wrap|Line Numbers
  1. ' Code for button to run report
  2. Private sub btnMyReport_Click()
  3. Dim strDocument As String
  4. Dim strLinkCriteria As String
  5.  
  6.     strDocument = "MyReportName"
  7.     strLinkCriteria = "[DateField] BETWEEN #" & Me!StartDate & "# AND  #" & Me!EndDate & "#"
  8.  
  9.     DoCmd.OpenReport strDocument, , , strLinkCriteria
  10.  
  11. End Sub
  12.  

20 2667
MMcCarthy
14,534 Expert Mod 8TB
Hi Slenish

Are you using VBA to set the report criteria or have you put the criteria links in the query?

I don't really understand what you mean here - "but I would like to make it so that it will only look at the Criteria from the Form that is currently being used. " - can you explain further.

Mary
Aug 5 '11 #2
slenish
283 100+
Hello Mary,

Thanks for the reply :D

I have the criteria links set in the Query.

Example: this is set in the query
Expand|Select|Wrap|Line Numbers
  1. DateField
  2. Total = Where
  3. Criteria = Between Forms!DateForm!StartDate And Forms!DateForm!EndDate
  4.  
Aug 6 '11 #3
MMcCarthy
14,534 Expert Mod 8TB
OK remove the criteria from the query and change the code that runs the report to something like the following...

Expand|Select|Wrap|Line Numbers
  1. ' Code for button to run report
  2. Private sub btnMyReport_Click()
  3. Dim strDocument As String
  4. Dim strLinkCriteria As String
  5.  
  6.     strDocument = "MyReportName"
  7.     strLinkCriteria = "[DateField] BETWEEN #" & Me!StartDate & "# AND  #" & Me!EndDate & "#"
  8.  
  9.     DoCmd.OpenReport strDocument, , , strLinkCriteria
  10.  
  11. End Sub
  12.  
Aug 6 '11 #4
MMcCarthy
14,534 Expert Mod 8TB
Just a further note to say that using the code to set the criteria like this means you can run the same report from any form.
Aug 6 '11 #5
slenish
283 100+
Hi Mary,

Thanks a lot for the help. Well I am still having a problem. I used the code that you gave me and put it in to the report button. Problem im having now is that when i run the report it shows all the dates and not the range that I select. Originally i had this set to where on the query but if i set it to where and leave the criteria blank it just deletes the field so im not sure how to fix this? Also if i wanted to add other fields like a name field along with the date would I add it in to the strLinkCriteria? Or would I make another new line like strLinkCriteria2?

Thanks again :D
Aug 6 '11 #6
MMcCarthy
14,534 Expert Mod 8TB
Problem im having now is that when i run the report it shows all the dates and not the range that I select. Originally i had this set to where on the query but if i set it to where and leave the criteria blank it just deletes the field so im not sure how to fix this?
Can you change the query layout to SQL and post the code in here so I can see what you are doing?

Also if i wanted to add other fields like a name field along with the date would I add it in to the strLinkCriteria? Or would I make another new line like strLinkCriteria2?
Add it to the strLinkCriteria. Use the AND operator e.g.
Expand|Select|Wrap|Line Numbers
  1.  
  2. strLinkCriteria = "([DateField] BETWEEN #" & Me!StartDate & "# AND #" & Me!EndDate & "#) " & _
  3. "AND (NameField = '" & Me!NameField & "')"
  4.  
Aug 6 '11 #7
slenish
283 100+
Here is the SQL from the query


Expand|Select|Wrap|Line Numbers
  1. SELECT MainTbl.Date, MainTbl.Name, MainTbl.Er1, MainTbl.Er2, NameTbl.Status
  2. FROM MainTbl INNER JOIN NameTbl ON (MainTbl.Name = NameTbl.Name) AND (MainTbl.Date = NameTbl.Date)
  3. WHERE (((NameTbl.Status)="Active"));
  4.  
Aug 6 '11 #8
MMcCarthy
14,534 Expert Mod 8TB
OK the first thing I would point out is using Date as a field name is bad as it's a protected name (function name) in access and can cause problems. So change the name of this field to something else.

So for now I'm going to rename the field to MyDate. First question is .. is this field actually on the report? Secondly is this the field you want the date range criteria to work on?
Aug 6 '11 #9
slenish
283 100+
Actually my date field is not called date i just made it a general name to show you, so no worries there. :D

The date field and the name field is what i want the criteria to work on. Also yes the date field is on the report as well. Reason is so when the person views the different pages of the report the date will change depending on what page they are on.

As of right now the report works great with no problems just trying to make it work from two different locations.

Thanks again for the help with this hate to take up to much of your time.
Aug 6 '11 #10
MMcCarthy
14,534 Expert Mod 8TB
Ok so I gather the following statements are true ..

1. There is no criteria currently on the query underlying the report
2. If you enter a StartDate and EndDate on the form and run the report using the code I gave you it is not limiting the records to between these two dates

Are both these statements true?
Aug 6 '11 #11
slenish
283 100+
Yes they are both true. :)
Aug 6 '11 #12
MMcCarthy
14,534 Expert Mod 8TB
OK, try changing the strLinkCriteria to this ...
Expand|Select|Wrap|Line Numbers
  1. strLinkCriteria = "[DateField] BETWEEN #" & Format(Me!StartDate, "dd mmmm yyyy") & "# AND  #" & Format(Me!EndDate, "dd mmmm yyyy") & "#"
  2.  
Access can have problems with date formats, hopefully this will fix the problem.
Aug 6 '11 #13
slenish
283 100+
Ok im still not having any luck here is what i have right now.

Expand|Select|Wrap|Line Numbers
  1. strDocName = "JReport"
  2.     strLinkCriteria = "([Date] BETWEEN #" & Format(Forms![Entry2]![Entry3]!StartDate, "mm/dd/YYYY") & "# AND  #" & Format(Forms![Entry2]![Entry3]!EndDate, "mm/dd/YYYY") & "#) " & _
  3.     "AND ([EName] ='" & Forms![Entry2]![Entry3]!txtName & "')"
  4.  
  5.    DoCmd.OpenReport strDocName, acViewPreview, strLinkCriteria
  6.  
Aug 6 '11 #14
MMcCarthy
14,534 Expert Mod 8TB
Q1. Where have you got that code? Behind the button on the form?

Q2. What is this reference "Forms![Entry2]![Entry3]!txtName"?
Aug 6 '11 #15
slenish
283 100+
Yes the code is behind the button on the form.

The "Forms![Entry2]![Entry3]!txtName" is the name entry field on the form I can remove it for now, but i will need this as a factor when all is said and done. I asked you about how to add this in like 7 posts ago :)
Aug 6 '11 #16
MMcCarthy
14,534 Expert Mod 8TB
But thats not the code I gave you. If txtName is a textbox on a subform called Entry3 then the code is ...

Expand|Select|Wrap|Line Numbers
  1. Me!Entry3.txtName
and not

Expand|Select|Wrap|Line Numbers
  1. Forms![Entry2]![Entry3]!txtName
If there is some particular reason you have for using this format it's still not correct. The correct way of writing this would be ..

Expand|Select|Wrap|Line Numbers
  1. Forms![Entry2]![Entry3].Form!txtName
Aug 6 '11 #17
slenish
283 100+
I do appologize, but i have used the this type of format (Forms!Form1!form2!txtBox) for many other things and it works just fine also i have used your last example as well (forms!Form1!form2!.Form!txtBox) on occasion.

For some reason though none of the above is working for this report haha! its getting to be so frustrating that its funny.

I appreciate your help with this. I will continue to play around with the code examples you have given me and see what i can figure out. For now im heading to bed its pretty late where i am.

If im successful i will let you know.

Thanks again :D
Aug 6 '11 #18
Mihail
759 512MB
Hi all !
Slenish, Take a look here:
http://bytes.com/topic/access/answer...control-values
I think you find a possible answer.
Aug 6 '11 #19
NeoPa
32,556 Expert Mod 16PB
Slenish,

Let me try to help here by pointing out then when working with someone else it is always very important to be precise. Posting SQL which is similar to, but not exactly, what you are using will cause problems. Testing code which is similar to, but not the same as, code that Mary suggests will cause problems.

What I'm saying here is that your problems and your frustration are entirely down to your approach and attitude towards working with someone else. If you take this lesson on board you will find it so much simpler to get your problems resolved.

Let me recap some of what Mary has already posted. All of this is true.
  1. Filtering a bound object (EG. Report or Form) is not remotely complicated. People do it every day without problems.
  2. Controlling this from behind a form control (A Command Button for instance) often requires reference to items (values of controls particularly) from that same form. The correct way to refer to these controls is as properties of the Me object. It is certainly possible to use a longer form, even via the Forms() collection if you're really masochistic, but there are good reasons why this is not considered a sensible approach (Development time; readability; maintainability; consistency; etc to name but a few).
  3. The standard way to handle such filtering, in its most basic form, is to ensure the object in question is not itself filtered, and is bound to a table or query which is not filtered. Exceptions to this may be filtering which is separate from, and does not impact upon, any filtering you may later rely on the form to provide. [Status]='Active' may be an example of this in your case.

    With this logically open query we can now apply the filtering determined by the code behind the form. The LinkCriteria (or whatever name you choose) variable then contains a string which matches the string you might find in a SQL WHERE clause.
  4. Date literals in SQL do not follow localised format conventions. The simplest format for a date in a SQL string is the standard (m/d/yyyy), but any unambiguous format will work reliably. Mary's suggestion of "dd mmmm yyyy" for instance, is entirely unambiguous for example.
  5. I know your SQL posted earlier is not your actual SQL, but if it were then the following should be an answer to your original question - assuming the information you've provided so far is reliable :
    Expand|Select|Wrap|Line Numbers
    1. With Me
    2.     strLinkCriteria = "([Date] Between #" & _
    3.                       Format(CDate(.StartDate),'m/d/yyyy') & _
    4.                       "# And  #" & _
    5.                       Format(CDate(.EndDate),'m/d/yyyy) & _
    6.                       "#) AND ([EName]='" & .txtName & "')"
    7. End With
Aug 6 '11 #20
slenish
283 100+
Just wanted to update this and once again say thanks to Mary for all of the help the other day. I figured out what the problem was which was actually just something very simple.

Mary was right on with the code examples that she gave me but the problem turned out to be in the DoCmd statement, I was missing a comma(,) before the strLinkCriteria.

so instead of this
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport strDocName, acViewPreview, strLinkCriteria 
  2.  
I should have had this
Expand|Select|Wrap|Line Numbers
  1. [DoCmd.OpenReport strDocName, acViewPreview,, strLinkCriteria 
  2.  
The extra comma puts the strLinkCriteria in to the where condition so it will pass the variables.

Thanks again for all the help :D

Also thank for the added support Mihail and NeoPa. I know i will find uses out of both of them sooner rather than later. :D
Aug 9 '11 #21

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

Similar topics

7
by: keliie | last post by:
Hello Just a quick question that I've spent a few hours trying to solve with no luck (although one would think this should be fairly easy). I have a form with a subform. The subform is based...
3
by: stevecat | last post by:
Hi there, I have created a form, "search" and a query "search_product". The query returns the product information based upon the criteria for three of the fields, author, title or isbn. The field...
2
by: starke1120 | last post by:
I have a main form with a text box on it "txtSearch" onlost focus it updates another text box "txtSearchString" to include a quote and asterik before and after the value of txtSeach. I then...
4
waynetheengineer
by: waynetheengineer | last post by:
Hi, I was wondering if anyone had any suggestions on my database: I have a form that accepts user input into a single text box. When the OK button is hit, a query should search for all records...
3
by: ericargent | last post by:
Hi I'm using Acces 2003 I have Query where the several parameters for the criteria are supplied from a form. One parameter source is a combo box. What I am trying to do is if: An item is...
4
by: candide_sh | last post by:
Hi, I have a combobox cbosaSAK_FKEY with these values: 1 2 3 The query criteria has to be changed into: 1 =1
4
by: tbeers | last post by:
Is there a method to pass along a criteria argument directly to the query criteria rather than through filtering a form or report? In other words, I would like to click a "print" button and in the...
7
by: DeZZar | last post by:
Hi all, Unfortunately I am quite a novice with Access!! I've created a number of data bases for my work however becuase my skills are limited to really built in functionality and wizards my...
30
by: DH22 | last post by:
Access 03 I'm currently having an issue using Dlookup when trying to reference a query (criteria syntax) Currently I have 1 table, which is L_Emps (which contains Employee_ID as my key...
9
ajhayes
by: ajhayes | last post by:
Hello everyone, This is my first time posting here and I'm hoping someone can help me out. I'm a relative newbie to Access and am pretty much learning as I go along, so please bear with me. ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
jinu1996
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...

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.