Recordset Field caption property

T

TCF

I am copying data from access to excel and using the following code
For i = 0 To rs2.Fields.Count - 1
CurrentValue = rs2.Fields(i).name
xlApp.Cells(j, i + 1).Value = CurrentValue
Next i

Instead of using the name of the rs2.fields(i), I want to use its caption
property. Is there a way to do this?

thanks
 
D

Dirk Goldgar

TCF said:
I am copying data from access to excel and using the following code
For i = 0 To rs2.Fields.Count - 1
CurrentValue = rs2.Fields(i).name
xlApp.Cells(j, i + 1).Value = CurrentValue
Next i

Instead of using the name of the rs2.fields(i), I want to use its caption
property. Is there a way to do this?


If a caption was defined for the field, it should have a property named
"Caption" in its Properties collection. But if no caption was defined, that
property won't exist. You have to trap or ignore the error, maybe by using
a function like this:

'----- air code -----
Function fncFieldCaption(fld As DAO.Field)

On Error Resume Next
fncFieldCaption = fld.Properties("Caption")
If Err.Number <> 0 Then
fncFieldCaption = fld.Name
End If

End Function
'----- end code -----

Then you could modify your posted code to:

For i = 0 To rs2.Fields.Count - 1
CurrentValue = fncFieldCaption(rs2.Fields(i))
xlApp.Cells(j, i + 1).Value = CurrentValue
Next i
 
J

Jeanette Cunningham

TCF,
try

CurrentValue = rs2.Fields(i).Properties("Caption").Value


Jeanette Cunningham
 

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