CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2003
    Posts
    42

    Catching SQL query

    Hi

    I have a very simple SQL query like this:
    strSQL = "select max(fieldname) from tablename"

    'Open connection ...
    'Excute query...
    'Until here everything goes well ...
    dim iMax as integer
    'Error getting value
    iMax = rs.Fields("fieldname").value

    But it doesn't work. Anyone any idea?

    Greetings,
    Jay

  2. #2
    Join Date
    Jun 2003
    Location
    Planet Earth
    Posts
    186
    Try this:

    iMax = rs("fieldname")

  3. #3
    Join Date
    Nov 2002
    Location
    Oakville, Ontario
    Posts
    48
    when using an aggregate function like MAX, even if it does not return an apparant result, it will return a row (in Oracle anyway), so you may be getting the error due to a null value

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    iMax = rs.Fields(0).value
    the recordset has a filed wich is not "fieldname"
    unless you do something like
    "select max(fieldname) as TheFieldDescription from tablename"
    and only then you could do:
    iMax = rs.Fields("TheFieldDescription").value
    If table is empty, you should get a record in any case.
    Do not know if null or 0 value (cannot test right now)...
    If null you can do
    iMax = iif(isnull(rs.Fields("TheFieldDescription").value), 0,rs.Fields("TheFieldDescription").value)
    Last edited by Cimperiali; March 15th, 2004 at 07:04 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Another thing: integer vs long

    You declared iMax as integer.
    This means max value should not excede around 32000
    If you could get a higher value, then declare it as long.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured