|Boolean||A boolean expression that indicates whether the control detects when a new record is added to the bounded recordset|
By default, the DetectAddNew property is False. The DetectAddNew property detects adding new records to a recordset ( /COM ). Use the DataSource property to bound the control to a table. If the DetectAddNew property is True, and user adds a new record to the bounded recordset, the control automatically adds a new item to the control. The DetectAddNew property has effect only if the control is bounded to an ADO, ADODB recordset, using the DataSource property.
Handling the AddNew method in the control, using the DAO recordset on MS Access
Private Sub Form_Load() With G2antt1 .BeginUpdate .DataSource = CurrentDb.OpenRecordset("Employees") .DetectAddNew = True .EndUpdate End With End Sub
The code binds the control to a DAO recordset. Please notice, that the DetectAddNew property is set after calling the DataSource method. Setting the DetectAddNew property on True, makes the control associate new items with new records added during the AddItem event as shown bellow.
Private Sub cmdAddNew_Click() With G2antt1.Items .EnsureVisibleItem .AddItem End With End Sub
The code adds a new item to the control and ensures that the new item fits the control's client area. The Items.AddItem call makes the control to fire the AddItem event, which will actually add the new record to the database, as in the following code
Private Sub G2antt1_AddItem(ByVal Item As Long) With G2antt1 If .DetectAddNew Then With .DataSource .AddNew !Lastname = "new" !FirstName = "new" .Update End With End If End With End Sub
The code adds a new record to the bounded recordset. Here you need to insert or update the required fields so the new record is added to the DAO recordset. Once the event is finished, the new item is associated with the new record in the database, so from now on, any change to the item will be reflected in the recordset.
Handling the AddNew method in the control, using the ADO recordset in VB
Private Sub Form_Load() With G2antt1 Set rs = CreateObject("ADOR.Recordset") With rs .Open "Employees", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\sample.accdb", 3, 3 End With .DataSource = rs .DetectAddNew = True End With End Sub
The code binds the control to an ADO recordset.
Private Sub cmdAddNew_Click() With G2antt1.DataSource .AddNew Array("FirstName", "LastName"), Array("new", "new") .Update End With End Sub
The code adds a new record to the attached recordset, and the control will add a new associated item, because the DetectAddNew method is True.
The AddItem event occurs if the AddNew ( method of the ADO.RecordSet object ) is performed, if the control's DetectAddNew property is True. If using the CellValue properties during the AddItem event, you must be sure that they are available, or they have the proper values or expected values. For instance, let's say that we defined the AddItem event such as:
Private Sub G2antt1_AddItem(ByVal Item As EXG2ANTTLibCtl.HITEM) With G2antt1.Items .AddBar Item, "Task", .CellValue(Item, 1), .CellValue(Item, 2) End With End Sub
If using the r.AddNew method we MUST use the values to be added as parameters of the AddNew method as in the following sample:
r.AddNew Array(0, 1, 2), Array("Task", #1/3/2001#, #1/4/2001#)
instead using the following code:
r(0) = "Task" r(1) = #1/1/2001# r(2) = #1/2/2001#r.Update
which is wrong as the AddItem event is called when the r.AddNew method is performed, and so during the AddItem event, the values for the cells are NOT yet available, as the r(0), r(1), r(2) are filled later then r.AddNew call.