[RESOLVED] Is There a batter way to implement Checkboxes?
I am basically writting a program to assign inventory to a patient. The inventory is being added to the database by checkboxes. So when they click each of the check boxes, 1 is added to each of their equipment signed out. and inventory is decremented by 1 for each equipment.
When the Submit button is clicked everything is added to the DB.
Question:
As I am writting this, I realised if there was 100 pieces of equipment, that would take me forever to write. I am only using 20 equipments--i.e checkboxes. And if the company ever changes their equipment, I would have alot of rewritting to do.
The code below would have to be written for each checkbox selected. Is there a better way?
Here is what I came up with.
VB Code:
[B]//code for the actual checkbox[/B]
Private Sub chkbed_Click()
bedchk = False
'vbchecked is a reserved word
chkBed.Value = vbChecked
bedchk = True
End Sub
[B]
//adds to the patients equipment in the database[/B]
'customers only get one thing of each equip
add = 1
'Adding equipment to patient
With rspatientequip
.AddNew
id = getCustID
!CustId = id
If addequip = True Then
!bed = "bed"
End If
rspatient.Update
End With
[B]//code for updating inventory[/B]
Dim id As String
'customers only get one thing of each equip
reduce = 1
'taking away from inventory, Updates inventory field UNitIn Stock - reduce(1)
Re: Is There a batter way to implement Checkboxes?
Originally Posted by ched35
... whats the diff with your way and what I had? ...
chkBed.Value get assigned when you click on the checkbox (same goes for option buttons) - you don't need to explicitely do that on click. You may (as I showed you) set some flags, get current value and proceed as you need to.
The way you have is a good way to run out stack/memory...
Re: Is There a batter way to implement Checkboxes?
Originally Posted by ched35
hmmm, whats the listview? can you explain
You can say ListView is an extended version of ListBox. Go to menu Project>>Components, and search for MS Windows Common Controls 5 or 6. Also, there's a complete guide to the ListView control in MS's MSDN.
Re: Is There a batter way to implement Checkboxes?
Originally Posted by gavio
You can say ListView is an extended version of ListBox. Go to menu Project>>Components, and search for MS Windows Common Controls 5 or 6. Also, there's a complete guide to the ListView control in MS's MSDN.
I looked at some examples but still don't know if that will help.
basically my form has in it.
info at the top to see the current patient
at the bottom of the screen there are checkboxes to select equipment for that patient to sign out.
Re: Is There a batter way to implement Checkboxes?
ok, I found a good example--finally. Most of the examples I found didn't work or load up.
so with a list view I can load all the equipment to the list. Now when a user selects or highlights them, how can I change inventory values for the DB.
Re: Is There a batter way to implement Checkboxes?
Originally Posted by ched35
ok, I found a good example--finally. Most of the examples I found didn't work or load up.
so with a list view I can load all the equipment to the list. Now when a user selects or highlights them, how can I change inventory values for the DB.
changes occur after the submit buttons is hit.
thanks,
You iterate through the ListItems collection and test the Checked property of each ListItem... if its checked then you update the count in the DB for that item, so its best to store the primary key of the record in the ListItem.Tag property for sql reference purposes.
Re: Is There a batter way to implement Checkboxes?
Here is what I have now for the listview. The list isn't populating for some reason. I am trying to read values from a database the inventory table. to the listview.
I am not getting any errors.
in project->components-> I added ADO data control
datagrid control
datalist control
windows common controls
Here is my module that I used for the connections. The whole project uses this.
Re: Is There a batter way to implement Checkboxes?
for the example program I mentioned, I used my DB instead of the Nwind one. The values do not get read in. Wierd, and the column names are the same somehow.
Does any one have a good example program for reading in from a database to a listview control?
Re: Is There a batter way to implement Checkboxes?
I attached the sample program with my DB, I just called Nwind...not the real name.
I figured out why its not working. IN the column heads there is:
EmployeeID LastName FirstName etc.....
These are the fields name in the DB. I just my DB to reflect and it worked
Looking through the code I haven't the slightest Idea where those columns are coming from. What's even stranger, I changed to a different DB(included).
Can someone tell me where these column names are coming from?
Re: Is There a batter way to implement Checkboxes?
quick question, how do I get the values that are checked in a list view, so that when an item is checked the item in the DB is decremented when the SUBMIT button is clicked on.
the post from earlier said something about the ProductID's Key....how do I do that.
Re: Is There a batter way to implement Checkboxes?
basically I want to have the selected items from the list:
what ever is selected, from the inventory table, I want the UnitInStock Decremented
and from the patientequipment table, I want the equipment thats selected to be +1 in the equipment columns.
I am trying to modify your way, might take me awhile.
Re: Is There a batter way to implement Checkboxes?
ok, I came up with this and it only works for the first record in UnitInStock--can't figure out how to use the i from the for loop. Is there a way to use the i value to jump to the next record.
is there a way to refresh the listView once values have been changed. i.e submit button is clicked.
VB Code:
'go through each item
For i = 1 To ListView1.ListItems.Count
'set the first value to lvwitem, then do again with 2,3,4...
Set lvwItem = ListView1.ListItems.item(i)
'if the item is checked
If lvwItem.Checked = True Then
'gets the values from under the Column UnitInStock
tmp = ListView1.ListItems.item(i).ListSubItems(2)
With rsinventory
.MoveFirst 'i tried to use .index(i) that didnt work...whats the proper way
Re: Is There a batter way to implement Checkboxes?
If you stored the primary keys in the listitem.tag property then you could iterate through listitems and collect these IDs (comma separated). You will use these IDs in your WHERE clause for your update query... below is a decrement
UPDATE table SET inventory = inventory - 1 WHERE ID IN (100,101,200)
while this is an increment
UPDATE table SET inventory = inventory + 1 WHERE ID IN (100,101,200)
Re: Is There a batter way to implement Checkboxes?
Originally Posted by ched35
i tried your code but I am getting an error. object doesnt support this property or method
it doesnt like lstYourItem.
I don't understand what you wrote, could you explain.
For the tag property, Do you mean when I click on properties for the listview and assign a value in the TAG under coulumn headers.
I assigned column 3 UnitInStock the tag of 100.
what is lstYourItem? is that the same as my lvwItem //tried this too and got the same error.
What is DaColumn?
whats the DaPrimaryKey?
thanks for helping.
I declared a listitem object and a string.
I then iterated through the listview's listitems collection, (For Each lstItem In ListView1.ListItems)
Everytime I encountered a listitem that's checked, I concatenate the value stored in the Tag property (which holds ProductID) to strTemp.
The ProductIDs in the string are comma separated with a leading comma. So I used Mid(strTemp,2) to discard the leading comma.
I then created the query, using the comma separated product IDs in the WHERE clause. Since the CSV is a list of comma separated Product IDs then your WHERE clause becomes WHERE ProductID IN (productID_CSV)
If the query didn't work then maybe ProductID is a string data type in the database? If so, each id should be placed within single quotes.
Since I had no idea of your table structure I used DaColumn and DaTable which is YourColumn and YourTable respectively.
And your implementation of cmdSave is confusing... you execute my code when there's nothing selected in the listview. Eh???
Last edited by leinad31; Oct 11th, 2006 at 02:47 AM.
Re: Is There a batter way to implement Checkboxes?
would I be able to update another table with your way as well? pretty much, decrement the inventory(did this one already) and add 1 to each of the checked items to the customer equipment table
I wanted to update patientEquip table, which contains a CustID, Bed, Wheelchair, etc...the column names. under each bed, wheelchair, there will be a 1 or 0. 1 being assigned out.
I have form that the employee adds the customer, and then clicks add equipment to patient. I have the custID saved for the next form(frmAddEquiptoPAtient), where we were adding update inventory stuff.
so basically I want to update this table too. I forgot about that part sorry.
any ideas?
Last edited by ched35; Oct 11th, 2006 at 11:19 AM.
Re: Is There a batter way to implement Checkboxes?
Hard to create the relevant query without knowing the structure of the tables involved (as well as the data types of their fields)... in addition to that, also include info regarding which control (eg. textbox, combobox) provides the data to which fields.
Also, will this be an UPDATE query, which updates an existing record, or an INSERT query which adds a record to the table Which do you need, pls explain more clearly.
Re: Is There a batter way to implement Checkboxes?
Originally Posted by leinad31
Hard to create the relevant query without knowing the structure of the tables involved (as well as the data types of their fields)... in addition to that, also include info regarding which control (eg. textbox, combobox) provides the data to which fields.
Also, will this be an UPDATE query, which updates an existing record, or an INSERT query which adds a record to the table Which do you need, pls explain more clearly.
I will attach the database so you can see it. Do you wanna see the program itself.
It should add the new custID, and then add the new equipment. Getting the info from a textbox from the previous form. All I really needed is the custID, so I saved it.
attached the DB. Its really basic for now. doesn't follow 2nf, 3nf
Last edited by ched35; Oct 11th, 2006 at 12:48 PM.
Re: Is There a batter way to implement Checkboxes?
Are you sure you want to implement patientequip that way, each equipment as a column? I thought you wanted a method that scales even with additional equipment, hence the use of the listview.
Re: Is There a batter way to implement Checkboxes?
what would be a better way?
I will attach my form so you can see what I mean. The App is ugly for now because I am remaking an old project that wasn't finished.
but basically the employee will click on add patient, and will be taken to the addpatient form. They will hit submit, and if they want to add equipment to the patient at that time. they will click on Add Equipment.
the patient info shows the patient being modified--it wont work cause I hard coded the values in. other wise it will work when you manually enter in stuff.
I am just learning DB design so any insights will be helpful.
Re: Is There a batter way to implement Checkboxes?
I need to go, hopefully someone else can look at your source code... as to the table structure of patientequip, for now I suggest as minimum; 1) autonum pri-key, 2) custID foreign key, 3) ProductID foreign key 3) TransDate for historical inventory movement reports, 4) Units consumed field also for inventory purposes
So if a patient uses 5 equipments, then there will be five records with one record per equipment. Pairing custId-productID allows for many-to-many relationship; eg. relative to a custID you have many productID, and by shifting view relative to productID columns you get a list of patients that used the equipment. This structure also scales to changes in number of equipment (num of records in inventory).
Only downside is that this table will grow in size very fast.
Note I didn't normalize custID and TransDate to make explanation simpler... up to you if you will normalize this table.
Last edited by leinad31; Oct 11th, 2006 at 01:55 PM.
Re: Is There a batter way to implement Checkboxes?
ok, I think I did what you said. I wanted to keep that DB simple so non-technical people could look at it and do what ever. But your way still looks simple.
in msAccess, I didnt know what join type to choose.
I also wanted to keep an amount signed out column, just incase they wanted to signout more than 1.
well, when anyone gets a chance take a gander and let me know if I enterpreted correctly. And still need help with the code part.