Results 1 to 12 of 12
  1. #1
    AntelopeOne is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    8

    Search for existing data, add new record if not found.

    Greetings all,



    I'm just getting started with Access and trying to build a form that will allow us to scan a unique serial number off of a product bar code. If the serial number already exists, it would locate that record and populate the form with the existing data so that it can be modified. If the serial number doesn't exist, it would add a new row. I have to assume this is possible, but haven't found anything in the text I own (The Missing Manual).

    Thanks if you're able to push me in the right direction!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    If you key the serial #in the table, then

    scan the serial (to txtSerial in form)
    append it (run append query that adds it to the table...since keyed, it wont duplicate) run an append qry based on the txtSerial
    open the record. docmd.openquery "qsSerial"

  3. #3
    AntelopeOne is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    8
    Thanks but that didn't get me any further than I already am...maybe I have more homework to do than I thought.

    The serial number is already my key field, but Access doesn't complain about duplicates until I try to commit the record by clicking the 'next record' button.

    My life for a decent Access manual...

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,847
    If your serial number is designated as your Primary Key, then Access will not allow a duplicate serial number.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Everyone note that this is also posted here

    http://www.dbforums.com/showthread.p...ew-add-new-row

    Linq ;0)>

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,601
    Could try scanning into a combobox and take advantage of its NotInList event.

    Otherwise, code in textbox AfterUpdate event would search for the value in table and act accordingly to what is discovered.

    With either control, must be UNBOUND. Controls used to input search criteria must be UNBOUND.

    Use bookmarks and recordsetclone to find record without filtering or can apply criteria to the form's Filter property.

    If IsNull(DLookup("barcode", "tablename", "barcode='" & Me.controlname & "'")) Then
    'code for new record, maybe just move to new record row and set value of barcode field to the value from search control
    Else
    'code to find and move to existing record or apply filter
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by AntelopeOne View Post

    ...The serial number is already my key field...
    I asked about this in the other post, and since it appears to be true:

    Create an Unbound Textbox in your Form Header, as June7 suggested, and name it txtSerialSearch. Then use this code:
    Code:
    Private Sub txtSerialSearch_AfterUpdate()
    
     Dim rst As Recordset
    
     Set rst = Me.RecordsetClone
    
     rst.FindFirst "[SerialNum] = '" & Me.txtSerialSearch & "'"
     
      If Not rst.NoMatch Then
          Me.Bookmark = rst.Bookmark
       Else
        DoCmd.GoToRecord , , acNewRec
        Me.SerialNum = Me.txtSerialSearch
       End If
    
    rst.Close
    
    Set rst = Nothing
    
    End Sub

    This assumes that SerialNum is defined as Text. I and others generally define a Field like this as Text, even if it only contains digits, unless it is going to be used for math operations. But if you want it to be defined as a Number, replace

    rst.FindFirst "[SerialNum] = '" & Me.txtSerialSearch & "'"

    with

    rst.FindFirst "[SerialNum] = " & Me.txtSerialSearch

    Linq ;0)>

  8. #8
    AntelopeOne is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    8
    Thanks, I'll give that a try.

    As for your highlighting my post on another forum, are the two sites related somehow?

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    No, but many members, here, like myself, contribute to more than one forum. Double-posting is frowned on by members of most forums and actually forbidden by a couple of them. The problems associated with Double-posting:

    1. A member of ForumA may waste their time posting advice that has already been posted in ForumB
    2. A member of ForumA may waste their time posting advice that has already been posted in ForumB but has not resolved the issue
    3. You may respond to a request for more information on ForumA and this info won't be available to members on ForumB

    Members of this and other Access forums are professionals who are volunteering their valuable time and knowledge, to help other developers, and wasting their time is simply not cool, as my generation would say!

    If you feel that you absolutely must Double-post, for instance if you haven't received any replies to your original thread, forum etiquette dictates that you at least inform people that this has been done so that they may view what advice has already been given.

    We understand that you are new to all of this, and this isn't meant be censorial, but merely informative.

    BTW, Welcome to AccessForums.Net!

    Linq ;0)>

  10. #10
    AntelopeOne is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    8
    Gotcha! My apologies, I was simply trying to cast as wide a net as possible, lesson learned.

    So...the code you provided is working well, as expected, thank you again for taking the time.

    I still have a few things to work out in terms of auto-tabbing, and maybe Google can help me now that I'm a bit further.

    I do have one more question for now, if you would be so kind.

    As it stands, when I scan a serial that is a new record, it autotabs to the ID field, and if I scan the serial again, it populates the ID field (autonumber), and the focus moves to the PartNum field, which will also be scanned in from the label. So that will work as is...






    As I'm trying to streamline things as much as possible for the end users, ideally, once the label was scanned and the focus moved into the data fields, the text in the search field would clear. Or, upon committing the data to the table, the focus would return to the search field so that another scan would replace the existing serial. I'm assuming I just need one line of code to add to the end of your example to clear the existing serial, but everything I have tried throws an error. Returning focus to the search field is probably more involved, but maybe a better solution?

    Thank you again, good sir, and thank you for the welcome

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,601
    What have you tried? The following should work:

    Me.txtSerialSearch = Null
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    AntelopeOne is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    8
    I was trying "= Nothing", thank you!

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

Similar Threads

  1. The search key was not found in any record
    By virtualprg in forum Import/Export Data
    Replies: 46
    Last Post: 08-26-2014, 10:51 AM
  2. Search key was not found in any record.
    By mgio in forum Access
    Replies: 2
    Last Post: 08-05-2014, 01:32 PM
  3. Replies: 2
    Last Post: 11-12-2013, 07:06 PM
  4. Replies: 7
    Last Post: 04-17-2013, 04:33 PM
  5. Replies: 10
    Last Post: 03-28-2011, 08:57 AM

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