Results 1 to 5 of 5
  1. #1
    BT Barnett is offline Novice
    Windows 10 Access 2019
    Join Date
    Nov 2024
    Posts
    2

    Search for existing record or add new

    I am constructing an Access database using VBA for the first time. I wish to search for an "MMSI" number (unique ship number used as Primary Key) to see if it exists. If it does, open the record, if not ask to add it as a new record.


    I've attempted to put together some VBA code I discovered from a similar database. My code appears to be working in so much as if the MMSI doesn't exist it will allow me to add it to the table. However, if the MMSI number exists I simply get an error code. I have tried everything that I know (which is very limited) but can't find a solution. Can you please help.
    The error I get is in this line: DoCmd.OpenForm "frmShipData", , , "MMSI=" & ID

    This is the subroutine in question:


    Private Sub ShipSearch_AfterUpdate()

    Dim ID As Long

    ID = Nz(DLookup("MMSI", "tblShip", "MMSI=""" & ShipSearch & """"), 0)
    If ID = 0 Then
    ' doesn't exist
    If MsgBox("Ship is not in database. Add as new ship?", vbYesNoCancel + vbQuestion, _
    "Add New?") = vbYes Then
    ' add as new ship
    DoCmd.OpenForm "frmShipData", , , , acFormAdd
    Forms!frmShipData!MMSI = ShipSearch
    ShipSearch = ""
    Else
    ' do nothing
    Exit Sub
    End If

    Else
    ' Ship exists'
    DoCmd.OpenForm "frmShipData", , , "MMSI=" & ID
    ShipSearch = ""
    End If


    End Sub
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    MMSI is a string so why is ID declared as integer type? Don't really need this variable.
    The ShipSearch = "" line could be outside the If Then block.
    The first Else is not necessary.
    Code:
        With Me
        If Nz(DLookup("MMSI", "tblShip", "MMSI='" & .ShipSearch & "'"), "") = "" Then
            ' doesn't exist
            If MsgBox("Ship is not in database. Add as new ship?", vbYesNo + vbQuestion, "Add New?") = vbYes Then
                ' add as new ship
                DoCmd.OpenForm "frmShipData", , , , acFormAdd
                Forms!frmShipData!MMSI = .ShipSearch
            End If
        Else
            ' Ship exists'
            DoCmd.OpenForm "frmShipData", , , "MMSI='" & .ShipSearch & "'"
        End If
        .ShipSearch = ""
        End With
    Might want to use ValidationRule and ValidationText properties of textbox to make sure user enters a valid value. Or use a combobox for selecting MMSI value and its NotInList event.

    Advise not to use exact same field name in multiple tables. For the MMSI foreign key, consider MMSI_fk.

    Note for future, post code between CODE tags to retain indentation and readability. Click # icon on post edit toolbar.
    Last edited by June7; 12-26-2024 at 03:58 AM.
    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.

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,930
    And what is the error description?

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    And what is the error description?
    Type Mismatch CJ.

    BT Barnett, when you ask for help, give us what errors you get. Do not make us go looking for them or guessing.

    Change your code to
    Code:
        Else
            ' Ship exists'
            DoCmd.OpenForm "frmShipData", , , "MMSI='" & ID & "'"
            ShipSearch = ""
        End If
    Learn the difference between text, numbers and dates and how they are referenced.

    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format
    Numbers do not need anything

    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.
    Added benefit is, if you still cannot see the error, you can copy and paste back here the output from the debug.print for someone else to spot it.
    Example:
    tt="Eg'g"
    ? dlookup("FoodID","tblFoods","FoodName = """ & tt & """")
    Last edited by Welshgasman; 12-26-2024 at 03:23 PM.
    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

  5. #5
    BT Barnett is offline Novice
    Windows 10 Access 2019
    Join Date
    Nov 2024
    Posts
    2
    Thank you for solving my query. I am extremely grateful.
    Brian Barnett

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

Similar Threads

  1. Replies: 16
    Last Post: 12-11-2017, 08:54 AM
  2. Replies: 11
    Last Post: 12-22-2014, 01:57 PM
  3. Replies: 29
    Last Post: 04-25-2014, 03:49 PM
  4. Replies: 11
    Last Post: 05-23-2012, 08:42 AM
  5. Replies: 5
    Last Post: 03-02-2012, 08:58 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