Solved

Write record to table if certain criteria exists

Posted on 2014-02-19
4
372 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 34

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 34

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

758 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

16 Experts available now in Live!

Get 1:1 Help Now