VBA CODE HELP on boolean (1 Viewer)

mikey222

Registered User.
Local time
Today, 15:49
Joined
Nov 4, 2008
Messages
40
Hi everyone i have a search engine and for my products with boolean yes/no options to filter the results.



i currently use

If Me.filterirregular = -1 Then
strWhere = strWhere & "([irregular] = True) AND "
ElseIf Me.filterirregular = 0 Then
strWhere = strWhere & "([irregular] = False) AND "
End If

but seems to be that its not working properly, i just copied this vb code from other examples.

could anyone give me a better vb code to sort out whether the products are irregular??

appreciate
 

irish634

Registered User.
Local time
Today, 18:49
Joined
Sep 22, 2008
Messages
230
Try using the words "True" and "False" instead of the integers.

If Me.filterirregular = TRUE Then

ElseIf Me.filterirregular = FALSE Then

End IF
 

DJkarl

Registered User.
Local time
Today, 17:49
Joined
Mar 16, 2007
Messages
1,028
Most combo boxes in VBA have 3 states, true, false and NULL.

Unless you need to check all three states just check for the true and default all other's to an else.

If Me.filterirregular = -1 Then
strWhere = strWhere & "([irregular] = True) AND "
Else Then
strWhere = strWhere & "([irregular] = False) AND "
End If
 

mikey222

Registered User.
Local time
Today, 15:49
Joined
Nov 4, 2008
Messages
40
thank you for the reply

i've changed my code to


If Me.filterirregular = True Then
strWhere = strWhere & "([irregular] = True) AND "
Else
strWhere = strWhere & "([irregular] = False) AND "
End If

and it works

but when i search the database with several different tick boxes the result does not work anymore



this is an example and i have

If Me.filterpopup = True Then
strWhere = strWhere & "([popup] = True) AND "
Else
strWhere = strWhere & "([popup] = False) AND "
End If

If Me.filterbrandlogo = True Then
strWhere = strWhere & "([irregular] = True) AND "
Else
strWhere = strWhere & "([irregular] = False) AND "
End If

and when i search it does not work as it should be...is it something to do with the code?

i've upload the file in 2007 format (4mb) and there are some chinese characters, you could just ignore it.

https://www.yousendit.com/download/TTZuS3d0RkVRR2MwTVE9PQ

appreciate the help! thank you
 

jal

Registered User.
Local time
Today, 15:49
Joined
Mar 30, 2007
Messages
1,709
Have you done a

MsgBox(StrWhere)

to verify it's turning out as planned? Have you truncated the final " AND " from your StrWhere ?
 

wazz

Super Moderator
Local time
Tomorrow, 06:49
Joined
Jun 29, 2004
Messages
1,711
please do not post the same thing in different places. thanks.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:49
Joined
Sep 12, 2006
Messages
15,660
various points

- you should paste your code, not type it in - this code wont compile as you have ELSE THEN which is a syntax error

- you also have a trailing AND at the end of your sql - is this causing the problem?

note also , that it MIGHT just be that there is no space before the ([irregular.
SQL is unforgiving of syntax errors


ignoring these, try this slightly different syntax, so that true and false are inserted by the compiler, rather than by your text.

Code:
'use true rather than -1
If Me.filterirregular = true Then
  'see the EXTRA space before the " (IRREGULAR
  strWhere = strWhere & " ([irregular] = " & TRUE & " AND "
Else 'you had else then
  strWhere = strWhere & " ([irregular] = " & FALSE & " AND "
End If


also how are you calling this SQL?

1. display it first in a msgbox to make sure it looks right
2. error trap it to see EXACTLY what is causing the problem
 

mikey222

Registered User.
Local time
Today, 15:49
Joined
Nov 4, 2008
Messages
40
First of all sorry for violating the rules in the forum and thank you those who have replied my post.

I've followed husky's code and it works much better now. Now the system won't show the result if the product contains two or more TRUE values when one tick box is choosed from the search.

eg. irregular is ticked

products that are true in irregular and popup would not show, products that are ONLY irregular would display in the search result

Code:
If Me.filterpopup = True Then
  'see the EXTRA space before the " (IRREGULAR
  strWhere = strWhere & " ([popup] = " & True & ") AND "
Else 'you had else then
  strWhere = strWhere & " ([popup] = " & False & ") AND "
End If

Thank you so much for the help appreciate...^^:)
 

jal

Registered User.
Local time
Today, 15:49
Joined
Mar 30, 2007
Messages
1,709
I've followed husky's code and it works much better now. Now the system won't show the result if the product contains two or more TRUE values when one tick box is choosed from the search.

eg. irregular is ticked

products that are true in irregular and popup would not show, products that are ONLY irregular would display in the search result

You're running into the following filter-problem. Let's consider cars.
Suppose the categories are as follows:

Brands: Ford, Mazda, Nissan
Style: Truck, Sedan, or SUV
Colors: White, Black, or Red


Let's suppose the user selects Truck. He wants a list of ALL trucks. Well, he can't get that, because the first category forces him to choose either Ford, Mazda, or Nissan. He can only get, say, Ford trucks onscreen. And he has to pick a color, so he can only get, say, white trucks. The solution is to add a No-Filter option to each category

Brands: Ford, Mazda, Nissan, All Brands (no-filter)
Style: Truck, Sedan, SUV, All Styles (no filter)
Colors: White, Black, or Red, All Colors (no filter)

In the long run, this is less confusing to the user. Interestingly, Excel auto-filtering works the same way. In your case you'll need something just like the above. You could use threee radio buttons for example (six in total)

Regular, Irregular, Both (no-filter)
Popup, Non-popup, Both (no-filter)

I don't really understand you're data so this might not be quite accurate. I'm just trying to point you in the right direction.

 

mikey222

Registered User.
Local time
Today, 15:49
Joined
Nov 4, 2008
Messages
40
Thank you for replying

i kinda understand what you trying to point me to but in terms of code i dont know how to command the system to perform with the criteria (BOTH) so it wont search from one level to the next

appreciate the effort to help out ^^:)
 

jal

Registered User.
Local time
Today, 15:49
Joined
Mar 30, 2007
Messages
1,709
Thank you for replying

i kinda understand what you trying to point me to but in terms of code i dont know how to command the system to perform with the criteria (BOTH) so it wont search from one level to the next

appreciate the effort to help out ^^:)

That should be the easy part. As stated, "Both" means NO FILTER - this means your code should do nothing in the sense of, it should not append another clause to the filter string.

I am confident you will find it easy to "do nothing at all."
 

mikey222

Registered User.
Local time
Today, 15:49
Joined
Nov 4, 2008
Messages
40
ok but exactly how do i use the command BOTH onto my example?

when i check the box pop up the following command shows up

([popup] = True) AND ([rock] = False) AND

so that is why a product with popup and rock feature would not show up unless i check both the boxes...

emmm :confused::confused::confused::confused:
 

CyberLynx

Stuck On My Opinions
Local time
Today, 15:49
Joined
Jan 31, 2008
Messages
585
Try this:

Code:
Dim strFilterCriteria As String

strFilterCriteria = ""

strFilterCriteria = strFilterCriteria & "[irregular] = " & Me.filterirregular.Value

If Len(strFilterCriteria) <> 0 Then strFilterCriteria = strFilterCriteria & " AND "

strFilterCriteria = strFilterCriteria & "[popup] = " & Me.filterpopup.Value


Forms("[I][COLOR="Red"]MyMainFormName[/COLOR][/I]")("[COLOR="Red"][I]MySubForm[B]Control[/B]Name[/I][/COLOR]").Form.Filter = strFilterCriteria
Forms("[I][COLOR="Red"]MyMainFormName[/COLOR][/I]")("[COLOR="Red"][I]MySubForm[B]Control[/B]Name[/I][/COLOR]").Form.FilterOn = True

Replace the names in italic red with their proper names.

Ummm...what's confusing me at this point is....where did [rock] come from :)

.
 
Last edited:

mikey222

Registered User.
Local time
Today, 15:49
Joined
Nov 4, 2008
Messages
40
Thank you for replying...

seems like its not solving the problem at all

i've created a test sample if you want to take a look

