1

I have some sql that works fine in access 2010 and returns the correct values. However, in VB.NET, when I run it causes the above error in the while loop. dr.HasRows is returning 'TRUE' but the var 'itm' shows nothing in auto window. Can someone please explain why this is happening and how I can correct it. Thanks

Dim cmd As OleDbCommand = New OleDbCommand("SELECT Max(Requests.[Request no]) AS [MaxOfRequest no], Requests.Customer, " &
                    "Max([Request Boxes].[Request no]) AS [MaxOfRequest no1], [Request Boxes].Customer " &
                    "FROM Requests, [Request Boxes] " &
                    "GROUP BY Requests.Customer, [Request Boxes].Customer " &
                    "HAVING (((Requests.Customer)='" & cmbCustomer.Text & "') AND (([Request Boxes].Customer)='" & cmbCustomer.Text & "')) " &
                    "ORDER BY Requests.Customer, [Request Boxes].Customer", oledbCnn)

            dr = cmd.ExecuteReader()

            If dr.HasRows Then <--- this returns true

                While dr.Read

                    itm = CStr(dr.Item("[MaxOfRequest no]")) <--- ERROR HERE
                    itm2 = CStr(dr.Item("[MaxOfRequest no1]"))

                End While

            End If
1
  • That dr.HasRows is true means there are rows. It doesn't mean that a particular field (like [MaxOfRequest no]) is present - which the error is trying to say. Commented Jan 24, 2014 at 12:42

1 Answer 1

2

You don't need the square brackets here:

itm = CStr(dr.Item("[MaxOfRequest no]")) 

The square brackets are just delimiters in the SQL statement; they are not part of the column name itself. Try this instead:

itm = CStr(dr.Item("MaxOfRequest no"))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much Gord

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.