Solved

Write record to table if certain criteria exists

Posted on 2014-02-19
4
380 Views
Last Modified: 2014-02-19
I have a form with a field name txtProdRef which is a text field.

I want to add a record to an unbound table using the following code IF the ProdRef string DOES NOT already exist in tblProducts.  I do not know what code to put before the code below.

        Set RS = CurrentDb.OpenRecordset("tblProducts")
        RS.AddNew
        RS!ProdName = Me.txtfieldname
        RS!FormatID = Me.txtfieldname
        RS!SizeID = Me.txtfieldname
        RS!LocationID = Me.txtfieldname
        RS!ClassID = Me.txtfieldname
        RS!ProdRef = Me.txtfieldname
        RS!ImageID = Me.txtfieldname
        RS.Update
0
Comment
Question by:SteveL13
  • 2
  • 2
4 Comments
 
LVL 36

Expert Comment

by:PatHartman
ID: 39870993
You could use DLookup() or DCount() to determine if the value already exists.  If it doesn't, then you would do the AddNew.  If it does, you would bypass the add and perhaps raise an error message.

A better technique for simple rules like this is to create a unique index on the column in the table.  That way if someone attempts to add a record via any means, the add will fail if the value already exists.
0
 

Author Comment

by:SteveL13
ID: 39871202
Ok.  I'm trying to use DLookup with this line of code but the syntax must be wrong.

If DLookup("[ProdRef]", "tblProducts" <> Me.txtTransferredProdRef Then

In other words, if no ProdRef in tblProducts equals Me.txtTransferredProdRef then the next line should fire which is a msgbox


??
0
 
LVL 36

Accepted Solution

by:
PatHartman earned 500 total points
ID: 39871250
You need to include a where argument to tell the DLookup() what record you are interested in.


If DLookup("[ProdRef]", "tblProducts", "ProdRef = " & me.txtTransferredProdRef) <> Me.txtTransferredProdRef

The code assumes that ProdRef is the column name and it is numeric.  If it is text, the syntax would be:

If DLookup("[ProdRef]", "tblProducts", "ProdRef = '" & me.txtTransferredProdRef & "'") <> Me.txtTransferredProdRef

Intellisense should have shown you the correct syntax.  Are you not getting it?
0
 

Author Comment

by:SteveL13
ID: 39871428
This was it...

If DLookup("[ProdRef]", "tblProducts", "ProdRef = '" & me.txtTransferredProdRef & "'") <> Me.txtTransferredProdRef

Yes, intellisese is working.  I just didn't know how to correct it.

Thanks again.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

828 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