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

Multiple selects including Null's in a query

damonreid
114 Expert 100+
Hi,
I want to select multiple variables from a form and pass them to a query, however I don't mind how many of the variables are used.

I am currently using the syntax;

Like [Forms]![Form Name]![Combo Box Name] & "*"

On each of the fields in the query I want to be able to filter on for my report. However it is ignoring my null fields and given I have 10+ variables and none of them are required fields I am getting very few results. Is there any way to do this but to include nulls as well?

I am using XP and Access 2003.



Very long winded code below...


Expand|Select|Wrap|Line Numbers
  1. SELECT PunchListDetails.Ref, PunchListDetails.[Entered On], PunchListDetails.[Associated System], PunchListDetails.[Location/Area], PunchListDetails.PunchDetails, PunchListDetails.[Additional Comments], PunchListDetails.[Item Open], PunchListDetails.[Item Closed], PunchListDetails.[Item Raised At], PunchListDetails.[Date Closed Out], PunchListDetails.[Due On], PunchListDetails.[Revised Due Date], PunchListDetails.Discipline, PunchListDetails.[Punch Owner], PunchListDetails.[Responsible Contractor], PunchListDetails.OwnerSignOff, PunchListDetails.OwnerSignOffLogIn, PunchListDetails.OwnerSignOffDate, PunchListDetails.EngineerSignOff, PunchListDetails.EngineerSignOffLogIn, PunchListDetails.EngineerSignOffDate, PunchListDetails.ClientSignOff, PunchListDetails.ClientSignOffLogIn, PunchListDetails.ClientSignOffDate, PunchListDetails.[Project Number], PunchListDetails.Impact
  2. FROM PunchListDetails
  3. WHERE (((PunchListDetails.[Associated System]) Like [Forms]![ReportsForm]![cmbSystemCode] & "*") AND ((PunchListDetails.[Location/Area]) Like [Forms]![ReportsForm]![cmbArea] & "*") AND ((PunchListDetails.[Item Raised At]) Like [Forms]![ReportsForm]![cmbRaised] & "*") AND ((PunchListDetails.Discipline) Like [Forms]![ReportsForm]![cmbDiscipline] & "*") AND ((PunchListDetails.[Punch Owner]) Like [Forms]![ReportsForm]![cmbOwner] & "*") AND ((PunchListDetails.[Responsible Contractor]) Like [Forms]![ReportsForm]![cmbContractor] & "*") AND ((PunchListDetails.Impact) Like [Forms]![ReportsForm]![cmbImpact] & "*") AND ((PunchListDetails.[Entered By]) Like [Forms]![ReportsForm]![cmbRaisedBy] & "*") AND ((PunchListDetails.[Item Type]) Like [Forms]![ReportsForm]![cmbItemType] & "*"));
Jul 9 '07 #1
11 2212
kepston
97 Expert
Try using OR instead of AND in your WHERE clause.
Jul 9 '07 #2
damonreid
114 Expert 100+
That does work but it now returns values for rows not intended.

For example if we wanted raised by Damon Reid and Mechanical items it now returns all items raised by Damon Reid and all Mechanical items instead of the Mechanical items raised by Damon Reid.

I need to keep it as AND but I need to be able to include nulls in the "*" search.

Thanks for the try though.
Jul 9 '07 #3
kepston
97 Expert
I thought it was too easy!
Let me think about it, and I'll get back to you.
Jul 9 '07 #4
kepston
97 Expert
You need to build the criteria 'on the fly'.
Do this in an appropriate event.
Do you have a search button on your [ReportsForm]? - build criteria in OnClick event
Is this query the basis for a report/form? - build criteria in OnLoad event
Expand|Select|Wrap|Line Numbers
  1. Dim strCriteria As String
  2. strCriteria=""
  3. If Not IsNull([Forms]![ReportsForm]![cmbSystemCode]) Then
  4.     strCriteria=strCriteria & "PunchListDetails.[Associated System]) Like [Forms]![ReportsForm]![cmbSystemCode] & '*'" & " AND "
  5. End if
  6. If Not IsNull([Forms]![ReportsForm]![cmbArea]) Then
  7.     strCriteria=strCriteria & "(PunchListDetails.[Location/Area]) Like [Forms]![ReportsForm]![cmbArea] & '*'" & " AND "
  8. End if
  9.  
etc...
Finish with
Expand|Select|Wrap|Line Numbers
  1. strCriteria=Left(strCriteria, Len(strCriteria)-5)
to remove the last " AND "

Let me know if you get stuck
Jul 9 '07 #5
NeoPa
32,556 Expert Mod 16PB
I would recommend the same course :)
Add only the items that are used to the WHERE clause in the string.
Jul 9 '07 #6
damonreid
114 Expert 100+
Thank you both... this was driving me crazy, I can't belive you are not able to do it in the query itself, would be so much easier.

One last question once I have my nice new string strCriteria how do I pass this to a report. I have opened the report and gone to the vb code but I am not sure how to pass a filter to it on open?

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub Report_Open(Cancel As Integer)
  4.  
  5. End Sub

