Solved

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

Posted on 2011-03-10
2
284 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
ID: 35094860
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
ID: 35094925
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

911 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

20 Experts available now in Live!

Get 1:1 Help Now