i got a little problem here, i want to update my record by opening it on another form.
but it only update the first record on the list,
can anybody help me with this.
heres my code:

'first form
Private Sub command1_Click()
    If Text1 = ListView1.SelectedItem.Text Then
            With form2
                .Label1.Caption = ID_no
                .lbfname.Caption = ListView1.SelectedItem.SubItems(1)
                .lbmname.Caption = ListView1.SelectedItem.SubItems(2)
                .lblname.Caption = ListView1.SelectedItem.SubItems(3)
                .Show vbModal
                End With
                Exit Sub

'second form
    Private Sub Command1_Click()
    With Adodc1.Recordset
        sql = "select * from first_sem_info where fname = '" & lbfname.Caption & "' and lname = '" & lblname.Caption & "'"
        .Fields("grade") = cbgrade.Text
        .update
        MsgBox "success"
        End With
    End Sub

You've set a condition that will restrict you getting only one item from your listview at a time,If Text1 = ListView1.SelectedItem.Text Then.

why only the first item of the list be updated? even if i did not chose that particular item.

Your code might have more success if you use,ListView1.SelectedItems(0).Text.

One of the problems with your code is that the button can be pressed even if no item is selected in the listview. One way to correct this is to use the Listview1_SelectedIndexChanged event. Something like this,

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        If ListView1.SelectedItems.Count > 0 Then
            Dim Result = MessageBox.Show("Update Record" + " " + ListView1.SelectedItems(0).Text + "?", "Confirm Update", MessageBoxButtons.YesNo)
            If Result = Windows.Forms.DialogResult.Yes Then
                 If Text1 = ListView1.SelectedItems(0).Text Then
                    With form2
                        .Label1.Caption = ID_no
                        .lbfname.Caption = ListView1.SelectedItems(0).SubItems(1)
                        .lbmname.Caption = ListView1.SelectedItems(0).SubItems(2)
                        .lblname.Caption = ListView1.SelectedItems(0).SubItems(3)
                        .Show vbModal
                    End With
                 End If
            End If
        End If
    End Sub

When the user clicks on an item a messagebox asks to confirm then it adds the subitems to form2.

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.