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

Eliminating Blank Results from Query

45
I have spent numerous hours now searching for a solution to this problem, but everything I have tried just hasn't work. I will try to be as detailed as possible to help explain the problem.

I have a table (we'll name TAB1) with five columns: Project #, Contract #, Date, Keyword, Conversation. Project # and Contract # are number formats, Date is a date format, Keyword is text format, and Conversation is memo format.

I have a query (we'll call QUER1)that searches through all of those categories except for the Conversation (as it is memo and is too complicated to search).

I have a form (we'll call FORM1) tied to this query that allows the user to input what they want to search.
I was able to code the query so that the user could enter any or all parts of the form to do the search, and whatever blank sections wouldn't be taken into account.
So if I input search data into Project # and Contract # and leave the rest blank, Date and Keyword are both Null, and the form returns any values that matched my input for Project # and Contract #, regardless of what Date and Keyword were on the entries.

This all works perfectly and I have no problems there. When the table is full of data and every cell has data in it, the search works fine.

Here's where the problem is:
When a user inputs a new entry and leaves some of the categories blank (i.e. They enter a Project # of 1 and a Contract # of 2 but leave the rest blank), the query then doesn't search properly. When you conduct a search and only enter to search for a Project Number of 3 or something, the other categories should be Null and it should only return Project Numbers that are 3. But since the Date and Keyword categories were left blank, it yields the entry as a positive result every time, even though the Project # doesn't match the search.

I've tried using the NZ function to change the Null or blank entries (although this isn't the ideal solution, because I don't want to change blank entries to 0, I'd rather just leave them blank). I've tried messing around with "Is Not Null" searching and just about everything, and I can't figure it out.

I hope I explained that well enough, because it is a slightly confusing problem. Please ask any clarifying questions if you don't understand. Thanks!
Jul 10 '14
75 2958
DK88
45
Rabbit: I fixed the SQL as you have said and got the form to at least run the query. When I run them, however, I don't get any results in the table at all.

This feels like I am going backwards about ten steps, because I spent a day or two of troubleshooting to get SQL code that works exactly as I need it to (mentioned in post 3 and 5), except for the original problem of this entire thread.

I need the user to be able to search critera and see the results that match that critera, but the problem is that when there are blank cells within a row, every search under my old SQL yields that row as positive, which I don't want.

If this can be done in SQL, if you could show me the exact syntax to get this to work, that would be great. I tried messing around with the Nz function, but never was able to get it to work as I needed. And I think I still need the "Is Null" in the SQL because the other search sections need to be ignored if the user does not input any criteria into them.

I recognize that SQL may run slower than VBA, but maybe that would work for this database as it only contains one table that will never have more than maybe a 1000 entries over its lifetime (And 1000 would be the extreme high end, it is likely to only contain 100 or so at most).

If it would be better in VBA, could you show me where/what VBA to use? I don't know much about VBA, but it seems like the code twinnyfo is giving me is extremely close to being exactly what I need, but I keep getting errors and nothing is working.

Thanks again for your time. If the response doesn't come within the next hour, I won't be able to try anything until Monday, because I don't have access to Access on my personal computer (har-d-har-har).
Jul 11 '14 #51
twinnyfo
3,653 Expert Mod 2GB
DK,

Your main form VBA module should look something like this:



Expand|Select|Wrap|Line Numbers
  1. Private Sub Me.txtProject_AfterUpdate()
  2.     ApplyFilters
  3. End Sub
  4. Private Sub Me.txtStartDate_AfterUpdate()
  5.     ApplyFilters
  6. End Sub
  7. Private Sub Me.txtEndDate_AfterUpdate()
  8.     ApplyFilters
  9. End Sub
  10. Private Sub Me.txtKeyword_AfterUpdate()
  11.     ApplyFilters
  12. End Sub
  13.  
  14. Private Sub ApplyFilters() 
  15.  
  16.    'This is the same code we have been playing with
  17.    'All should be in the same module on the same form
  18.  
  19. End Sub
Is this what you have? The error you have been getting, that Access can't find Apply Filters, sounds like you have the ApplyFilters procedure in a different module.

I have, essentially, this same exact code through my db when I am looking at multiple records that need to be filtered, and it has never failed. So, I am at a loss at to why it won't work with your DB.

Rabbit? NeoPa? ZMBD? Maybe one o' all y'all has some insight that we all are just missing here.....
Jul 11 '14 #52
DK88
45
I don't have lines 1-12 on the code you just posted...I was never told to add that in. When I go to add it in right now, it highlights red and says:
Compile error:
Expected: identifer
highlighting the "Me" before the dot.
Jul 11 '14 #53
DK88
45
I just put it in without the Me., but it still has the same error as earlier. And if I put the Me. in, it has the above mentioned error. Here's the overall code currently.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtProject_AfterUpdate()
  2.     ApplyFilters
  3. End Sub
  4. Private Sub txtStartDate_AfterUpdate()
  5.     ApplyFilters
  6. End Sub
  7. Private Sub txtEndDate_AfterUpdate()
  8.     ApplyFilters
  9. End Sub
  10. Private Sub txtKeyword_AfterUpdate()
  11.     ApplyFilters
  12. End Sub
  13.  
  14. Private Sub ApplyFilters()
  15. On Error GoTo EH
  16.     Dim strFilter As String
  17.     strFilter = ""
  18.  
  19.     'Cover all the possible cases:
  20.  
  21.     'First check the Project #
  22.     If Not IsNull(Me.txtProject) Then
  23.         If strFilter <> "" Then
  24.             strFilter = strFilter & _
  25.                 " AND Project = " & Me.txtProject
  26.         Else
  27.             strFilter = strFilter & _
  28.                 "Project = " & Me.txtProject
  29.         End If
  30.     End If
  31.  
  32.     'Check the Contract #
  33.     If Not IsNull(Me.txtContract) Then
  34.         If strFilter <> "" Then
  35.             strFilter = strFilter & _
  36.                 " AND Contract = " & Me.txtContract
  37.         Else
  38.             strFilter = strFilter & _
  39.                     "Contract = " & Me.txtContract
  40.         End If
  41.     End If
  42.  
  43.  
  44.     'Check the Starting Contract Date
  45.     If Not IsNull(Me.txtStartDate) Then
  46.         If strFilter <> "" Then
  47.             strFilter = strFilter & _
  48.                 " AND ConversationDate >= #" & Me.txtStartDate & "#"
  49.         Else
  50.             strFilter = strFilter & _
  51.                 "ConversationDate >= #" & Me.txtStartDate & "#"
  52.         End If
  53.     End If
  54.  
  55.     'Check the Ending Contract Date
  56.         If Not IsNull(Me.txtEndDate) Then
  57.             If strFilter <> "" Then
  58.                 strFilter = strFilter & _
  59.                     " AND ConversationDate >= #" & Me.txtEndDate & "#"
  60.             Else
  61.                 strFilter = strFilter & _
  62.                     "ConversationDate >= #" & Me.txtEndDate & "#"
  63.             End If
  64.         End If
  65.  
  66.         'Check the Keyword
  67.         If Not IsNull(Me.txtKeyword) Then
  68.             If strFilter <> "" Then
  69.                 strFilter = strFilter & _
  70.                     " AND Keyword Like '*" & Me.txtKeyword & "*'"
  71.             End If
  72.             fWhere = True
  73.         End If
  74.  
  75.  
  76.         With Me.frmSearchResults!Form
  77.             .Filter = strFilter
  78.             .FilterOn = True
  79.         End With
  80.         Exit Sub
  81. EH:
  82.     MsgBox "There was an error applying filters! " & _
  83.         "Please contact your Database Administrator.", vbCritical, "Error!"
  84.         Exit Sub
  85. End Sub
Jul 11 '14 #54
DK88
45
For what it's worth, I think I like this Subform datasheet format. If we can get the search to work properly, users can change their criteria and see the data right there on the form, and then press a button that would generate a report when they have the data they were looking for. So this will work much better than the previous method of two separate buttons for a separate form and report, presuming we can get the darned code to work! haha
Jul 11 '14 #55
twinnyfo
3,653 Expert Mod 2GB
Oooops -- sorry about the "Me." that I left in when I copied and pasted. It should not have the me.

So, if you code on your main form is exactly like you have it in post #55, you are still getting the "Cannot find ApplyFilters" error?

And all your controls are named exactly as the controls listed in the Code?
Jul 11 '14 #56
DK88
45
Sorry I wasn't able to reply until now. Yes, my code is exactly as it is posted in Post #54 (technically, there are about 80 lines of extra code beneath that, but I put a ' on every line and tripled checked that it is greened out and not relevant). I checked the controls and syntax and it all matches up, but whenever I use the form, I get the error from before:

Microsoft Office Access can't find the object 'ApplyFilters.'
If 'ApplyFilters' is a new macro or macro group, make sure you have saved it and that you have typed its name correctly.
Jul 15 '14 #57
twinnyfo
3,653 Expert Mod 2GB
Let's get weird. Lines 14, 80 and 84 from Post #54, change "Sub" to "Function". And Line 14, change "Private" to "Public". This should absolutely not make any difference at all, because everything is on the same form and VBA calls Sub just as easily as Functions. This is mind-numbing....
Jul 15 '14 #58
DK88
45
At this point, I suppose anything is worth a shot. Unfortunately, the same error. :/ I really do appreciate how much you have put into trying to figure this thing out. I'm kind of at my wits end about it too. I have spend about a full week trying to troubleshoot this, to no avail. It's so frustrating, because I spent about a week to set up and troubleshoot the rest of the entire database, and now this one silly issue cannot be fixed!

I've searched so many questions about similar issues and tried numerous options (almost exclusively through SQL, considering I don't really know how to code in VBA). The closest I got was to add an "And Is Not Null" to each category's column, i.e:

Expand|Select|Wrap|Line Numbers
  1. [Forms]![Conversation Search Form]![Project #] And Is Not Null
or
Expand|Select|Wrap|Line Numbers
  1. [Forms]![Conversation Search Form]![Project #] & (Is Not Null)
This is in the backup database before the changes to syntax, although I don't think that's causing a problem. The first code doesn't make any changes to the original problem, and the second one comes up with a run-time error (which I think is the issue with SQL running large searches, as a couple people have said now).

I just don't know what else to try...YEARG! :P
Jul 15 '14 #59
twinnyfo
3,653 Expert Mod 2GB
I am at a loss, too. I have code built the same exact way and it works perfectly. I have run out of alternative ideas. I wish I could offer more.....
Jul 15 '14 #60
Rabbit
12,516 Expert Mod 8TB
We don't normally do this but since we have stalled, please attach a zipped copy of your database to the thread. In 2007 format or earlier.
Jul 15 '14 #61
DK88
45
Okay. How do I attach it to the thread?
Jul 15 '14 #62
Rabbit
12,516 Expert Mod 8TB
Below the reply box, click advanced. Scroll down a little and there is a manage attachments button.
Jul 15 '14 #63
DK88
45

Okay, here's the database. Hopefully looking at it will make the troubleshooting significantly easier, and hopefully the problem isn't something as silly as a missing comma or mis-spelled word.
Attached Files
File Type: zip Conversations Database - New Test.zip (328.2 KB, 60 views)
Jul 15 '14 #64
Rabbit
12,516 Expert Mod 8TB
A few things.

1) I don't see a query where you used my SQL.

2) Here is a modified SQL that will work.
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.      Conversations.Project, 
  3.      Conversations.Contract, 
  4.      Conversations.ConversationDate, 
  5.      Conversations.Keyword, 
  6.      Conversations.Conversation
  7. FROM Conversations
  8. WHERE 
  9.      (Project=Forms![Conversation Search Form]!txtProject Or Forms![Conversation Search Form]!txtProject is null) And 
  10.      (Contract=Forms![Conversation Search Form]!txtContract Or Forms![Conversation Search Form]!txtContract is null) And 
  11.      (ConversationDate=Forms![Conversation Search Form]!txtStartDate Or Forms![Conversation Search Form]!txtStartDate is null) And 
  12.      (Keyword=Forms![Conversation Search Form]!txtKeyword Or Forms![Conversation Search Form]!txtKeyword is null);
3) The events tab of your controls should reference an event procedure. Put anything else in there Access will try to find a macro with that name. And you're not using macros.

