Ler e gravar em um campo em um Conjunto de Registros de 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. Portanto, você pode definir a propriedade Valor DAO do campo LastName no rstEmployees Recordset de qualquer uma das seguintes maneiras.

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. Se essa propriedade for True, o objeto Recordset pode ser atualizado.

Campos individuais em um objeto Recordset atualizável talvez não sejam atualizáveis, e tentar gravar nesses campos gerará um erro em tempo de execução. 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. Para um objeto Field de um Recordset, essas propriedades são somente leitura; o estado delas é determinado pela tabela de base.

Validação é o processo de determinar se os dados inseridos na propriedade Value de um campo estão dentro de um intervalo aceitável. 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. A propriedade ValidationText do DAO é uma cadeia de caracteres que o Access exibe em uma mensagem de erro quando você tenta inserir no campo dados que estão fora dos limites da propriedade ValidationRule do DAO. Se você estiver usando o DAO em seu código, poderá usar o ValidationText do DAO em uma mensagem que queira exibir para o usuário.

Observação

As propriedades ValidationRule e ValidationText do DAO também existem no nível de Recordset. These are read-only properties, reflecting the table-level validation scheme established on the table from which the current record is retrieved.

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. Quando ela é definida como False (o padrão), o Access verifica a validação somente quando o registro inteiro é atualizado.

Por exemplo, se você estiver adicionando dados a um registro que contenha um campo grande de memorando ou objeto OLE e que tenha a propriedade ValidationRule do DAO definida, deverá determinar se os novos dados violam a regra de validação antes de tentar gravá-los. Para isso, defina a propriedade ValidateOnSet como True. Se você aguardar para verificar a validação até o registro todo ser gravado no disco, poderá perder tempo tentando gravar um registro inválido no disco.

Suporte e comentários

Tem dúvidas ou quer enviar comentários sobre o VBA para Office ou sobre esta documentação? Confira Suporte e comentários sobre o VBA para Office a fim de obter orientação sobre as maneiras pelas quais você pode receber suporte e fornecer comentários.