Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-10
2
Medium Priority
?
297 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 1000 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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

824 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