its a 2007 version...

View attachment test.zip

Code:
Private Sub cmdfilter_Click()

 Dim strFilterCriteria
strFilterCriteria = ""

strFilterCriteria = strFilterCriteria & "[irregular] = " & Me.filterirregular.Value

If Len(strFilterCriteria) <> 0 Then strFilterCriteria = strFilterCriteria & " AND "

strFilterCriteria = strFilterCriteria & "[popup] = " & Me.filterpopup.Value


Forms("test").Form.Filter = strFilterCriteria
Forms("test").Form.FilterOn = True


End Sub

thanks for the help
 

mikey222

Registered User.
Local time
Today, 15:49
Joined
Nov 4, 2008
Messages
40
now i tried using

Code:
 If Not IsNull(Me.filterpopup) Then
        strWhere = strWhere & "([popup] = " & True & ") AND "
    Else
    strWhere = strWhere & "([popup] = " & False & ") AND "
    End If
        
 If Not IsNull(Me.filterirregular) Then
        strWhere = strWhere & "([irregular] = " & True & ") AND "
    Else
    strWhere = strWhere & "([irregular] = " & False & ") AND "
    End If

NOW only products that have both the criteria would show up...

i'm totally lost in finding a solution

how could i command the access to ignore doing a filter IF the checkbox is not checked??
 

jal

Registered User.
Local time
Today, 15:49
Joined
Mar 30, 2007
Messages
1,709
now i tried using

Code:
 If Not IsNull(Me.filterpopup) Then
        strWhere = strWhere & "([popup] = " & True & ") AND "
    Else
    strWhere = strWhere & "([popup] = " & False & ") AND "
    End If
 
 If Not IsNull(Me.filterirregular) Then
        strWhere = strWhere & "([irregular] = " & True & ") AND "
    Else
    strWhere = strWhere & "([irregular] = " & False & ") AND "
    End If

NOW only products that have both the criteria would show up...

i'm totally lost in finding a solution

how could i command the access to ignore doing a filter IF the checkbox is not checked??

I've not even bothered examining your recent code because you seem to have missed the whole point of what I said. My claim is that your approach was flawed. You need a THIRD OPTION (six in all) for the user. That's what I said. A checkbox only allows two options (checked and unchecked) except for tristate checkboxes (which may prove confusing to the user). That's why I suggested using RADIOBUTTONS (called Option Buttons in VBA).

The user NEEDS TO SEE six options on screen: The first set of three options buttons is this:

Irregular Items, Regular Items, All Items (both regular and irregular)

The logic must force the user to choose one of these three.
The second set will be this:


Popup Items, Non-popup Items, All Items (both popup and non-popup).


The logic must force the user to choose one of these three.



In total, the logic must force the user to checkmark two option buttons (one from each set). No more, no less.
 

jal

Registered User.
Local time
Today, 15:49
Joined
Mar 30, 2007
Messages
1,709
I've uploaded an example. You may be able to copy and paste the option buttons directly from my form onto yours (I'm not sure).

Also, here's a section from my notebook which explains how to use option buttons (you shouldn't need it as I already wrote the code for you).

------------------ start of option Group control --------------------
start of group box
start of radiobutton
WARNING: If two option groups are close together, they might not work right. Keep them far apart. Also, if you want to change the backcolor, you may need to change the special effect to Raised in order for the new color to show up.

- In a VBA UserForm (as opposed to an Access Form), option group works different than in Access - works like a VB.net groupbox. Yes !!!



- one problem with an Access optin group is that when you drag it, the controls don't drag with it. You might be better off using a subform, if all you need is a draggable group box to encircle the controls.
Option Group is a group box into which you can drop radio buttons or checkboxes.
An option button is just a radiobutton or a checkbox.

If you DRAG radiobuttons into an OptionGroup, the OptoinGroup will behave like a mere rectangle (where all the radiobuttons can be checkmarked at once). But if you DROP radiobuttons into an OPtionGroup (using Control-V to paste them), it will behave like a true OptionGroup. To insure that the RBs fall into the right place, make sure the target OptionGroup is selected and then use Control-V to paste. WARNING: Although you can paste the radio buttons this way, you cannot paste textboxes and labels this way - these you have to drag.



