A VB and SQL question....

Discussion in 'OT Technology' started by XtremelyCanadian, Apr 26, 2004.

  1. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    Im making a little program in VB with an SQL backend. Just trying to get back into SQL and have a few questions.

    I have this code which raises an error if more than one of the same customer is added.
    When it comes back into VB i get the worse error message ever, where you have to debug or end the program. Anyway to just have a message box that tells the user the error without ending the program ???

    Also i am trying to select a bunch of records from a table that have the earliest date...such as

    Sept 23, 1981 - Frank
    Sept 23, 1981 - Bob
    Jan 1, 2004 -Sandy
    Feb 8, 2003 - Al

    I only want to select Frank and Bob...i know there is a way to do this but the code escaped me for some reason ???

    Any help would be appreciated :bigthumb:
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    You need to read about Error handling in VB. Something similar to this would do the trick:

    Code:
    Sub YourSub
       On Error Goto Handle_Error
    
       'sql code here
    
    Exit Sub
       Handle_Error:
          MsgBox "Error occured"
    
    End Sub
    
    with your other query, you can do:
    Code:
    SELECT TOP 2 *
    FROM Your_table
    ORDER BY YourDateField
    
    this will order your records in earliest to latest dates, so the top whatever (2 in this case) records will be the earliest.
     
  3. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    The only problem with the top command is, i dont know how many records will be in my table with the same earliest date, could be 5 or 50. I tried taking out the set number, but it didnt work...any other suggestions ???
     
  4. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    you could do a first select and return your earliest date "SELECT MIN(date_field) FROM Your_Table"

    then return that to VB, and then do your select "WHERE date_field = [date from above]"
     
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    or i guess you could combine it in a single query:

    Code:
    SELECT *
    FROM your_table
    WHERE Date_field = (SELECT MIN(date_field) FROM your_table)
    
     
  6. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    doh, sub-query, good idea, ill try that, thanks :bigthumb:
     

Share This Page