4) Line 76 of post 54, the last piece should be .Form and not !Form
Jul 15 '14 #65
DK88
45
By Jove I think you've got it!! There's light at the end of the tunnel! The frustrating VBA code still doesn't work (even with the fixed typo), but I put the SQL into one of the backups I had where the button code simply ran the query, and it fixed the problem I had! The only thing I would ask is how to fix the dates with this new SQL? I want the user to be able to put in a start date only (to search any date after the start date), an end date only (to search any date before the end date), or both (to search a range of dates between the start and end date). The SQL code you have only takes the Start date into account.

Despite that minor issue though, I think this is exactly what I need! Thank you so much!

I'm not sure what you mean about the events section in your 3rd point though. What object did I have the wrong events procedures written on, and what should they be?

And the SQL you gave me wasn't on the database I posted because I have been testing through multiple backups with this database, so I only posted the one where we were trying to solve the problem with VBA. The SQL database I was using is entirely different.
Jul 15 '14 #66
DK88
45
And twinnyfo, thank you very much as well! You stuck with this despite how frustrating and lasting it got without any seeming success, and I really do appreciate that.
Jul 15 '14 #67
DK88
45
Sorry for the repetative posts, but also the keyword section. Previously, I had used this for the date:
Expand|Select|Wrap|Line Numbers
  1. ((Conversations.[Date:]) Between Nz([Forms]![Conversation Search Form]![Start Date],#1/1/1900#) And Nz([Forms]![Conversation Search Form]![End Date],#9/9/2999#))
and this for the keyword:
Expand|Select|Wrap|Line Numbers
  1. ((Conversations.[Keyword:]) Like "*" & [Forms]![Conversation Search Form]![Keyword] & "*")
How would I incorporate these into the new SQL?
Jul 15 '14 #68
Rabbit
12,516 Expert Mod 8TB
You can replace the date section with what you have.

You can replace the first half of the keyword section with what you have.

Also, you'll need to fix your field and control references. That's not what they're named in the database and form.
Jul 15 '14 #69
DK88
45
I'm getting an error "Extra in query expression". I hate to be so helpless, but could you post the exact SQL incorporating the date and keyword changes? It should allow the user to enter either date for their search, without needing both. With the SQL you posted, the txtEndDate isn't in the SQL at all, so entering an End Date does nothing. The code I just tried was:

Expand|Select|Wrap|Line Numbers
  1. SELECT Conversations.Project, Conversations.Contract, Conversations.ConversationDate, Conversations.Keyword, Conversations.Conversation
  2. FROM Conversations
  3. WHERE (Project=Forms![Conversation Search Form]!txtProject Or Forms![Conversation Search Form]!txtProject Is Null) And  (Contract=Forms![Conversation Search Form]!txtContract Or Forms![Conversation Search Form]!txtContract Is Null) And
  4. ((Conversations.[ConversationDate]) Between Nz(Forms![Conversation Search Form]!txtStartDate,#1/1/1900#) And Nz(Forms![Conversation Search Form]!txtEndDate,#9/9/2999#)) And
  5. ((Conversations.[Keyword]) Like "*" & Forms![Conversation Search Form]!Keyword & "*") Or Forms![Conversation Search Form]!txtKeyword Is Null);
which got the error and won't work.

If you could post the new SQL, I'd appreciate it very much. Cheers.
Jul 15 '14 #70
Rabbit
12,516 Expert Mod 8TB
You have an extra closing parentheses )

The last line should be
Expand|Select|Wrap|Line Numbers
  1. ((Conversations.[Keyword]) Like "*" & Forms![Conversation Search Form]!Keyword & "*" Or Forms![Conversation Search Form]!txtKeyword Is Null); 
Jul 15 '14 #71
DK88
45
Okay, we're getting sooo close. I took out the parentheses and the date sections work perfectly, but the keyword section doesn't filter anything. Searching anything yields all of the entries as positive results. I need it to search for any string of characters in the keyword column.

I.E. searching "oo" would yield "Good" and "food" and "Shoo" and "Oodles", but not any section that doesn't have "oo" consecutively.
Jul 15 '14 #72
DK88
45
I hate to say it, but I think there's still a problem with the SQL (besides the keyword syntax). I just realized that when you enter no criteria into the search form and then open up the results (by clicking either the table or report button), it doesn't show you the entries that have missing data. If no search criteria is entered at all, all of the entries should be considered positive results. Hopefully this is an easy fix? Is there some quick VBA code we could tie into the button that says that if no criteria is put into the form, all entries in the table are returned?

EDIT: Actually, the way the date is syntaxed, entering no criteria still yields results that have a date as positive (I think because the ConversationDate column in the table isn't explicitly part of the SQL, but rather split up into StartDate and EndDate), so I think it actually isn't a problem. There should always be a date entered with the purpose of this database, so I think it actually works out on its on. Phew.
Jul 15 '14 #73
Rabbit
12,516 Expert Mod 8TB
That's because the control on your form is named txtKeyword, not Keyword.

If you want the missing dates, then you can check if it's null.
Jul 15 '14 #74
DK88
45
That's it! It works exactly as I need it to work! :) Thank you very much to both of you for all of the help!! I will definitely be coming back to this site if I need any other Access help. Cheers!
Jul 15 '14 #75
twinnyfo
3,653 Expert Mod 2GB
Glad this one is finally resolved. Hope to see you again!
Jul 15 '14 #76

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

Similar topics

0
by: Rahat | last post by:
Hello, I have a database randomly scattered with 'NULL' values, being a newbie to mysql, I was wondering if there was a single command to replace all 'null' values with a string 'no data'. all the...
1
by: - TW | last post by:
I need some help with a query. I have a table with inventory that I need to allow customer searches on. Based on their search criteria, a preference level is calculated; the higher the preference...
5
by: MX1 | last post by:
Hi, I have a report with Name1 and Name2 in the address section . Sometimes, Name2 is not populated with data via the query that feeds the report. Unfortunately, the blank line stays in the...
2
by: New Guy | last post by:
I've been trying to look this up in Help but I've had no luck because I don't know what to call it. (That's not phrased very well but I think you get what I mean.) I have a report that is...
3
by: ShaeMills via AccessMonster.com | last post by:
In my table I imported from .txt, one of my columns has blank spaces. The column is as follows, how do I eliminate the blank spaces in between the second and third, and fifth and sixth digits? ...
0
by: LizRickaby | last post by:
My client has several Access databased that they wanted converted into MSSQL to be able to access them online (excuse the pun). I added the first Access database, LenderPrograms, as a table in the...
1
by: rokuk | last post by:
Hi there! I am trying to run the following simple query from within VBA: ""INSERT INTO tempTable SELECT * FROM Deletions"" When I run this from the Access 2003 query wizard in SQL view, it...
2
by: Bigdaddrock | last post by:
I have created an address field that consists of Address1; Address2; City, State and Zip. When all are populated this would create a three line record. However, when there is no Address2, I would...
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
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
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.