Solved

Write record to table if certain criteria exists

Posted on 2014-02-19
4
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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.

740 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