Lire et écrire dans un champ d’un jeu d’enregistrements DAO

When you read or write data to a field, you are actually reading or setting the DAO Value property of a Field object. The DAO Value property is the default property of a Field object. Par conséquent, vous pouvez définir la propriété DAO Value du champ LastName dans le recordset rstEmployees de l’une des manières suivantes.

rstEmployees!LastName.Value = strName 
rstEmployees!LastName = strName 
rstEmployees![LastName] = strName 

The tables underlying a Recordset object may not permit you to modify data, even though the Recordset is of type dynaset or table, which are usually updatable. Check the Updatable property of the Recordset to determine whether its data can be changed. Si la propriété a la valeur True, l’objet Recordset peut être mis à jour.

Les champs individuels d’un objet Recordset pouvant être mis à jour peuvent ne pas être mis à jour, et la tentative d’écriture dans ces champs génère une erreur d’exécution. To determine whether a given field is updatable, check the DataUpdatable property of the corresponding Field object in the Fields collection of the Recordset. The following example returns True if all fields in the dynaset created by strQuery are updatable and returns False otherwise.

Function RecordsetUpdatable(strSQL As String) As Boolean 
 
Dim dbsNorthwind As DAO.Database 
Dim rstDynaset As DAO.Recordset 
Dim intPosition As Integer 
 
On Error GoTo ErrorHandler 
 
   ' Initialize the function's return value to True. 
   RecordsetUpdatable = True 
 
   Set dbsNorthwind = CurrentDb 
   Set rstDynaset = dbsNorthwind.OpenRecordset(strSQL, dbOpenDynaset) 
 
   ' If the entire dynaset isn't updatable, return False. 
   If rstDynaset.Updatable = False Then 
      RecordsetUpdatable = False 
   Else 
      ' If the dynaset is updatable, check if all fields in the 
      ' dynaset are updatable. If one of the fields isn't updatable, 
      ' return False. 
      For intPosition = 0 To rstDynaset.Fields.Count - 1 
         If rstDynaset.Fields(intPosition).DataUpdatable = False Then 
            RecordsetUpdatable = False 
            Exit For 
         End If 
      Next intPosition 
   End If 
 
   rstDynaset.Close 
   dbsNorthwind.Close 
 
   Set rstDynaset = Nothing 
   Set dbsNorthwind = Nothing 
 
Exit Sub 
 
ErrorHandler: 
   MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description 
End Function

Any single field can impose a number of criteria on data in that field when records are added or updated. These criteria are defined by a handful of properties. The DAO AllowZeroLength property on a Text or Memo field indicates whether or not the field will accept a zero-length string (""). The DAO Required property indicates whether or not some value must be entered in the field, or if it instead can accept a Null value. En ce qui concerne l'objet Field d'un Recordset, ses propriétés sont en lecture seule ; leur état est déterminé par la table sous-jacente.

La validation est le processus qui consiste à déterminer si les données entrées dans la propriété DAO Value d’un champ se trouvent dans une plage acceptable. A Field object on a Recordset may have the DAO ValidationRule and ValidationText properties set. The DAO ValidationRule property is simply a criteria expression, similar to the criteria of an SQL WHERE clause, without the WHERE keyword. The DAO ValidationText property is a string that Access displays in an error message if you try to enter data in the field that is outside the limits of the DAO ValidationRule property. If you are using DAO in your code, then you can use the DAO ValidationText for a message that you want to display to the user.

Remarque

[!REMARQUE] Les propriétés DAO ValidationRule et ValidationText existent également au niveau du Recordset. Il s'agit de propriétés en lecture seule, correspondant au modèle de validation établi pour la table de laquelle l'enregistrement actif est extrait.

A Field object on a Recordset also features the ValidateOnSet property. When the ValidateOnSet property is set to True, Access checks validation as soon as the field's DAO Value property is set. When it is set to False (the default), Access checks validation only when the completed record is updated.

For example, if you are adding data to a record that contains a large Memo or OLE Object field and that has the DAO ValidationRule property set, you should determine whether the new data violates the validation rule before trying to write the data. To do so, set the ValidateOnSet property to True. If you wait to check validation until the entire record is written to disk, you may waste time trying to write an invalid record to disk.

Assistance et commentaires

Avez-vous des questions ou des commentaires sur Office VBA ou sur cette documentation ? Consultez la rubrique concernant l’assistance pour Office VBA et l’envoi de commentaires afin d’obtenir des instructions pour recevoir une assistance et envoyer vos commentaires.