Solved

Write record to table if certain criteria exists

Posted on 2014-02-19
4
386 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 37

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 37

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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…
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.

705 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