Dynamic ADO Field Names

M

MF Scheetz

I was wondering if there was a way to use an if statement to change an ado
field name that is to be updated similar to the following:

Dim FieldNm as String
Dim cn as ADODB.Connection
Dim rsTable as ADOBD.Recordset

Set cn = Currentproject.connection
set rsTable = new adodb.recordset

rsTable.Open "SELECT * FROM TableName",cn, adOpenDynamic, adLockOptimistic

IF Variable = True Then
FieldNm = Field1
ELSEIF Variable = False then
FieldNm = Field2
endif
rsTable.addnew
rsTable.fields('"& FieldNm &"') = 2
rsTable.update

rsTable.close
cn.close

Any help is greatly appreciated.
 
D

Douglas J. Steele

What are Field1 and Field2? If they're supposed to be the names of the
fields, you're missing quotes around them:


IF Variable = True Then
FieldNm = "Field1"
ELSEIF Variable = False then
FieldNm = "Field2"
endif
 
M

MF Scheetz

Yeah they are field names. But I ended going about this problem a completely
different way. But thanks for the help!
 

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