(By the way, a toggle button is simply a radiobutton. Instead of having a bullet, it changes color when you select it).
Suppose you want multiple checkboxes selectable at once. Don't use an option group. Put them in a rectangle.
Thus a rectangle is a group box that allows for multiple radiobuttons or checkboxes to be selected at once.
- by the way, an optionGroup is named Frame1 in the designer.
Handle the After_Update event of the Option Group. If you like, you can caption the optionGroupBox1.Value property, which will be set to the radiobutton.Text value.
alternvatively, loop through all the radiobuttons (For each control in ...)
Dim ctl As Control
For Each ctl In grpOption.Controls
If ctl.ControlType = acOptionButton Then...
Next ctl

You cannot checkmark the radiobutton (called an Option Button0) directly. If there are 3 radio buttons, setting the group box to a vaue of 3 will checmkark the third one. You can do this during Form1_Open
optionGroup1 = 3
Or you can set the option groups "Default VAlue" in the designer, to 3. Note, also, that the third button doesn't necessarily have to associate with 3. You can associate it insstead with 2, if you like, by setting its option value to 2. Then you could set the second button's option value to 3.


How to change the color of a label. Also it demonstrats the use of an option group. For each radiobutton, set the OptionValue to a different number (in this case 1 to 4) so that when the user selects a radiobutton, the OptionGroup will value to the selected number, so that you can track his selectin.
Private Sub optNatureAndExtentGroup_AfterUpdate()
If Me.optNatureandExtentGroup = 1 Then
Me.lblHeadNeck.BackColor = vbYellow

ElseIf Me.optNatureandExtentGroup = 2 Then
Me.lblRightShoulder.BackColor = vbYellow
ElseIf Me.optNatureandExtentGroup = 3 Then
Me.lblLeftShoulder.BackColor = vbYellow

ElseIf Me.optNatureandExtentGroup = 4 Then
Me.lblChest.BackColor = vbYellow

End If

VBA has no TypeOf or GetType, instead use this:
If ctl.ControlType = acOptionButton Then...
Oddly, ctrl.ControlType doesn't show in Intellisense.


As far as to change the color, if the BackStyle is set to Transparent (i.e. zero), the color will be ignored because the parent color wins. If you change the color using code, be sure to add an extra line of code specifying Normal instead of Transparent
optionGroupCmbs.BackColor = vbBlack
optionGroupCmbs.BackStyle = 1 'Means 'Normal'

------------- end of option Group ---------------
 

Attachments

  • Option Buttons.zip
    14.9 KB · Views: 127

mikey222

Registered User.
Local time
Today, 15:49
Joined
Nov 4, 2008
Messages
40
jal thank you so much for trying to help out

i checked out your example and i now understand what ya talking about

i've changed all my check boxes to pull down menu with a true value in every of my criteria. Surprisingly it works...

your solution is very handy too but it might takes extra time for users choose between TRUE and FALSE everytime they search a product.

maybe the search criteria does is not suitable but i'm sure it will be very useful when i create similar database.

未命名.jpg

i really appreciate the time and effort to help out. many thanks!
 

jal

Registered User.
Local time
Today, 15:49
Joined
Mar 30, 2007
Messages
1,709
jal thank you so much for trying to help out


i've changed all my check boxes to pull down menu with a true value in every of my criteria. Surprisingly it works...
Good for you. Glad you got it working.

i checked out your example and i now understand what ya talking about
...your solution is very handy too but it might takes extra time for users choose between TRUE and FALSE everytime they search a product.
I think you completely misunderstood. The user isn't to choose between "True and False' - he never sees that. He just chooses the desired radiobuttons. Yes, I put "True and False" on the screen but that it is only temporary - for debugging purposes - that was for YOU to see, not for them to see (the idea was for you to remove that msgBox from the code once you got it working).

If you have found a way to offer them all six options without actually putting six options in your (drop-down) menu, then I can only say that I am impressed, and am left scratching my head as to how that is possible.

AnyWho, Congratulations.
 
Last edited:

Users who are viewing this thread

Top Bottom