Results 1 to 10 of 10
  1. #1
    Archer's Avatar
    Archer is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Mar 2019
    Location
    Down Under
    Posts
    16

    Check if record exists based on 1 field.

    Hi Folks.



    I want to have a field on my Access Form check to see if the record i'm about to add already exists based on the text value of the Field I am about to enter data into so essentially, something on the 'After Update' event that checks if that value already exists, if so takes me to that record, if not, proceeds with the data entry. Tried working my way through this with Macros with no joy so hoping someone has a vba answer?

    thanks in advance.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    in an unbound form (fLookup) , with an text box, (txtBox)

    Code:
    sub txtBox_afterupdate()
    dim lCt as long
    
    lCt = Dcount("*","table","[field]='" & me.txtBox & "'")
    if lCt = 0 then
         docmd.openform "fDataEntry",,,,acFormAdd
         forms!fDataEntry!txtBox = forms!fLookup!txtBox
    else
         msgbox lCt & " exist"
    endif
    end sub

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I don't use macros, only VBA.

    Can use domain aggregate function (DLookup, DCount, etc) to search.

    I suppose by 'field' you mean an UNBOUND control - a textbox? If you enter into a BOUND control then a new record is initiated. Can cancel the record depending on result of search.
    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.

  4. #4
    Archer's Avatar
    Archer is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Mar 2019
    Location
    Down Under
    Posts
    16
    Sorry my bad. Should have stated it's a Form bound to a table.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I wonder why user would be entering data that may have already been entered.

    Can set field in table to not allow duplicates then Access can nag the user when they try to enter duplicate. Otherwise, perhaps code in textbox BeforeUpdate event.
    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.

  6. #6
    Archer's Avatar
    Archer is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Mar 2019
    Location
    Down Under
    Posts
    16
    Quote Originally Posted by June7 View Post
    I wonder why user would be entering data that may have already been entered.

    Can set field in table to not allow duplicates then Access can nag the user when they try to enter duplicate. Otherwise, perhaps code in textbox BeforeUpdate event.
    User does not know if record already exists. Setting not to allow duplicates will only notify user after the end of the data entry process. There are 60 fields on this form. I want to notify the user before they proceed any further and then take them to the record that already exists.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    But why? What about your business process would allow the same data to go through data entry process again?

    Have you attempted code in the textbox BeforeUpdate as suggested?
    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.

  8. #8
    Archer's Avatar
    Archer is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Mar 2019
    Location
    Down Under
    Posts
    16
    Quote Originally Posted by June7 View Post
    But why? What about your business process would allow the same data to go through data entry process again?

    Have you attempted code in the textbox BeforeUpdate as suggested?
    I'm attempting to put code on the 'After Update' event of the field in question but a novice at VBA so was looking for an example.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I am suggesting you use BeforeUpdate. Something like:
    Code:
    Private Sub tbxFieldname_BeforeUpdate(Cancel As Integer)
    Dim strVal As String
    strVal = Me.tbxFieldname
    If DCount("*", "tablename", "fieldname='" & strVal & "'") > 0 Then
        Me.Undo
        Me.Filter = "fieldname='" & strVal & "'"
        Me.FilterOn = True
    End If
    End Sub
    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.

  10. #10
    Archer's Avatar
    Archer is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Mar 2019
    Location
    Down Under
    Posts
    16
    Quote Originally Posted by June7 View Post
    I am suggesting you use BeforeUpdate. Something like:
    Code:
    Private Sub tbxFieldname_BeforeUpdate(Cancel As Integer)
    Dim strVal As String
    strVal = Me.tbxFieldname
    If DCount("*", "tablename", "fieldname='" & strVal & "'") > 0 Then
        Me.Undo
        Me.Filter = "fieldname='" & strVal & "'"
        Me.FilterOn = True
    End If
    End Sub
    Works perfectly. Thank you sir. Much appreciated. Clearly I would have taken the wrong approach.

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

Similar Threads

  1. Check to see if record already exists
    By zipaway in forum Programming
    Replies: 4
    Last Post: 06-05-2014, 09:16 AM
  2. VBA code to check if a record already exists
    By fra90 in forum Programming
    Replies: 3
    Last Post: 11-20-2013, 11:20 AM
  3. Check if record exists based off one field
    By cactuspete13 in forum Forms
    Replies: 3
    Last Post: 01-14-2013, 05:56 PM
  4. Check if record exists, Check Number
    By burrina in forum Forms
    Replies: 9
    Last Post: 01-06-2013, 03:49 PM
  5. Replies: 3
    Last Post: 10-19-2012, 04:30 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