A
Alex Leonard
I am trying to build a function that adds a record to an
existing table, where the field name is provided by means
of a string variable.
e.g. this does not work:
Function AddData()
Dim dbs As Database, rst As DAO.Recordset, fld As String
fld = "MyField"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("MyTable")
With rst
.AddNew
!fld = "test" 'should be ![MyField]
.Update
End With
End Function
I can get it to work by comparing the names of each field
in the table to the string variable "fld" and setting the
value of the matching item to the new data, but this seems
pretty clutzy.
Is there any easier way to refer to the field (rst!
[MyField]) by means of a variable containing the field
name?
existing table, where the field name is provided by means
of a string variable.
e.g. this does not work:
Function AddData()
Dim dbs As Database, rst As DAO.Recordset, fld As String
fld = "MyField"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("MyTable")
With rst
.AddNew
!fld = "test" 'should be ![MyField]
.Update
End With
End Function
I can get it to work by comparing the names of each field
in the table to the string variable "fld" and setting the
value of the matching item to the new data, but this seems
pretty clutzy.
Is there any easier way to refer to the field (rst!
[MyField]) by means of a variable containing the field
name?