8 Replies - 1224 Views - Last Post: 21 September 2012 - 07:45 AM Rate Topic: -----

#1 katie1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-July 12

Have more than one checkbox ticked vb6?

Posted 14 September 2012 - 07:04 AM

I have a program which, when clicking Search, displays a list of products. When creating a new product there is an "Active" checkbox which is automatically set to True. The user can edit any product and, if desired, change the product to "Inactive".

On the search form, there are two checkboxes: Active and Inactive.
When the program loads the Active checkbox is always ticked. If the user wants to view inactive products as well the should be able to have both ticked.

At the moment when I have active ticked, the listbox displays all active items and when I have Inactive only ticked it display Inactive items. However when I have both ticked, only inactive products are shown.

This is my code:


Private Sub cmdProdSearch_Click()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim list_item As ListItem
Dim itm As ListItem


db_file = db_file & "ProductsDatabase1.mdb"
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ProductsDatabase1.mdb;Persist Security Info=False" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
conn.Open

Set rs = conn.Execute("Select * from Export where Product like '%" & txtProduct.Text & "%'")
If chkActive.value = 1 Then
Set rs = conn.Execute("Select * from Export where Inactive = '0'")
Else
Set rs = conn.Execute("Select * from Export where Inactive = '1'")


ListView1.ListItems.Clear

With ListView1
.View = lvwReport
.FullRowSelect = True
Do While Not rs.EOF
Set itm = .FindItem(txtProduct.Text, lvwText, , lvwPartial)
Set list_item = .ListItems.Add(, , rs!Product)
list_item.SubItems(1) = rs!barcode & ""
list_item.SubItems(2) = rs!quantity & ""
list_item.SubItems(3) = rs!Department & ""
list_item.SubItems(4) = rs!Active & ""
list_item.SubItems(5) = rs!Inactive
rs.MoveNext
Loop
End With
End If

End Sub




Also I am using an Access database to store product info, and am using SQL statements to find information.

Any suggestions as to be able to display bot Active and Inactive items when both checkboxes are ticked?
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Have more than one checkbox ticked vb6?

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,796
  • Joined: 26-March 09

Re: Have more than one checkbox ticked vb6?

Posted 14 September 2012 - 08:08 AM

Katie, you only seem to be checking the one checkbox before doing your query.

Maybe using something like below would work??:

' If both selected, then return all records with no filtering
If chkActive.value = 1 and chkInActive.value = 1 Then

  Set rs = conn.Execute("Select * from Export")

elseif chkActive.value = 1 then
  ' If active is checked, return only active records
  Set rs = conn.Execute("Select * from Export where Inactive = '0'")

Elseif chkActive.value = 0 then
  
  Set rs = conn.Execute("Select * from Export where Inactive = '1'")

else
  'No checkboxes selected, tell user to select one
  msgbox "You need to check at least one box"
endif


Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Have more than one checkbox ticked vb6?

Posted 15 September 2012 - 08:18 PM

But what about the business about selecting a product? Surely the OP didn't intend to have that bit of code ignored. How about
dim sql as string
[check for neither box checked and handle it]
sql = "Select * from Export where Product like '%" & txtProduct.Text & "%'"
if chkActive.value XOr chkInactive.value then
   sql = sql & " where inactive = '" & IIf(chkActive.value = vbChecked, 0, 1) & '"
end if
rs.open sql, conn
The xor is a concise way of asking if one or the other is checked but not both.
Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,796
  • Joined: 26-March 09

Re: Have more than one checkbox ticked vb6?

Posted 16 September 2012 - 12:43 AM

I must of misread it, I thought the idea was to be able to select both active and inactive, as well as the individual options.
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Have more than one checkbox ticked vb6?

Posted 20 September 2012 - 03:01 PM

Well, I was just trying to make sense out of the logic in lines 16 to 20 of the OP's code. Perhaps you can have a go at that as well?
Was This Post Helpful? 0
  • +
  • -

#6 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,796
  • Joined: 26-March 09

Re: Have more than one checkbox ticked vb6?

Posted 21 September 2012 - 12:00 AM

View PostBobRodes, on 20 September 2012 - 11:01 PM, said:

Well, I was just trying to make sense out of the logic in lines 16 to 20 of the OP's code. Perhaps you can have a go at that as well?


I think the OP, should use a combination of both in that case, I didn't really look at the product side of things, I was just concentrating on the Active/Inactive part of the question. :)


Something like below....not as slick as your's I know! ;)


' If both selected, then return all records with no filtering
If chkActive.value = 1 and chkInActive.value = 1 Then

  Set rs = conn.Execute("Select * from Export where Product like '%" & txtProduct.Text & "%'")

elseif chkActive.value = 1 then
  ' If active is checked, return only active records
  Set rs = conn.Execute("Select * from Export where Product like '%" & txtProduct.Text & "%' AND Inactive = '0'")

Elseif chkActive.value = 0 then
  
  Set rs = conn.Execute("Select * from Export where Product like '%" & txtProduct.Text & "%' AND Inactive = '1'")

else
  'No checkboxes selected, tell user to select one
  msgbox "You need to check at least one box"
endif

Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Have more than one checkbox ticked vb6?

Posted 21 September 2012 - 07:03 AM

Well ain't dat wot I done? Why you fix if ain't broke? :dontgetit:

This post has been edited by BobRodes: 21 September 2012 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,796
  • Joined: 26-March 09

Re: Have more than one checkbox ticked vb6?

Posted 21 September 2012 - 07:30 AM

View PostBobRodes, on 21 September 2012 - 03:03 PM, said:

Well ain't dat wot I done? Why you fix if ain't broke? :dontgetit:


You know what Bob, I'm having a bad day, it is the same....I need to learn how to read again! :S
Was This Post Helpful? 0
  • +
  • -

#9 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Have more than one checkbox ticked vb6?

Posted 21 September 2012 - 07:45 AM

See, I was proud of my XOr thing, don't get to use it very often. So I had to stick up for it... :lol:


Quote

You know what Bob, I'm having a bad day
I dunno, you might want to check your other posts before you opine thusly.

This post has been edited by BobRodes: 21 September 2012 - 07:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1