Results 1 to 14 of 14
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191

    Adding A Single Record When Criteria Matches

    Hi Guy's, this should be a simple answer that for some reason I can't think why this doesn't work properly!!!!

    I may have 12 records that is DelNo 1

    This event is After Update on DelNo, so only on the 1st instance it needs to add to tblJobSheets2 1 x record

    Name-Town-PostCode 12 Items

    Once you change the next DelNo (2 of 12), im trying to stop it adding because the record now exists

    It is adding 12 records instead so i believe the If Not Is NUll is not doing it's job

    I haven't put full code on as the result is correct just adding however many records that are for the selected DelNo instead of 1 record only

    Any advise is appreciated, its probably so simple and jut me having a logic lapse



    Code:
    If Not IsNull(DLookup("DelNo", "tblJobSheets2", "[Vehicle] = '" & strVehicle & "' And [DelDate] = #" & dtDelDate & "# And [DelNo] = " & intDelNo)) Then    DoCmd.CancelEvent
        Else
        With rs2
        .AddNew
        !IDNumber = iJS
        !DelDate = dtDelDate
        !Driver = strDriver
        !Vehicle = strVehicle
        !DelNo = intDelNo
        !DelTo = Me.DelTo & "-" & Me.Town & "-" & Me.PostCode
        !Items = strAddDel
        !EntCode = strEntCode
        !ExtCode = strExtCode
        .Update
        .Close
        End With
        
        End If

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Walk through your code
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Hi WGM, yes i am going to do that because I think the item count is zero so thats then next step as you have suggested

    I just can't get my head around why i can't do the simplest tasks ever which is: Count a set of records with criteria's

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    To count you use DCount with "*":
    Code:
    If DCount("*", "tblJobSheets2", "[Vehicle] = '" & strVehicle & "' And [DelDate] = #" & dtDelDate & "# And 	[DelNo] = " & intDelNo)=0 Then    
        With rs2
        .AddNew
        !IDNumber = iJS
        !DelDate = dtDelDate
        !Driver = strDriver
        !Vehicle = strVehicle
        !DelNo = intDelNo
        !DelTo = Me.DelTo & "-" & Me.Town & "-" & Me.PostCode
        !Items = strAddDel
        !EntCode = strEntCode
        !ExtCode = strExtCode
        .Update
        .Close
        End With
    End If
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by DMT Dave View Post
    Hi WGM, yes i am going to do that because I think the item count is zero so thats then next step as you have suggested

    I just can't get my head around why i can't do the simplest tasks ever which is: Count a set of records with criteria's
    I think I have said this before.
    If you have more than one criteria, put it all into a string variable. Than you debug.print it and inspect it.
    Whilst that does not appear to issue here, always worth doing, as you walk through your code.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Thank you guy's i can't come back to this at the moment, soon as i can get back to it, will totally take on what you have suggested and run the debug.print to see results

    Thanks Again

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Really sounds like you should be using DCount() ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Yes just picked up on that, i totally forgot about the wildcard rather than counting specific field, count anything within criteria's

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by DMT Dave View Post
    Yes just picked up on that, i totally forgot about the wildcard rather than counting specific field, count anything within criteria's
    Could just as easy used a fieldname?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Yeh, would typically use postcode as all postcodes in contacts are unique then fine tune to names, vehicles, drivers

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Whilst postcodes are themselves unique, it is quite likely that you will have two or more contacts with the same postcode
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    We have 14 houses in our cul de sac.
    Amazon must deliver to pretty much all of them. :-)
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Thanks guys, we never deliver to any postcodes that close, the competition un the market is too great but your right if we were door to door then postcodes may not be in certain circumstances as unique as a primary key ID NUmber

  14. #14
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Never...yet! It will happen at some point!
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. Replies: 3
    Last Post: 12-26-2020, 07:48 PM
  2. Replies: 2
    Last Post: 02-15-2019, 01:38 PM
  3. Replies: 6
    Last Post: 06-24-2014, 10:09 PM
  4. Replies: 6
    Last Post: 07-02-2012, 02:53 PM
  5. Adding a single record
    By kfoyil in forum Forms
    Replies: 2
    Last Post: 11-22-2006, 09:12 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