I create a project add some item (C_Number,E Number, Model, Type, Color, Key No, Price(numeric field)) in MSFlexGrid1. then add a button when click on this add the record in data1, but only 1(one) data add in the data1 database. I use access database. How can I add all (minimum 2 or maximum 12 record) add in data1 database. Please help me.......

Private Sub Command11_Click()
Dim count As Integer
Data2.Recordset.AddNew
MSFlexGrid1.Col = 0
Data2.Recordset.Fields("C_Number") = MSFlexGrid1.Text
MSFlexGrid1.Col = 1
Data2.Recordset.Fields("E_Number") = MSFlexGrid1.Text
Data2.Recordset.Update
Data2.Refresh
End Sub

Dani AI

Generated

Brief diagnosis: your current handler only performs a single AddNew and Update, so only one row reaches the table. was pointing toward a batch-style commit (ADO's UpdateBatch), but the simplest fix is to loop the grid and add each row. Below are two practical options—simple looping (easy and reliable) and a client-side batch (fewer round-trips).

Simple, reliable approach (loop and add each row)

Private Sub Command11_Click()
    Dim r As Long
    With MSFlexGrid1
        For r = .FixedRows To .Rows - 1
            If Trim(.TextMatrix(r, 0)) <> "" Then        'skip empty rows
                Data1.Recordset.AddNew
                Data1.Recordset.Fields("C_Number").Value = .TextMatrix(r, 0)
                Data1.Recordset.Fields("E_Number").Value = .TextMatrix(r, 1)
                ' map other columns here...
                Data1.Recordset.Update
            End If
        Next r
    End With
End Sub

Batch approach (use when you want fewer commits)

  • Create an ADODB.Recordset with CursorLocation = adUseClient and LockType = adLockBatchOptimistic.
  • Open "SELECT * FROM MyTable WHERE 1=0", conn to get the schema, AddNew rows in the loop, then rs.UpdateBatch once at the end. This sends all inserts in one go and is more efficient for many rows.

Troubleshooting & cautions

  • Start looping at FixedRows (grid headers). Use TextMatrix(row,col) rather than changing .Col.
  • Convert numeric fields (e.g. Price) using Val() to avoid type errors.
  • Watch primary-key/unique constraints and auto-increment fields—duplicates will abort inserts.
  • If edits to the grid cell are custom (edit control over the grid), ensure the cell edit is committed before reading.
  • If you still see one row, step through the loop with a breakpoint to confirm For runs multiple times and cell text is populated.

This ties back to ’ batch hint—use UpdateBatch only after preparing a client-side recordset; otherwise the per-row AddNew+Update shown above is simplest and will insert all rows.

Recommended Answers

All 2 Replies

You can use BatchUpdate method.

Kindly explain the method I don't know how to use this method. Please help me sir please help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.