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

Filter code won’t work on pop-up form but will with Input boxes?

112 100+
I created a keyword filter to filter my form in 5 fields. I tried this code using two input boxes first and it works. Then tried it with a pop-up form and 2 unbound text boxes, and it doesn’t work now? What am I doing wrong? The code is supposed to filter through 5 fields if you fill in the first box but only 3 fields if you fill in both boxes. That is because the other two fields only have one word in them.

Code with Input Boxes:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Apply_Filter_Click()
  2. Dim MyValue As Variant
  3. Dim MyValue2 As Variant
  4.  
  5. MyValue = InputBox("Searches Fields: Object Name, Object Type, Subject/Image, Description, History", "Enter Keyword")
  6. MyValue2 = InputBox("Optional, Searches Fields: Subject/Image, Description, History", "Enter Second Keyword")
  7.  
  8.     If MyValue2 = "" Then
  9.  
  10.     Forms!frmDescription.Filter = "((Description) Like '*" & MyValue & "*') OR " & vbCrLf & _
  11.                                   "((History) Like '*" & MyValue & "*') OR " & vbCrLf & _
  12.                                   "(([Object Name]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  13.                                   "(([Object Type]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  14.                                   "(([Subject/Image]) Like '*" & MyValue & "*')"
  15.  
  16.     Forms!frmDescription.FilterOn = True
  17.  
  18.     Else
  19.  
  20.     Forms!frmDescription.Filter = "(((Description) Like '*" & MyValue & "*" & MyValue2 & "*' Or (Description) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  21.                                   "(((History) Like '*" & MyValue & "*" & MyValue2 & "*' Or (History) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  22.                                   "((([Subject/Image]) Like '*" & MyValue & "*" & MyValue2 & "*' Or ([Subject/Image]) Like '*" & MyValue2 & "*" & MyValue & "*'))"
  23.  
  24.     Forms!frmDescription.FilterOn = True
  25.  
  26.     End If
  27.  
  28. End Sub
Code with pop-up form:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Imput2_LostFocus()
  2.  
  3. Dim MyValue As Variant
  4. Dim MyValue2 As Variant
  5.  
  6. MyValue = Imput1
  7. MyValue2 = Imput2
  8.  
  9.  
  10.     If MyValue2 = "" Then
  11.  
  12.     Forms!frmDescription.Filter = "((Description) Like '*" & MyValue & "*') OR " & vbCrLf & _
  13.                                   "((History) Like '*" & MyValue & "*') OR " & vbCrLf & _
  14.                                   "(([Object Name]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  15.                                   "(([Object Type]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  16.                                   "(([Subject/Image]) Like '*" & MyValue & "*')"
  17.  
  18.     Forms!frmDescription.FilterOn = True
  19.  
  20.     Else
  21.  
  22.     Forms!frmDescription.Filter = "(((Description) Like '*" & MyValue & "*" & MyValue2 & "*' Or (Description) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  23.                                   "(((History) Like '*" & MyValue & "*" & MyValue2 & "*' Or (History) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  24.                                   "((([Subject/Image]) Like '*" & MyValue & "*" & MyValue2 & "*' Or ([Subject/Image]) Like '*" & MyValue2 & "*" & MyValue & "*'))"
  25.  
  26.     Forms!frmDescription.FilterOn = True
  27.  
  28.     End If
  29.  
  30. Me.Visible = False
  31.  
  32. End Sub
When I run this code there in no error but it just skips the first filter and runs the second one. If I comment out the second filter then it does nothing. I assume the problem is with the “IF” statement but I don’t know what? Any suggestions?
Jun 20 '13 #1
18 1718
TheSmileyCoder
2,322 Expert Mod 2GB
Line 6 and 7 of your second code:
Expand|Select|Wrap|Line Numbers
  1. MyValue = Imput1
  2. MyValue2 = Imput2
I don't see imput1 and imput2 defined anywhere?

Remember to turn on option Explicit, it will catch many errors during compile.
Jun 20 '13 #2
zmbd
5,501 Expert Mod 4TB
Just a follow up on TSC's post: >> Before Posting (VBA or SQL) Code. The information here will save you many a headache.
Jun 21 '13 #3
Redbeard
112 100+
Hi Guys thanks for the help.

I do have “Option Explicit” on and “Imput1 and Imput2” are my Unbound text boxes on the pop-up form. I am not sure how else to “Define” them? But I am sort of new at this, please let me know if I am doing it wrong. I have figured out the problem with my code, although I am not sure why it didn’t work and now it does. Especially since it worked with the input boxes? Here is what I changed on line 10:
Expand|Select|Wrap|Line Numbers
  1. If MyValue2 = "" Then
to
Expand|Select|Wrap|Line Numbers
  1. If IsNull(Imput2) Then
Can anyone tell me why this works? It is great to have it working but I want to know why?
Jun 21 '13 #4
zmbd
5,501 Expert Mod 4TB
me!imput1
Me is the shortcut reference to the form wherein the control is immediately located.

The reason it worked with the "input" code is because these returned the values from the user entry. In the case with the control/field "imput*", it is not being referenced correctly. The compiler is treating it as an undefined variable.

If you had ran the compile, the "imput1" should have flagged as an undefined variable, unless you have a Dim imput1 As (classtype) - in which case the variable would not have flagged as undefined.

This is what Rabbit mentioned within post#1.

It is essential that you follow the basic debugging steps as outlined in the link I provided you in post #3. We can not do this for you... we're not in possession of your PC/Project.
Jun 21 '13 #5
Redbeard
112 100+
Hi Zmbd
Thanks for your help. I did read the link you sent and I did run “Complie” and nothing happened. So I have now just done it again and nothing happen? It does not flag “Imput1 or Imput2” as undefined and the code works fine without putting “Me.” in front. I also don’t have “Dim Imput1 As (classtype)” in my code. So I guess one, I am confused as to why I didn’t have a problem without using either of these methods? And two, back to my previous question, why did I need to change Line 10 to get the code to recognize a Null in “Imput2”? I am trying to understand so I don’t make the same mistakes again which will lead to unnecessary posts.
Jun 21 '13 #6
zmbd
5,501 Expert Mod 4TB
Could you please do the following:
Open the VBA Editor <Alt><F11>
Tools/Options
List what options are selected in the "Editor" tab in the "Code Settings" group.
Jun 21 '13 #7
Redbeard
112 100+
I am running Access 2010 if that makes a difference. The options under the “Editor” tab that are checked are all of them. They are: Auto Syntax Check, Require Variable Declaration, Auto List Members, Auto Quick Info, Auto Data Tips, Drag-and-drop Text Editing, Default to Full Module View, Procedure Separator, Auto Indent and Tab Width: 4. I am also posting my completed code with the changes below. I have add a few more steps but it all runs fine.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Imput2_LostFocus()
  2.  
  3. Dim MyValue As Variant
  4. Dim MyValue2 As Variant
  5.  
  6. MyValue = Imput1
  7. MyValue2 = Imput2
  8.  
  9.  
  10.     If IsNull(MyValue2) Then
  11.  
  12.         If CurrentProject.AllForms("frmDescription").IsLoaded Then
  13.             Forms!frmDescription.Filter = "((Description) Like '*" & MyValue & "*') OR " & vbCrLf & _
  14.                                           "((History) Like '*" & MyValue & "*') OR " & vbCrLf & _
  15.                                           "(([Object Name]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  16.                                           "(([Object Type]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  17.                                           "(([Subject/Image]) Like '*" & MyValue & "*')"
  18.  
  19.             Forms!frmDescription.FilterOn = True
  20.  
  21.          ElseIf CurrentProject.AllForms("frmDescriptionPhoto").IsLoaded Then
  22.             Forms!frmDescriptionPhoto.Filter = "((Description) Like '*" & MyValue & "*') OR " & vbCrLf & _
  23.                                           "((History) Like '*" & MyValue & "*') OR " & vbCrLf & _
  24.                                           "(([Object Name]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  25.                                           "(([Object Type]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  26.                                           "(([Subject/Image]) Like '*" & MyValue & "*')"
  27.  
  28.  
  29.             Forms!frmDescriptionPhoto.FilterOn = True
  30.  
  31.          ElseIf CurrentProject.AllForms("frmDescriptionPublic").IsLoaded Then
  32.             Forms!frmDescriptionPublic.Filter = "((Description) Like '*" & MyValue & "*') OR " & vbCrLf & _
  33.                                           "((History) Like '*" & MyValue & "*') OR " & vbCrLf & _
  34.                                           "(([Object Name]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  35.                                           "(([Object Type]) Like '*" & MyValue & "*') OR " & vbCrLf & _
  36.                                           "(([Subject/Image]) Like '*" & MyValue & "*')"
  37.  
  38.  
  39.             Forms!frmDescriptionPublic.FilterOn = True
  40.         End If
  41.     Else
  42.         If CurrentProject.AllForms("frmDescription").IsLoaded Then
  43.             Forms!frmDescription.Filter = "(((Description) Like '*" & MyValue & "*" & MyValue2 & "*' Or (Description) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  44.                                           "(((History) Like '*" & MyValue & "*" & MyValue2 & "*' Or (History) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  45.                                           "((([Subject/Image]) Like '*" & MyValue & "*" & MyValue2 & "*' Or ([Subject/Image]) Like '*" & MyValue2 & "*" & MyValue & "*'))"
  46.  
  47.             Forms!frmDescription.FilterOn = True
  48.  
  49.         ElseIf CurrentProject.AllForms("frmDescriptionPhoto").IsLoaded Then
  50.             Forms!frmDescriptionPhoto.Filter = "(((Description) Like '*" & MyValue & "*" & MyValue2 & "*' Or (Description) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  51.                                           "(((History) Like '*" & MyValue & "*" & MyValue2 & "*' Or (History) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  52.                                           "((([Subject/Image]) Like '*" & MyValue & "*" & MyValue2 & "*' Or ([Subject/Image]) Like '*" & MyValue2 & "*" & MyValue & "*'))"
  53.  
  54.             Forms!frmDescriptionPhoto.FilterOn = True
  55.  
  56.         ElseIf CurrentProject.AllForms("frmDescriptionPublic").IsLoaded Then
  57.             Forms!frmDescriptionPublic.Filter = "(((Description) Like '*" & MyValue & "*" & MyValue2 & "*' Or (Description) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  58.                                           "(((History) Like '*" & MyValue & "*" & MyValue2 & "*' Or (History) Like '*" & MyValue2 & "*" & MyValue & "*')) OR " & vbCrLf & _
  59.                                           "((([Subject/Image]) Like '*" & MyValue & "*" & MyValue2 & "*' Or ([Subject/Image]) Like '*" & MyValue2 & "*" & MyValue & "*'))"
  60.  
  61.             Forms!frmDescriptionPublic.FilterOn = True
  62.  
  63.         End If
  64.     End If
  65.  
  66. Me.Visible = False
  67.  
  68. End Sub
Jun 22 '13 #8
Mihail
759 512MB
I think that the answer for you is here
Jun 22 '13 #9
zmbd
5,501 Expert Mod 4TB
Mihail,
I'm not sure how the link you provided applies to this issue.

Redbeard,
At Line # 8 of the code as given in post# 8
Please insert
Expand|Select|Wrap|Line Numbers
  1. debug.print "From Imput#1 = " & _ 
  2.      nz(MyValue,"null") & _
  3.      vbcrlf & "From Imput#2 = " & _ 
  4.      nz(MyValue2,"null")
Once your code has ran or if it fails and goes into the debug mode press <Ctrl+G>
You should see the resolved values for your variables in the immediate window. Please do a standard copy and paste into post within this thread. Please run it a few times with differing user input - I'd like to see what value(s) you are returning, if anything.

Because you are building your criteria string directly within in the filter you've made it impossible to troubleshoot what is happening - not really your fault per say, it is the way that most of the examples show to do it... and (IMHO) it is teaching new programmers a lazy coding method as one doesn't define a string variable first and then work thru the resolved string if trouble develops.
Jun 24 '13 #10
Mihail
759 512MB
@ZMBD
I have provide that link because I think that is probably that Redbeard to have the same problem as me with the bounded textboxes.

I spent 2 hours to discover the warm water:
The Textbox.Value remain equal with the record's field value (whatever you type in) until the record is updated (saved).
So, if the field value in a record is NULL, the Textbox.Value will be NULL even if the user will type one thousand characters. Only the TextBox.Text has been changed as you type.

Of course: not sure that this is the problem here. Just I suspect that.
Jun 24 '13 #11
zmbd
5,501 Expert Mod 4TB
MiHail,
I can see where you would think the issues related.

Please regard the code in both OP and #8 - where in fact are "Imput1" and "Imput2" defined?
As used in #8, these are not defined at any-point as being related to the unbound control in that neither the me!imput* nor the more formal "Forms!...." method are used (or the "dot" nomenclature). So the question(s) become: why isn't the compiler failing this and what values are actually being returned by "imput*"
Jun 24 '13 #12
Redbeard
112 100+
Hi zmdb
I have put your code in the appropriate place and I can't get your code to run... it keeps getting stuck on Line 2. Specifically on "Nz" it is highlighted and I get a “Compile Error” that reads "Wrong Number of arguments or invalid property assignment". When I hit <CtrlG> it goes to the “Immediate” window but nothing comes up? I assume that is because it won't go pass the “Nz”?
Jun 24 '13 #13
zmbd
5,501 Expert Mod 4TB
Opps, sorry about that, I was away from my normal PC and working on a different project and trying to handle my four kids... gota-love'em!

I've fixed the code in post#10.... please try it again using the correct code :)
Jun 24 '13 #14
Redbeard
112 100+
Hey no problem... I know what kids are like.
Here are the value that I got, they are exactly what I typed into the unbound text boxes:

From Imput#1 = print
From Imput#2 = black

From Imput#1 = print
From Imput#2 = null

From Imput#1 = null
From Imput#2 = print

From Imput#1 = john
From Imput#2 = smith

Code is still running fine.
Jun 24 '13 #15
zmbd
5,501 Expert Mod 4TB
Ok,
I have recreated to some extent what you are seeing...
I am impressed that V2010 has this much forgivness in the coding!

What I've done to recreate the issue created a form with two unbound textboxes, named them imput1 and imput2, then used my basic default print this...

Note in the VBA code that imput1 and imput2 are not defined... the compiler is finding the names in the form and using that information.
If I change either the control names, or the variable names (say change the variable from imput1 to imput5 or change the control name to "z_txt_imput1") without changing the other to match then the compiler chokes.

I've not seen this before, yet here's the proof:

Attached Images
File Type: jpg BytesThread950100_recreate.jpg (44.6 KB, 245 views)
Jun 24 '13 #16
Redbeard
112 100+
So does that mean that "Me." is now not needed in the code since it will pick up any name that is listed on the form and use it? Or would there be an instance where you could get fouled up by not using "Me."? I would like to use best practices, to avoid any problems in the future.

By the way zmbd, thanks for following through and investigating this, it is appreciated.
Jun 24 '13 #17
zmbd
5,501 Expert Mod 4TB
I'd still use the "Me." and "Me!" constructs. What happens if we place this into a Form/SubForm situation... Say in this case we have a control named imput99 on both the parent form and the subform, now what? Given the code is in the SubForm class module would this pull the value fromt he SubForm, Parent, or will there be a compiler error?

IMHO: It is always better to be explicit in how things are defined and named. It makes the code much easier to follow for both yourself and anyone that comes behind you later that has to maintain it.

It very well could be that this is a bug in the V2010 compiler that might be fixed via an update or in the V2013 that is already in the wild (or future release).
Jun 24 '13 #18
Redbeard
112 100+
Thanks zmbd for all the help! I will add the "Me." and continue that as my practice.
Jun 25 '13 #19

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

Similar topics

2
by: Joey | last post by:
Say a customer inserts into a sql database field (NUMINSERTS) the number 6. On the following page, I want to build a table that displays 6 input boxes, since the customer said they wanted 6 text...
3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
3
by: williamc | last post by:
Somebody asked me if it would be possible to add auto-advance to a web form where there are a lot of repetitive 5 character fields. I took a look around the web and found a script, which appears to...
7
by: Nathan Bloomfield | last post by:
Hi All, I am hoping someone out there will be kind enough to find out where my code is going wrong. The current code is inefficiant but hopefully it will convey the data I require to be...
2
by: Tony Williams | last post by:
I have this code behind a command button but it doesn't delete the record. If vbNo = MsgBox("Are you sure you want to save this new record?", 4) Then DoCmd.DoMenuItem acFormBar, acEditMenu, 8, ,...
4
by: Macbane | last post by:
Hi, I have a 'main' form called frmIssues which has a subform control (named linkIssuesDrug) containing the subform sfrmLink_Issues_Drugs. A control button on the main form opens a pop-up form...
2
by: NDayave | last post by:
How do, A while ago i had the problem of backing up a Access 2002 table with unique data that changed in some tables but not others, resulting in restore failure. This was sorted by NeoPa with the...
1
by: Cara Murphy | last post by:
Hi There! Hoping you are able to help me with a filtering scenario: I have created a pop up form (Filter) to filter a report (Open Issues), which currently has a number of combo boxes which...
10
omerbutt
by: omerbutt | last post by:
hi there i am working on an inventory rpoject and for that i have made a stock entry form which is pasted below,I have included this form in the main Add Stock page, having the following inputs to be...
5
by: Die Klein | last post by:
ive been trying to get this code to work for a few days now, but i dont know what im doing wrong :S im basicaly trying to create a login that will allow members to login to their details. the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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: 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,...

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.