Adding Records and Editing Data in a DBGrid CellLast reviewed: June 6, 1996Article ID: Q149092 |
The information in this article applies to:
SUMMARYThis article describes how to programmatically edit the data and add new records to a DBGrid control. You can change the contents of a cell and those changes will appear on the screen, but they are not written to the data source bound to the DBGrid control. To write these changes to the data source, you must instead edit the underlying recordset.
MORE INFORMATIONThe following example creates a simple database containing one table with four fields and five records. Each record is a number in the field. The database is going to be the data source. When you click on a cell, the data in that cell is shown in a TextBox. When you change the contents of that cell, the changes are shown on the DBGrid control. However, when you execute the UpdateControls method through a command button, the changes disappear from the cell, indicating the data source was not updated. You must click the Update Recordset button when you make your changes in the TextBox to ensure that the changes are written to the data source. The Update Recordset button updates the underlying recordset of the Data control. Since the DBGrid control is bound to the Data control, the changes appear in the DBGrid control.
Creating the Sample ApplicationThe following sample application demonstrates how to edit the contents of a DBGrid cell:
'of the text box to the database. Since the contents of therecordset 'is being modified, the contents are saved to the database after 'executing the Update method. Data1.Recordset.Edit Data1.Recordset.Fields(igColumn) = Text1.Text Data1.Recordset.Update End Sub Private Sub Command3_Click() 'The Update DBGrid button. Clicking this button executes the 'UpdateControls method on the Data control to demonstrate that 'changing the cell in a bound DBGrid control does not save 'the new information to the database. To save these changes, 'the underlying recordset from the Data control must be modified. Data1.UpdateControls End Sub Private Sub Command4_Click() 'The Add New Record button. Clicking this button adds new records 'to the recordset. Use the following code to add a new record to 'the DBGrid control. 'Set DBGrid and Data Control Properties to 'allow new records to be added DBGrid1.AllowAddNew = True Data1.EOFAction = vbAddNew Data1.Recordset.MoveLast Data1.Recordset.MoveNext DBGrid1.Row = DBGrid1.VisibleRows - 1 Data1.Recordset.AddNew For iFields = 1 To 4 'For each field in the record 'Add the contents of the text box Data1.Recordset("Field " & CStr(iFields)) = Text1.Text Next iFields Data1.Recordset.Update End Sub Private Sub DBGrid1_Change() Command3.Visible = True End Sub Private Sub DBGrid1_MouseUp(Button As Integer, Shift As Integer, _ X As Single, Y As Single) Command2.Visible = True igColumn = DBGrid1.ColContaining(X) igRow = DBGrid1.RowContaining(Y) vargBookmark = DBGrid1.RowBookmark(igRow) Text1.Text = DBGrid1.Columns(igColumn).CellValue(vargBookmark) End Sub Private Sub Form_Load() Command1.Visible = False Command2.Visible = False Command3.Visible = False Command4.Visible = False Command1.Caption = "Create Database" Command2.Caption = "Update Database" Command3.Caption = "Update DBGrid" Command4.Caption = "Add New Record" 'If the database does not exist, show the 'Create Database button If Dir("C:\test.mdb") = "" Then Command1.Visible = True Else 'Open existing database Set db = OpenDatabase("C:\test.mdb") Set rs1 = db.OpenRecordset("Select * from Table1") Set Data1.Recordset = rs1 Command4.Visible = True End If End Sub Private Sub Text1_Change() Command2.Visible = True End Sub Running the Sample Application
|
Additional reference words: 4.00 vb4win vb4all
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |