Results 1 to 4 of 4
  1. #1
    fra90 is offline Novice
    Windows 8 Access 2003
    Join Date
    Nov 2013
    Posts
    1

    VBA code to check if a record already exists

    Hello everyone,

    I have a Form (ITEMS) where it's also possible to insert
    the supplier name of a particular item. I'm trying to use a macro to check if
    the supplier name registered in the form ITEMS already exists in the table
    "Suppliers". If it doesn't, then tha macro should show a message to ask to the
    user if he/she wants to update the "suppliers" table with the info of the new
    supplier.

    I saw on the web that I can use as conditional expression in
    the macro the commands IsNull(DlookUp... etc.
    however I'm having difficulties to


    make this command works..
    the syntax that I'm using is the following :


    IsNull(DLookUp("[Supplier Name]","Suppliers","[Supplier Name]='" &
    [Forms]![ITEMS]![SupplierName] & "'"))

    Basically in the command I
    want to say: "look for the "Supplier Name" in the table "Suppliers" that is
    equal to the "SupplierName" of the form ITEMS just added. And tell me if you
    find it.

    but apparently there is something wrong in the syntax because
    when I try to updade the SupplierName in the form ITEMS it gives me as message
    "type mismatch".

    I hope you guys can help me to solve this problem

  2. #2
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    Use Dcount instead of Dlookup. If value returned is 1 supplier exists else does not. Be careful with spelling & Lowercase/Uppercase names.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I doubt DCount will help with the type mismatch error.

    Are you using a combobox to select supplier? If not in the list then supplier doesn't exist. Use combobox NotInList event to handle when item not found. I would use VBA for this but might be possible with macro. According to MS: To run a macro or event procedure when this event occurs, set the OnNotInList property to the name of the macro or to [Event Procedure].

    Shouldn't save supplier name, should save an ID.
    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
    naeemahmad is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Location
    Riverside, USA
    Posts
    46
    I used the code below to check if records exist using DLookup.. but I have since changed to DCount.

    Private Sub MyCombo_BeforeUpdate(Cancel As Integer)

    If Not IsNull(DLookup("RacerID", "Medals", "RaceID = " & Val(EventCombo.Columns(0)) & " AND Medal = '" & MedalCombo.Columns(0) & "'")) Then
    MsgBox "Record Exists"
    End If

    End Sub
    Last edited by naeemahmad; 11-20-2013 at 02:53 PM.

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

Similar Threads

  1. Check if record exists based off one field
    By cactuspete13 in forum Forms
    Replies: 3
    Last Post: 01-14-2013, 05:56 PM
  2. Check if record exists, Check Number
    By burrina in forum Forms
    Replies: 9
    Last Post: 01-06-2013, 03:49 PM
  3. Replies: 3
    Last Post: 10-19-2012, 04:30 PM
  4. Trouble with check if Exists before Edit or Add Record
    By mrfixit1170 in forum Programming
    Replies: 4
    Last Post: 02-10-2012, 10:38 AM
  5. Check to see if record id exists in joined table
    By csoseman in forum Programming
    Replies: 1
    Last Post: 08-18-2011, 01:06 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