Solved

How check for existing record and edit if exists otherwise create new record

Posted on 2011-03-10
2
277 Views
Last Modified: 2012-05-11
I want to open a form to edit a record if it already exists OR create a new record if it does not exist.  The form is bound to a table.  But two field criterias have to be met in determining if the record exists already or not.  How can I do this?
0
Comment
Question by:SteveL13
2 Comments
 
LVL 6

Accepted Solution

by:
TinTombStone earned 250 total points
Comment Utility
The following uses the DLookup() function to check for a particular CustomerID before opening the form in Add (not found) or Edit (found) mode

Sub OpenFormInAddOrEditMode()

Dim custID As String

    custID = "CUST1"

    If Not IsNull(DLookup("[CustomerID]", "[tblCustomers]", "[tblCustomers].[CustomerID]='" & custID & "'")) Then
    'or for a numeric field
    'If Not IsNull(DLookup("[CustomerID]", "[tblCustomers]", "[tblCustomers].[CustomerID]=" & custID)) Then
        DoCmd.OpenForm "frmCustomers", acNormal, , "[CustomerID]='" & custID & "'", acFormEdit, acWindowNormal
    Else
        DoCmd.OpenForm "frmCustomers", acNormal, , , acFormAdd, acWindowNormal
    End If

End Sub
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
Comment Utility
i suggest that you add an openargs option when opening the form in add mode

DoCmd.OpenForm "frmCustomers", acNormal, , , acFormAdd, acWindowNormal,openargs:=custID

then in the open event of the form, use this codes to set the value of the field automatically

private sub form_load()

if me.openargs & ""<> "" then
  me.custID=me.openargs
end if

end sub
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now