rdgWarren
rdgWarren

Reputation: 87

ADODB.Recordset: referencing columns with troublesome names- vb6 solution fails in vb .net

I have some old vb6 code that I'm moving to .NET. It uses ADODB recordsets, and some of the column names are troublesome: starting with digits or containing spaces. VB6 worked by enclosing them in brackets, but VB .NET throws a compile error.

Reference to typical column name:

rsVals!Break1Yr4

References to troublesome column names:

rsVals![1_yr]

rsVals![Gross 1 Year]

Sample code:

Dim rsVals As New ADODB.Recordset
rsVals.Open("Mf_BarChart", conn)
Dim Net1Yr, Gross1Yr
Net1Yr = rsVals![1_Yr]
Gross1Yr = rsVals![Gross 1 Year]

That compiles fine on VB6. However, in .NET the reference to [1_Yr] gives 'identifier expected' and the reference to [Gross 1 Year] gives 'Bracketed Identifier is missing closing ']'" I have not been able to come up with a syntax it will accept. Does anyone know how to do this?

Upvotes: 0

Views: 317

Answers (1)

MarkL
MarkL

Reputation: 1771

Instead of using the ! notation, try the Fields property on the recordset:

Net1Yr = rsVals.Fields("1_Yr")
Gross1Yr = rsVals.Fields("Gross 1 Year")

You won't need to use the brackets on the column name for the Fields property - though if you include that column on the column list of your select statements the brackets are (still) needed there.

Upvotes: 1

Related Questions