Page 1 of 2 12 LastLast
Results 1 to 40 of 50

Thread: [RESOLVED] Is There a batter way to implement Checkboxes?

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Resolved [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:
    1. [B]//code for the actual checkbox[/B]
    2. Private Sub chkbed_Click()
    3.  
    4. bedchk = False
    5.  
    6. 'vbchecked is a reserved word
    7. chkBed.Value = vbChecked
    8. bedchk = True
    9.  
    10. End Sub
    11. [B]
    12. //adds to the patients equipment in the database[/B]
    13. 'customers only get one thing of each equip
    14. add = 1
    15.  
    16. 'Adding equipment to patient
    17. With rspatientequip
    18. .AddNew
    19.  
    20. id = getCustID
    21.  
    22. !CustId = id
    23.  
    24. If addequip = True Then
    25. !bed = "bed"
    26.  
    27. End If
    28. rspatient.Update
    29. End With
    30.  
    31.  
    32.  
    33. [B]//code for updating inventory[/B]
    34.  
    35. Dim id As String
    36. 'customers only get one thing of each equip
    37. reduce = 1
    38.  
    39.  
    40.  
    41. 'taking away from inventory, Updates inventory field UNitIn Stock - reduce(1)
    42. Dim newDBInstockValue As Integer
    43. Dim dbInStock As Integer
    44.  
    45. With rsinventory
    46.     .MoveFirst
    47.     .Find "ProductName = 'Bed'"
    48.     dbInStock = !UnitInStock
    49.    
    50.     newDBInstockValue = (dbInStock - reduce)
    51.     !UnitInStock = newDBInstockValue
    52.    
    53. rsinventory.Update
    54. End With

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Is There a batter way to implement Checkboxes?

    Use a listview with its checkboxes enabled

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Is There a batter way to implement Checkboxes?

    VB Code:
    1. Private Sub chkbed_Click()
    2.  
    3. bedchk = False
    4.  
    5. 'vbchecked is a reserved word
    6. chkBed.Value = vbChecked
    7. bedchk = True
    8.  
    9. End Sub
    Code from the above quote really makes no sense. Here is what you can do instead:

    VB Code:
    1. Private Sub chkbed_Click()
    2.     bedchk = CBool(chkBed.Value)
    3. End Sub

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Is There a batter way to implement Checkboxes?

    There's no need for CBool(), since both types (the one you're assigning with and the one you're assigning to) are booleans...

    edit: Oops... this goes for Option Button. Didn't read well

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    Quote Originally Posted by RhinoBull
    Code from the above quote really makes no sense. Here is what you can do instead:

    VB Code:
    1. Private Sub chkbed_Click()
    2.     bedchk = CBool(chkBed.Value)
    3. End Sub

    what I did works though. I am not good at VB, so I am not sure what I can and shouldn't do.

    whats the diff with your way and what I had?


    everything above works, just need a more effiecint way.

    I have like 20 checkboxes, I dont want to do the code above(my post) 20 times +
    Last edited by ched35; Oct 7th, 2006 at 12:07 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    Quote Originally Posted by leinad31
    Use a listview with its checkboxes enabled

    hmmm, whats the listview? can you explain


    thanks,
    ched

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Is There a batter way to implement Checkboxes?

    Quote 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...

  8. #8
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Is There a batter way to implement Checkboxes?

    Quote 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.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    Quote 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.


    not sure how a listview control helps me.

    If you could explain further(based on my needs).

    thanks for helping me understand,
    ched

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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.

    changes occur after the submit buttons is hit.

    thanks,

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    anyone?

  12. #12
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Is There a batter way to implement Checkboxes?

    Please, don't bump after ONLY 2 hours... give people some time... preferably, don't bump at all.

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Is There a batter way to implement Checkboxes?

    Quote 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.

  14. #14

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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.

    VB Code:
    1. Option Explicit
    2.  
    3. Public helpinghandCon As ADODB.Connection
    4.  
    5. Public Sub openConnection()
    6. Set helpinghandCon = New ADODB.Connection
    7. helpinghandCon.ConnectionString = "provider = Microsoft.Jet.OLEDB.4.0;" & _
    8.                         "Data Source =" & App.Path & "\helpinghand.mdb"
    9. helpinghandCon.Open
    10.  
    11. End Sub
    12.  
    13. Public Sub closeConnection()
    14. helpinghandCon.Close
    15. Set helpinghandCon = Nothing
    16.  
    17. End Sub
    18. Public Sub openRecordset(tableName As String, recordsetName As _
    19.                                     ADODB.Recordset)
    20. Set recordsetName = New ADODB.Recordset
    21. recordsetName.Open tableName, helpinghandCon, adOpenDynamic, _
    22.                             adLockOptimistic, adCmdTable
    23. End Sub
    24.  
    25. Public Sub closeRecord(recordsetName As ADODB.Recordset)
    26. recordsetName.Close
    27. Set recordsetName = Nothing
    28.  
    29. End Sub



    Here is the Form I am using to populate listview I have.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim rspatient As ADODB.Recordset
    4. Dim rspatientequip As ADODB.Recordset
    5. Dim rsinventory As ADODB.Recordset
    6.  
    7. Dim customerID As String
    8. Dim bedchk      As Boolean
    9. Dim reduce As Integer
    10.  
    11. Private Sub cmdGoBack_Click()
    12. Call Load(frmAddPatient)
    13. Call frmAddPatient.Show
    14.  
    15. 'Call Unload(frmAddEquipPatient)
    16. End Sub
    17.  
    18. Private Sub cmdSubmit_Click()
    19.  
    20. rspatient.MoveLast
    21. 'rspatientequip.MoveLast
    22. 'rsinventory.MoveLast
    23. save
    24.  
    25. End Sub
    26.  
    27. [U]Private Sub Form_Load()[/U]
    28. openRecordset "Patient", rspatient
    29. openRecordset "Patientequip", rspatientequip
    30. openRecordset "Inventory", rsinventory
    31.  
    32. 'calling the name of the listview control and table I am using to fill it
    33. Call AddToList(lvwMain, rsinventory)
    34.  
    35. 'getting from module to place in the frame Patient here for testing...
    36.  txtCustID = getCustID
    37.  txtHospice = getHospice
    38.  txtFirst = getFirstName
    39.  txtLast = getLastName
    40.  txtMiddle = getMiddleName
    41. [U]End Sub[/U]
    42.  
    43. Private Sub save()
    44. End Sub 'end function save
    45.  
    46. Private Function AddToList(List As Control, RS As ADODB.Recordset) As Boolean
    47. On Error Resume Next
    48.     'This is a  routine for quickly adding items from a recordset to a listitem.
    49.     Dim mItem     As Variant
    50.     Dim lx        As Long
    51.     Dim strItem   As String
    52.     Dim STRA      As String
    53.     Dim STRB      As String
    54.     Dim Refreshed As Boolean
    55.         With List
    56.             .ListItems.Clear
    57.             .Sorted = False
    58.             If RS.RecordCount > 0 Then RS.MoveFirst
    59.             Do Until RS.EOF
    60.                 STRA = .ColumnHeaders(1)
    61.                 strItem = RS(STRA)
    62.                 'Add the item
    63.                 Set mItem = .ListItems.Add(, , strItem, 1, 7)
    64.                 'Do the subitems
    65.                 For lx = 1 To .ColumnHeaders.Count - 1
    66.                     STRA = .ColumnHeaders(lx + 1)
    67.                     STRB = .ColumnHeaders(lx)
    68.                     If IsNull(RS(STRA)) Then mItem.SubItems(STRB) = "" Else mItem.SubItems(lx) = RS(STRA)
    69.                 Next
    70.                 'Refresh for appearance
    71.                 If List.ListItems.Count > 45 And Not Refreshed Then
    72.                     Refreshed = True
    73.                     DoEvents
    74.                     List.Refresh
    75.                 End If
    76.             RS.MoveNext '
    77.             lx = lx + 1
    78.             Loop
    79.             RS.MoveFirst
    80.             List.ListItems(1).EnsureVisible
    81.             .Refresh
    82.         End With
    83.         AddToList = True
    84. End Function
    85.  
    86.  
    87. Private Sub lvwMain_ItemCheck(ByVal item As MSComctlLib.ListItem)
    88.    ' Set the variable to the SelectedItem.
    89.     Set lvwMain.SelectedItem = item
    90.     If item.Checked Then
    91.         ' user checked an item
    92.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).SmallIcon = 6
    93.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).Bold = True
    94.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ForeColor = vbBlack
    95.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).Checked = True
    96.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(1).Bold = True
    97.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(1).ReportIcon = 58
    98.        
    99.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(2).Bold = True
    100.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(2).ReportIcon = 58
    101.        
    102.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(3).Bold = True
    103.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(3).ReportIcon = 58
    104.        
    105.         ' Code to handle checked items.
    106.     Else
    107.         ' user unchecked an item
    108.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).SmallIcon = 7
    109.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).Bold = False
    110.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ForeColor = vbBlack
    111.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).Checked = False
    112.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(1).Bold = False
    113.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(1).ReportIcon = 0
    114.        
    115.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(2).Bold = False
    116.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(2).ReportIcon = 0
    117.        
    118.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(3).Bold = False
    119.         lvwMain.ListItems.item(lvwMain.SelectedItem.Index).ListSubItems(3).ReportIcon = 0
    120.        
    121.         ' Code to handle unchecked items.
    122.  
    123.     End If
    124. End Sub

  15. #15
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Is There a batter way to implement Checkboxes?

    It's better to declare mItem as ListItem, instead of Variant. Never declare as variant. Also (declare List as ListView, instead of Control):
    VB Code:
    1. Private Function AddToList(List As [B]ListView[/B], RS As ADODB.Recordset) As Boolean
    What's the .View in your ListView? Have you add any Column Headers?

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    what do you mean .View?

    add to list function is supposed to add the columns and then fill the data from the DB.

    I have the sample program, if you wanna see it. They used a module class and I used a module.

    I didn't add command stuff they had cause when I did it didn't work, got errors

    this is what they had, its from the module
    Set cmdEmployees = New ADODB.Command

    cmdEmployees.ActiveConnection = cnnConnection
    cmdEmployees.CommandType = adCmdTable
    cmdEmployees.CommandText = "Employees"
    rstEmployees.CursorType = adOpenKeyset
    rstEmployees.LockType = adLockOptimistic
    Set rstEmployees = cmdEmployees.Execute

    EmployeesRecordCount = rstEmployees.RecordCount

  17. #17

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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?

  18. #18

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Question 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?

    thanks
    Attached Files Attached Files

  19. #19
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Is There a batter way to implement Checkboxes?

    Check under...
    VB Code:
    1. 'Do the subitems
    ... routine in AddToList() function.

  20. #20

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    but where is it getting the names for the columns?

    there is no name EmployeeID FirstName etc.....

    I had to change my DB to those name inorder for it to work

    my original column names where ProductId ProductName etc.... and it wouldn't read in the stuff.

  21. #21

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    ok, I figured out where the column name are coming from.

    I went to list view properties --> column headers tag
    and changed the appropriate names to my needs.

  22. #22

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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.

    is there another way?

    thnks.

  23. #23
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Is There a batter way to implement Checkboxes?

    Does this help:
    VB Code:
    1. Option Explicit
    2.  
    3. Dim i As Integer, j As Integer
    4. Dim tmp As String
    5. Dim lvwItem As ListItem
    6.  
    7. Private Sub Command1_Click()
    8.  
    9. For i = 1 To ListView1.ListItems.Count
    10.     Set lvwItem = ListView1.ListItems.Item(i)
    11.         If lvwItem.Checked = True Then
    12.             If ListView1.ColumnHeaders.Count > 0 Then
    13.                 tmp = vbNullString: tmp = lvwItem.Text
    14.                     If ListView1.ColumnHeaders.Count > 1 Then
    15.                         For j = 2 To ListView1.ColumnHeaders.Count
    16.                             tmp = tmp & ";" & lvwItem.SubItems(j - 1) 'divide columns with ";" sign
    17.                         Next j
    18.                     End If
    19.             End If
    20.                 'now use tmp however you want
    21.                 Debug.Print tmp
    22.         End If
    23. Next i
    24.  
    25. End Sub

  24. #24

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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.

    is there a diff way? or is what you wrote good.

  25. #25

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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:
    1. 'go through each item
    2. For i = 1 To ListView1.ListItems.Count
    3.  
    4.     'set the first value to lvwitem, then do again with 2,3,4...
    5.     Set lvwItem = ListView1.ListItems.item(i)
    6.    
    7.         'if the item is checked
    8.         If lvwItem.Checked = True Then
    9.        
    10.         'gets the values from under the Column UnitInStock
    11.             tmp = ListView1.ListItems.item(i).ListSubItems(2)
    12.            
    13.             With rsinventory
    14.             .MoveFirst            'i tried to use .index(i) that didnt work...whats the proper way
    15.        
    16.                 unit = !UnitInStock
    17.                
    18.                 !UnitInStock = unit - 100
    19.  
    20.            End With
    21.            
    22.                        End If
    23.                 'now use tmp however you want
    24.                 Debug.Print tmp
    25.         'End If
    26. Next i

  26. #26
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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)

  27. #27
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Is There a batter way to implement Checkboxes?

    VB Code:
    1. Const SQLInc = "UPDATE DaTable SET DaColumn = DaColumn + 1 "
    2. Const SQLDec = "UPDATE DaTable SET DaColumn = DaColumn - 1 "
    3.  
    4. Dim lstYourItem As ListItem
    5. Dim strTemp As String
    6.  
    7. strTemp = ""
    8. For Each lstYourItem In ListView1.ListItems
    9.    If lstYourItem.Checked Then strTemp = strTemp & "," & lstYourItem.Tag
    10. Next
    11. strTemp = SQLInc & "WHERE DaPrimaryKey IN (" & Mid(strTemp, 2) & ") "  'Mid removes leading comma
    12.  
    13. connADO.Execute strTemp
    Keep it simple
    Last edited by leinad31; Oct 10th, 2006 at 07:34 AM.

  28. #28

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    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.

  29. #29

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    here is what I have and it works when I put a number by the ????? below. So If I put 1, it will work when the ID columns have a 1.

    How do I get the tags ID that I saved so I can update stuff thats clicked.

    strTemp = SQLInc & "WHERE ????? IN (" & Mid(strTemp, 2) & ") " 'Mid removes leading comma

    all code below.

    VB Code:
    1. Private Sub getColumnHeaders()
    2.  
    3. 'build the listviews column headers
    4. Dim Header As ColumnHeader
    5.  
    6. 'build header Name
    7. Set Header = ListView1.ColumnHeaders.Add()
    8.     Header.Text = "ProductID"
    9.     Header.Width = ListView1.Width * 0.28
    10.     ' width of the header is 29% of the total width of listview1.
    11.     ' I choose 29%, not 30%, because with 29% the horizontal scrollbar will disappear.
    12.     ' So in total I use 99% and not 100%
    13.  
    14. 'build header Address
    15. Set Header = ListView1.ColumnHeaders.Add()
    16.     Header.Text = "ProductName"
    17.     Header.Width = ListView1.Width * 0.4
    18.     ' width of the header is 40% of the total width of listview1
    19.    
    20. 'build header Telephone
    21. Set Header = ListView1.ColumnHeaders.Add()
    22.     Header.Text = "UnitInStock"
    23.     Header.Width = ListView1.Width * 0.3
    24.     ' width of the header is 30% of the total width of listview1
    25.    
    26. 'we have to reset ItemIndex, so there is nothing selected in listview1
    27. ItemIndex = 0
    28.  
    29.  
    30. End Sub
    31.  
    32. 'gets the records from the database
    33. 'set column header names in the listview properties to Match columns in DB
    34. 'To have numbers sorted normally, go to listviw property sorting, check sort, sortkey = 1
    35.  
    36. Private Sub addToListView()
    37. 'End Sub 'end function save
    38.  
    39. ' We use the integer 'a', so we know in which row we are writing (listview)
    40.     Dim a As Integer
    41.     a = 1
    42.  
    43.  
    44. Set rsinventory = helpinghandCon.Execute("SELECT * FROM inventory ORDER BY ProductID", , adCmdText)
    45.    
    46.     ' Load the data.
    47.     Do While Not rsinventory.EOF
    48.      '   Set list_item = ListView1.ListItems.Add(, , rsinventory!ProductID) 'column 0
    49.       '  list_item.SubItems(a) = rsinventory!ProductName 'column 1
    50.        ' list_item.SubItems(a) = rsinventory!UnitInStock 'column 2
    51.  
    52.  
    53.  ListView1.ListItems.Add , , rsinventory!ProductID
    54.         ListView1.ListItems(a).ListSubItems.Add , , rsinventory!ProductName
    55.         ListView1.ListItems(a).ListSubItems.Add , , rsinventory!UnitInStock
    56.        
    57.  
    58.  
    59.  
    60.  
    61. ' We must know which row contains the data from which row from the table People.
    62.         ' Therefore we copy the ID from the table to the Tag of the row which contains
    63.         ' the data from that row in the table. Sorry if this sounds complicated,
    64.         ' but I'm not that good in teaching LOL :-)
    65.         ListView1.ListItems(a).Tag = rsinventory!ProductID
    66.  
    67.  
    68.  ' Increase 'a'
    69.         a = a + 1
    70.  
    71.  
    72.         ' Move to the next row in the recordset
    73.         rsinventory.MoveNext
    74.        
    75.     ' Do the same stuff again, until we reached the End Of File
    76.     Loop
    77.  
    78.     ' Close the recordset and connection.
    79.     'rsinventory.Close
    80.     'helpinghandCon.Close
    81. End Sub
    82.  
    83. 'when the submit button is clicked, all checked items are saved
    84. Private Sub save()
    85.  
    86. Dim a As Integer
    87. Dim id As Integer
    88.  
    89. a = 1
    90.  
    91. If ItemIndex <> 0 Then
    92.  
    93.  rsinventory.Index = "ProductID"
    94.  
    95. rsinventory.MoveFirst
    96.  
    97. rsinventory.Seek "=", ListView1.ListItems.item(ItemIndex).Tag
    98. 'we reset itemindex to 0, this means that nothing is selected in listview1
    99.             ItemIndex = 0
    100.            
    101.            
    102. Else
    103.  
    104. Const SQLInc = "UPDATE inventory SET UnitInStock = UnitInStock + 100 "
    105. Const SQLDec = "UPDATE DaTable SET DaColumn = DaColumn - 1 "
    106.  
    107. Dim lstYourItem As ListItem
    108. Dim strTemp As String
    109.  
    110. strTemp = ""
    111. For Each lstYourItem In ListView1.ListItems
    112.    If lstYourItem.Checked Then strTemp = strTemp & "," & lstYourItem.Tag
    113. id = ListView1.ListItems(a).Tag
    114. Next
    115. strTemp = SQLInc & "WHERE [B]?????[/B] IN (" & Mid(strTemp, 2) & ") "  'Mid removes leading comma
    116.  
    117. helpinghandCon.Execute strTemp
    118. End If
    119.  
    120.  
    121. End Sub

  30. #30
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Is There a batter way to implement Checkboxes?

    Quote 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.

  31. #31

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    yeah, I was confused about my cmdsave too--LOL. I was trying to use code from a sample program.

    You method now works perfectly. I had the ProductID set to text. changing to number fixed the problem.

    Your explanation really helped me understand.

    Thanks everyone for taking the time to help me.

  32. #32

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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.

  33. #33
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  34. #34

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    Quote 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
    Attached Files Attached Files
    Last edited by ched35; Oct 11th, 2006 at 12:48 PM.

  35. #35
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  36. #36

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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.
    Attached Files Attached Files

  37. #37
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  38. #38

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    Re: Is There a batter way to implement Checkboxes?

    ok, thanks!! No hurry, I have a month or so to finish up the basics.

  39. #39

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    59

    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.


    thanks
    Attached Files Attached Files

  40. #40
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Is There a batter way to implement Checkboxes?

    In the listitems iteration, you will execute an INSERT query that will add a record into patientequip table for each checked listitem.

    sSQL = "INSERT INTO patientequip (CustID, ProductID, AmountSignedOut) VALUES ("
    sSQL = sSQL & txtCustID.Text & "," & lstYourItem.Tag & ", " lstYourItem.SubItem(YourIndex) & ") "

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width