Results 1 to 7 of 7

Thread: [RESOLVED] multiple data in Listview save to Database

  1. #1
    Member
    Join Date
    Aug 12
    Posts
    42

    Resolved [RESOLVED] multiple data in Listview save to Database

    I'm trying to insert/save data in the database from Listview:

    The code I use:
    Code:
    For i = 0 To ListView3.ListItems.Count - 1
            With ListView3
            cn.Execute "insert into tempclientPurchases (baseID,invoiceno,pcode,ID,dateout,productdesc,qty,unit,discount,price,amt,salereport,type) values (" & .ListItems(i).Text & ",'" _
            & .ListItems(i).SubItems(1) & "'," & .ListItems(i).SubItems(2) & "," & .ListItems(i).SubItems(3) & ",#" _
            & .ListItems(i).SubItems(4) & "#,'" & .ListItems(i).SubItems(5) & "'," & .ListItems(i).SubItems(6) & ",'" _
            & .ListItems(i).SubItems(7) & "'," & .ListItems(i).SubItems(8) & "," & .ListItems(i).SubItems(9) & "," _
            & .ListItems(i).SubItems(10) & "," & .ListItems(i).SubItems(11) & "," & .ListItems(i).SubItems(12) & ")"
            End With
        Next i
    I get Out of bounds error

  2. #2
    Member
    Join Date
    Aug 12
    Posts
    42

    Re: multiple data in Listview save to Database

    I modified the code: I think this is the proper code:
    Code:
    For i = 1 To ListView3.ListItems.Count - 1
            With ListView3
            cn.Execute "insert into tempclientPurchases (baseID,invoiceno,pcode,ID,dateout,productdesc,qty,unit,discount,price,amt,salereport,type) values (" & .ListItems.Item(i).SubItems(13) & ",'" _
            & .ListItems.Item(i).SubItems(1) & "'," & .ListItems.Item(i).SubItems(2) & "," & .ListItems.Item(i).SubItems(3) & ",#" _
            & .ListItems.Item(i).SubItems(4) & "#,'" & .ListItems.Item(i).SubItems(5) & "'," & .ListItems.Item(i).SubItems(6) & ",'" _
            & .ListItems.Item(i).SubItems(7) & "'," & .ListItems.Item(i).SubItems(8) & "," & .ListItems.Item(i).SubItems(9) & "," _
            & .ListItems.Item(i).SubItems(10) & "," & .ListItems.Item(i).SubItems(11) & "," & .ListItems.Item(i).SubItems(12) & ")"
            End With
        Next i
    The program runs ok, no error... but in the database no data was save
    Last edited by micdansan; Sep 9th, 2012 at 02:18 AM.

  3. #3
    Web developer Nightwalker83's Avatar
    Join Date
    Dec 01
    Location
    Adelaide, Australia
    Posts
    9,722

    Re: multiple data in Listview save to Database

    You want the recordset to be saving the data not the connection. Try something like:

    vb Code:
    1. rs.Fields("baseID") = .ListItems.Item(i).SubItems(13) 'Do this for each item to be saved
    2. rs.Update

    Edit:

    Don't forget to add error handling! Something such as:

    vb Code:
    1. On Error GoTo HandleSaveErrors 'if an error occurs, let "HandleSaveErrors"
    2. 'Put the above code here
    3.  
    4. HandleSaveErrors:
    5.    
    6.     Select Case Err.Number
    7.          Case 3022 'duplicate primary key
    8.          strMessage = "Duplicate Customer Field"
    9.          MsgBox strMessage, vbExclamation, "Database Error"
    10.          rs.cancelUpdate
    11.        
    12.         Case 3058       'Primary key left blank
    13.             strMessage = "Field was left empty"
    14.             MsgBox strMessage, vbExclamation, "Database Error"
    15.             rs.cancelUpdate
    16.         Case Else
    17.             strMessage = "Record could not be saved. " & vbCrLf _
    18.                             & Err.Description
    19.             Resume Next
    20.     End Select
    Last edited by Nightwalker83; Sep 9th, 2012 at 02:32 AM. Reason: Adding more!
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    DON'T BUMP YOUR POSTS!!! Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  4. #4
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,134

    Re: multiple data in Listview save to Database

    When building up an SQL query I normally put it into a String variable in some sort of structured fashion, so (1) I can print the contents to the debug.window to check for any errors and (2) make sure the SQL is complete and properly formed.
    Code:
    Dim strSQL As String
    For i = 1 To ListView3.ListItems.Count - 1
        strSQL = "INSERT  INTO tempclientPurchases " _
            & "(baseID,invoiceno,pcode,ID,dateout,productdesc,qty,unit,discount,price,amt,salereport,type) " _
            & "VALUES("
        With ListView3.ListItems.Item(i)
            strSQL = strSQL & .SubItems(13) & ","           'baseID
            strSQL = strSQL & "'" & .SubItems(1) & "',"     'invoiceno
            strSQL = strSQL & .SubItems(2) & ","            'pcode
            strSQL = strSQL & .SubItems(3) & ","            'ID
            strSQL = strSQL & "#" & .SubItems(4) & "#,"     'dateout
            strSQL = strSQL & "'" & SubItems(5) & "',"      'productdesc
            strSQL = strSQL & .SubItems(6) & ","            'qty
            strSQL = strSQL & "'" & .SubItems(7) & "',"     'unit
            strSQL = strSQL & .SubItems(8) & ","            'discount
            strSQL = strSQL & .SubItems(9) & ","            'price
            strSQL = strSQL & .SubItems(10) & ","           'amt
            strSQL = strSQL & .SubItems(11) & ","           'salereport
            strSQL = strSQL & .SubItems(12)                 'type
            strSQL = strSQL & ")"
            cn.Execute strSQL
        End With
    Next i
    It's a bit of extra typing, but worth it in the end.

    @Nightwalker: The Execute method of the Connection Object is the correct way to perform action queries, also there's no sign of a RecordSet being populated in OPs code
    Last edited by Doogle; Sep 9th, 2012 at 03:02 AM. Reason: Put SubItem(11) in twice

  5. #5
    Member
    Join Date
    Aug 12
    Posts
    42

    Re: multiple data in Listview save to Database

    Quote Originally Posted by Doogle View Post
    When building up an SQL query I normally put it into a String variable in some sort of structured fashion, so (1) I can print the contents to the debug.window to check for any errors and (2) make sure the SQL is complete and properly formed.
    Code:
    Dim strSQL As String
    For i = 1 To ListView3.ListItems.Count - 1
        strSQL = "INSERT  INTO tempclientPurchases " _
            & "(baseID,invoiceno,pcode,ID,dateout,productdesc,qty,unit,discount,price,amt,salereport,type) " _
            & "VALUES("
        With ListView3.ListItems.Item(i)
            strSQL = strSQL & .SubItems(13) & ","           'baseID
            strSQL = strSQL & "'" & .SubItems(1) & "',"     'invoiceno
            strSQL = strSQL & .SubItems(2) & ","            'pcode
            strSQL = strSQL & .SubItems(3) & ","            'ID
            strSQL = strSQL & "#" & .SubItems(4) & "#,"     'dateout
            strSQL = strSQL & "'" & SubItems(5) & "',"      'productdesc
            strSQL = strSQL & .SubItems(6) & ","            'qty
            strSQL = strSQL & "'" & .SubItems(7) & "',"     'unit
            strSQL = strSQL & .SubItems(8) & ","            'discount
            strSQL = strSQL & .SubItems(9) & ","            'price
            strSQL = strSQL & .SubItems(10) & ","           'amt
            strSQL = strSQL & .SubItems(11) & ","           'salereport
            strSQL = strSQL & .SubItems(12)                 'type
            strSQL = strSQL & ")"
            cn.Execute strSQL
        End With
    Next i
    It's a bit of extra typing, but worth it in the end.

    @Nightwalker: The Execute method of the Connection Object is the correct way to perform action queries, also there's no sign of a RecordSet being populated in OPs code
    @Doogle
    Thanks for your reply... Tried your code, still the same runs ok, no errors, but no data was save in the database.

  6. #6
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,134

    Re: multiple data in Listview save to Database

    How are you checking the contents of the table after performing the Inserts?

  7. #7
    Member
    Join Date
    Aug 12
    Posts
    42

    Re: multiple data in Listview save to Database

    Quote Originally Posted by Doogle View Post
    How are you checking the contents of the table after performing the Inserts?
    @Doogle
    I run the code again and then returns this error error: no value given for one or more required parameters

    I added trim() and val() and it works! Great Help Doogle.. Thanks a lot.

    here's the final code:
    Code:
    For i = 1 To ListView3.ListItems.Count
            stringSQLT = "INSERT  INTO TempclientPurchases " _
                & "(baseID,invoiceno,pcode,ID,dateout,productdesc,qty,unit,discount,price,amt,salereport,type) " _
                & "VALUES("
            With ListView3.ListItems.Item(i)
                stringSQLT = stringSQLT & Val(.SubItems(13)) & ","          'baseid
                stringSQLT = stringSQLT & "'" & Trim(.SubItems(1)) & "',"   'invoiceno
                stringSQLT = stringSQLT & Val(.SubItems(2)) & ","            'pcode
                stringSQLT = stringSQLT & Val(.SubItems(3)) & ","            'ID
                stringSQLT = stringSQLT & "#" & .SubItems(4) & "#,"        'dateout
                stringSQLT = stringSQLT & "'" & Trim(.SubItems(5)) & "',"   'productdesc
                stringSQLT = stringSQLT & Val(.SubItems(6)) & ","            'qty
                stringSQLT = stringSQLT & "'" & Trim(.SubItems(7)) & "',"   'unit
                stringSQLT = stringSQLT & Val(.SubItems(8)) & ","            'discount
                stringSQLT = stringSQLT & Val(.SubItems(9)) & ","            'price
                stringSQLT = stringSQLT & Val(.SubItems(10)) & ","          'amt
                stringSQLT = stringSQLT & Val(.SubItems(11)) & ","          'salereport
                stringSQLT = stringSQLT & Val(.SubItems(12))                 'type
                stringSQLT = stringSQLT & ")"
                cn.Execute stringSQLT
            End With
        Next i

Posting Permissions

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