Results 1 to 9 of 9
  1. #1
    robsworld78 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    181

    Need to add 2nd criteria to strSQL

    I have a form that updates a field for a table for a specific order when the form opens, now I want to apply that same update except with a 2nd criteria. Here's what I have that works on the OnLoad event.



    Dim strBarsReturned As String
    Dim OrderID As Long
    Dim strSQL As String

    strBarsReturned = 0
    On Error Resume Next
    OrderID = Me!NightCountOrdersSubform.Form!OrderID

    strSQL = "UPDATE Inventory set Inventory.[BarsReturned] = " & strBarsReturned & " where Inventory.[OrderID] = " & OrderID
    CurrentDb.Execute strSQL

    Now I want to add the following code but can't get it.

    "where Inventory.[BarsSold] = " & IsNull

    This would go in the OnClose event. The idea is if someone blanks out the field on the form when it closes it will add a 0 to the blank records on that order for that field.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Try

    strSQL = "UPDATE Inventory set Inventory.[BarsReturned] = " & strBarsReturned & " where Inventory.[OrderID] = " & OrderID & " AND Inventory.[BarsSold] Is Null"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    robsworld78 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    thanks, that does work however I need it to be different, instead of updating with a 0 I need it to update from a control on the form. Here's what I have.

    Dim strBarsSold As String
    Dim OrderID As Long
    Dim strSQL As String

    strBarsSold = Me!NightCountInventorySubform.Form!BarsLeft
    OrderID = Me!NightCountOrdersSubform.Form!OrderID

    strSQL = "UPDATE Inventory set Inventory.[BarsSold] = " & strBarsSold & " where Inventory.[OrderID] = " & OrderID & " AND Inventory.[BarsSold] Is Null"

    CurrentDb.Execute strSQL

    What this does is populate all the records for that order with the same number, not the number in BarsLeft for that specific record. Its picking one record from the BarsLeft subform and using it for all the records in the order. I need it to go record by record. These records use InventoryID for the ID. The 2 subforms have a record set so the records follow each other on each form.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Am I missing something? It sounds like you just need to use InventoryID instead of OrderID.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    robsworld78 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    That's what I thought however it doesn't work, no errors come with the code, if I change the location for InventoryID it throws an error cause it can't find so it is finding it with the following code. The InventoryID is on 2 subforms I tried referring to both but same results.

    What I originally wanted was the form not to close if a field is left blank or is a negative number, couldn't get that working so I tried this. I think doing the originally idea is easier and works just as effective if I can get it for all records.

    When I hit the post button on the form this is the code that's run.

    If Forms!NightCount!NightCountEnterReturnsSubform.For m!BarsReturned >= 0 And Forms!NightCount!NightCountEnterReturnsSubform.For m!BarsSold >= 0 Then
    Me.Status = "Review"
    DoCmd.Close
    Else
    MsgBox "You must have missed an item, please try again! ", , "Please Try Again"
    End If

    The msgbox will show up if your on the record that's empty or a negative. So If I blank "BarsReturned" on one record the error will show as long as I stay on that record, when I go to another record it will post the order because the current record it sits on isn't empty or negative. If forgets to look at all the child records on the order.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    I would probably use the subform's before update event to prevent the 0 or Null records from getting saved in the first place. Or setting a validation rule at the table level so Access will enforce it for you.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    robsworld78 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Thanks, I feel like an idiot, I forgot about the validation rule, that should work fine.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Happy to help. Sometimes there are so many ways to do something it's hard to remember them all.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    robsworld78 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Yeah how true but that's whats nice about access, anything can be accomplished if you have the know how and a good day, hopefully soon I'll have the know how.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. DLookup in strSQL for OpenRecordset
    By szucker1 in forum Programming
    Replies: 2
    Last Post: 07-30-2011, 06:00 PM
  2. Replies: 1
    Last Post: 07-13-2011, 11:00 AM
  3. Replies: 1
    Last Post: 06-12-2011, 07:08 AM
  4. Criteria help
    By jcaptchaos2 in forum Access
    Replies: 13
    Last Post: 05-08-2011, 06:49 PM
  5. Search By Criteria - Flexible Criteria Fields
    By lilanngel in forum Access
    Replies: 0
    Last Post: 03-16-2011, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums