• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Copy selected ListView row into a Table only once

inddon

Member
Hello There,

I have a listview with filled rows. I have 2 buttons 'Create' and 'Update'.

The VBA code should do the following:
1. When a ListView row is selected and the User clicks the 'Update' button then it should copy the selected row to the lastrow in a worksheet ("Sheet1") Table ("Table1")

Copy the ListView data fieldwise, as there are 15 fields in listview and 5 columns in the Table.

If the User double click the row again, it should check if the row exists in the Table. If Yes then give message and do not copy it to the Table.

2.When the user clicks the 'Create' button, it should add a new row in the table ("Table1") in worksheet ("Sheet1")



Below detail:
UserForm = FrmPersons
ListView = ListViewMaster
Worksheet = "Sheet1"
Table Name = "Table1"

Below short example code for your reference.

Could any one advice how this can be achieved using VBA?

Thanks for your help

Regards
Don


Code:
Dim LstItem As ListItem
Dim RecordSet As ADODB.RecordSet

Sub LVPersons_AddColumnHeaders()

   
  'Clear Column Headers
  FrmPersons.ListViewMaster.ColumnHeaders.Clear
  FrmPersons.ListViewMaster.ListItems.Clear
   
  'Add the column headers
  FrmPersons.ListViewMaster.ColumnHeaders.Add Text:="Person ID", Width:=55
  FrmPersons.ListViewMaster.ColumnHeaders.Add Text:="Person Type", Width:=67
  FrmPersons.ListViewMaster.ColumnHeaders.Add Text:="Title", Width:=40
  FrmPersons.ListViewMaster.ColumnHeaders.Add Text:="First Name", Width:=100
  FrmPersons.ListViewMaster.ColumnHeaders.Add Text:="Last Name", Width:=100
  FrmPersons.ListViewMaster.ColumnHeaders.Add Text:="Gender", Width:=50
End Sub

Sub LVPersons_FillListView(p_search As Boolean)
  'Dim LstItem As ListItem
  If Not p_search Then RecordSet.MoveFirst
   
  While Not (RecordSet.BOF And RecordSet.EOF)
  If RecordSet.EOF Then GoTo DisConnect
  Set LstItem = FrmPersons.ListViewMaster.ListItems.Add(Text:=RecordSet.Fields(0).Value)
  LstItem.ListSubItems.Add Text:=RecordSet.Fields(1).Value
  LstItem.ListSubItems.Add Text:=RecordSet.Fields(2).Value
  LstItem.ListSubItems.Add Text:=RecordSet.Fields(3).Value
  LstItem.ListSubItems.Add Text:=RecordSet.Fields(4).Value
  LstItem.ListSubItems.Add Text:=RecordSet.Fields(5).Value
   
  RecordSet.MoveNext
  Wend
End Sub
 
Back
Top