Refer to fieldname using a variable

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top