Is the complete code for the command button (in case anyone is interested).

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPrePunchDetails_Click()
  2. On Error GoTo Err_cmdPrePunchDetails_Click
  3.  
  4.         Dim strCriteria As String
  5.         strCriteria = ""
  6.         If Not IsNull([Forms]![ReportsForm]![cmbSystemCode]) Then
  7.             strCriteria = strCriteria & "PunchListDetails.[Associated System]) Like [Forms]![ReportsForm]![cmbSystemCode] & '*'" & " AND "
  8.         End If
  9.         If Not IsNull([Forms]![ReportsForm]![cmbArea]) Then
  10.             strCriteria = strCriteria & "(PunchListDetails.[Location/Area]) Like [Forms]![ReportsForm]![cmbArea] & '*'" & " AND "
  11.         End If
  12.         If Not IsNull([Forms]![ReportsForm]![cmbRaisedBy]) Then
  13.             strCriteria = strCriteria & "(PunchListDetails.[Entered By]) Like [Forms]![ReportsForm]![cmbRaisedBy] & '*'" & " AND "
  14.         End If
  15.         If Not IsNull([Forms]![ReportsForm]![cmbRaised]) Then
  16.             strCriteria = strCriteria & "(PunchListDetails.[Item Raised At]) Like [Forms]![ReportsForm]![cmbRaised] & '*'" & " AND "
  17.         End If
  18.         If Not IsNull([Forms]![ReportsForm]![cmbDiscipline]) Then
  19.             strCriteria = strCriteria & "(PunchListDetails.[Discipline]) Like [Forms]![ReportsForm]![cmbDiscipline] & '*'" & " AND "
  20.         End If
  21.         If Not IsNull([Forms]![ReportsForm]![cmbOwner]) Then
  22.             strCriteria = strCriteria & "(PunchListDetails.[Punch Owner]) Like [Forms]![ReportsForm]![cmbOwner] & '*'" & " AND "
  23.         End If
  24.         If Not IsNull([Forms]![ReportsForm]![cmbImpact]) Then
  25.             strCriteria = strCriteria & "(PunchListDetails.[Impact]) Like [Forms]![ReportsForm]![cmbImpact] & '*'" & " AND "
  26.         End If
  27.         If Not IsNull([Forms]![ReportsForm]![cmbContractor]) Then
  28.             strCriteria = strCriteria & "(PunchListDetails.[Responsible Contractor]) Like [Forms]![ReportsForm]![cmbContractor] & '*'" & " AND "
  29.         End If
  30.         If Not IsNull([Forms]![ReportsForm]![cmbItemType]) Then
  31.             strCriteria = strCriteria & "(PunchListDetails.[Item Type]) Like [Forms]![ReportsForm]![cmbItemType] & '*'" & " AND "
  32.         End If
  33.  
  34.         strCriteria = Left(strCriteria, Len(strCriteria) - 5)
  35.  
  36.         Dim stDocName As String
  37.  
  38.         stDocName = "PunchListDetails"
  39.         DoCmd.OpenReport stDocName, acPreview
  40.  
  41. Exit_cmdPrePunchDetails_Click:
  42.     Exit Sub
  43.  
  44. Err_cmdPrePunchDetails_Click:
  45.     MsgBox Err.Description
  46.     Resume Exit_cmdPrePunchDetails_Click
  47.  
  48. End Sub
Jul 10 '07 #7
kepston
97 Expert
Expand|Select|Wrap|Line Numbers
  1. Dim stDocName As String
  2.         stDocName = "PunchListDetails"
  3.         DoCmd.OpenReport stDocName, acPreview, , strCriteria
Jul 10 '07 #8
damonreid
114 Expert 100+
That works perfectly.
Only exception is when no fields are entered, but I have a separate report for that.

Thank you very much.
Jul 10 '07 #9
NeoPa
32,556 Expert Mod 16PB
When no criteria are entered, you simply need to call it with :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport stDocName, acPreview
Jul 10 '07 #10
damonreid
114 Expert 100+
That is far easier then having report a and report b for updating.

Thanks again.

Damon
Jul 10 '07 #11
NeoPa
32,556 Expert Mod 16PB
No problem.
I'm pleased that worked for you and yes, it is easier ;)
Jul 10 '07 #12

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

Similar topics

4
by: vishal | last post by:
hi here is vishal. i am creating a site which will have one option for change language. once user selects the language i am storing his reply in session variable. then i want to display the...
6
by: JackT | last post by:
I want to get a column count several times in one query using different filters but can't work out how to do it - can anyone point me in the right direction? For example, how would combine these...
7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
3
by: gregory.sharrow | last post by:
I need to secure a datawarehouse table at the row level based on 1 to many keys on that table. A user should only see the rows they have access to. I need to be able to figure out which rows they...
10
by: chudson007 | last post by:
I want to join 2 tables by a unique ID field, but the ID field also has multiple NULLS which I do not want to ignore and I fear they will cause duplication. Using TableA and TableB below i will...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
7
by: Ceebaby via AccessMonster.com | last post by:
Hi All Here's hoping someone can help me with this. I have a report based on a query where the criteria for 4 of the fields is set from an unbound form. I want the user to be able to select any...
1
by: Vivienne | last post by:
Hi there This is a hard problem that I have - I have only been using sql for a couple of weeks and have gone past my ability level quickly! The real tables are complex but I will post a simple...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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...

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.