Results 1 to 14 of 14

Thread: Finding the last similar record... [Resolved]

  1. #1

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Resolved Finding the last similar record... [Resolved]

    I have an app that deals with ording supplies..

    Now when someone chooses the item the want to order I want to display the date this item was last orderd..

    I've looked up records before with no problem, but usually a record using a specific item of data specified by the user...

    this time i need to find the last entered record with similar criteria.
    Last edited by DKasler; May 31st, 2005 at 01:02 PM.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Finding the last similar record...

    How about something like
    VB Code:
    1. strSQL = "SELECT MAX(datefield) FROM table WHERE field = '" & item_that_was_just_ordered & "'"

  3. #3

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Finding the last similar record...

    i think that could work... Thanks much.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  4. #4

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Finding the last similar record...

    How would that be inputed to a label once its called... I tried

    VB Code:
    1. Label17.Caption = datPrimaryRs![Max(SupDate)]

    but got this error "Item could not be found in the collection corresponding to the requested name or ordinal"

    I also tried the about code with out the MAX()

    Here is the complete code I used.

    VB Code:
    1. Dim sConnect As String
    2.     Dim sSQL As String
    3.     Dim dfwConn As ADODB.Connection
    4.     Dim datPrimaryRs
    5.    
    6.     ' set strings
    7.     sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=M:\supplies\supplies.mdb;Persist Security Info=False"
    8.     sSQL = "select MAX(SupDate) from SupplyMaster where SupType = '" & DBCombo2.Text & "'"
    9.    
    10.     ' open connection
    11.     Set dfwConn = New ADODB.Connection
    12.     dfwConn.Open sConnect
    13.  
    14.     ' create a recordset using the provided collection
    15.     Set datPrimaryRs = New ADODB.Recordset
    16.     datPrimaryRs.CursorLocation = adUseClient
    17.     datPrimaryRs.Open sSQL, dfwConn, adOpenForwardOnly, adLockReadOnly
    18.    
    19.     If IsNull(datPrimaryRs![MAX(SupDate)]) Then
    20.         Label17.Caption = "Never Ordered"
    21.     Else
    22.         Label17.Caption = datPrimaryRs![MAX(SupDate)]
    23.     End If
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Finding the last similar record...

    You don't need the MAX in the if statement.

    VB Code:
    1. If IsNull(datPrimaryRs![SupDate]) Then
    2.         Label17.Caption = "Never Ordered"
    3. Else
    4.         Label17.Caption = datPrimaryRs![SupDate]
    5. End If

  6. #6

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Finding the last similar record...

    I removed the Max from the if statement and Im still getting the same error.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Finding the last similar record...

    Try
    VB Code:
    1. datPrimaryRs.Fields(0)
    ' where 0 is the position in your recordset

  8. #8

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Finding the last similar record...

    Im not 100% sure where the

    VB Code:
    1. datPrimaryRs.Fields(0)

    needs to go. Though I did try it to the best of my ability and i got the same error as before.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  9. #9

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Finding the last similar record...

    like... *bump* - for suuurrre.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Finding the last similar record...

    Label17.Caption = sSQL(supdate)

    try it, i'm not sure

    pete

  11. #11

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Finding the last similar record...

    Quote Originally Posted by westconn1
    Label17.Caption = sSQL(supdate)

    try it, i'm not sure

    pete
    No, that didnt do it either. It just told me that the variable "supdate" was not defined.


    On a side note, I even tried changing the field in the db to a date/time field and that made no difference.
    Last edited by DKasler; May 31st, 2005 at 10:48 AM.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Finding the last similar record...

    i would think that sSQL should return only one record which must have must have the date in it you want, so it is just a matter off getting it out.

    put a breakpoint in your code just after you get sSQL, then have a look in the locals window at how it is made up, i presume it should be an array of fields

    pete

  13. #13
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: Finding the last similar record...

    Quote Originally Posted by DKasler
    How would that be inputed to a label once its called... I tried

    VB Code:
    1. Label17.Caption = datPrimaryRs![Max(SupDate)]

    but got this error "Item could not be found in the collection corresponding to the requested name or ordinal"

    I also tried the about code with out the MAX()

    Here is the complete code I used.

    VB Code:
    1. Dim sConnect As String
    2.     Dim sSQL As String
    3.     Dim dfwConn As ADODB.Connection
    4.     Dim datPrimaryRs
    5.    
    6.     ' set strings
    7.     sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=M:\supplies\supplies.mdb;Persist Security Info=False"
    8.     sSQL = "select MAX(SupDate) from SupplyMaster where SupType = '" & DBCombo2.Text & "'"
    9.    
    10.     ' open connection
    11.     Set dfwConn = New ADODB.Connection
    12.     dfwConn.Open sConnect
    13.  
    14.     ' create a recordset using the provided collection
    15.     Set datPrimaryRs = New ADODB.Recordset
    16.     datPrimaryRs.CursorLocation = adUseClient
    17.     datPrimaryRs.Open sSQL, dfwConn, adOpenForwardOnly, adLockReadOnly
    18.    
    19.     If IsNull(datPrimaryRs![MAX(SupDate)]) Then
    20.         Label17.Caption = "Never Ordered"
    21.     Else
    22.         Label17.Caption = datPrimaryRs![MAX(SupDate)]
    23.     End If
    This has worked for me in the previous programs

    VB Code:
    1. If IsNull(datPrimaryRs(0)) then
    2.    Label17.caption = "Never Ordered"
    3. Else
    4.    Label17.Caption = datPrimaryRs(0)
    5. end if

    I'm not sure why you have the "!" in there

    HTH
    Last edited by space_monkey; May 31st, 2005 at 11:33 AM.
    Using VB6 or VB.net 2008 with .net 3.5
    "Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad

  14. #14

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: Finding the last similar record...

    Quote Originally Posted by space_monkey
    This has worked for me in the previous programs

    VB Code:
    1. If IsNull(datPrimaryRs(0)) then
    2.    Label17.caption = "Never Ordered"
    3. Else
    4.    Label17.Caption = datPrimaryRs(0)
    5. end if

    I'm not sure why you have the "!" in there

    HTH
    Thank you so much space monkey. That worked just the way I had hoped.

    As for the "!" if you dont have it the system expects the end of statement to be "datprimaryRS" so you need the "!" to add the field after
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

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