Solved

MS Access - Code for a button

Posted on 2013-12-22
2
411 Views
Last Modified: 2013-12-26
Just starting in Access ... I need help with the following.

I have BRAND_NAME in the current navigation form ( refernences a field of the same name).
I have a button on the same form... I want when the button clicked to check on a table called BRAND_NAME has one field CALLED BRAND_NAME. If that value does not exist, then add the value. If the value exist then ignore. I also need that comparing the value doesn't care about case.

How can I do that?

I am started over 40 Years no school... Just for a hobby / side work.


Thanks
Emad
0
Comment
Question by:egirgis
[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 Comments
 
LVL 30

Assisted Solution

by:hnasr
hnasr earned 250 total points
ID: 39735165
Check if value of BRAND_NAME exists in table BRAND_NAME.  
If DLookup("[BRAND_NAME]", "[BRAND_NAME]", "[BRAND_NAME]=" & BRAND_NAME) > 0 Then
        Me.Undo
Else
        DoCmd.RunCommand acCmdSaveRecord
End If

Open in new window

0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 39735875
just a note, since you are new to Access.

You might want to consider using a naming convention for your tables, forms, reports, queries, and the controls you use in your forms and reports.

Naming conventions (there are several) make it easier to understand your code or SQL as you are trying to learn how to write and edit software.  For example, I preface all of my tables with the prefix "tbl_".  This makes it easier for me to tell whether [BRAND_NAME] is a table or a field.

Several references for naming conventions are:

http://support.microsoft.com/kb/110264
http://en.wikipedia.org/wiki/Leszynski_naming_convention

Also, when you use DLOOKUP, if the [Brand_Name] field, contains a string, then you will need to wrap that value in quotes in the criteria argument of the DLOOKUP() domain function.  Furthermore, if DLOOKUP() doesn't find the value you are looking for, it will return a NULL value, so you must test for that, not >0

Personally, I like to write my criteria strings outside the DLOOKUP() , but that is just personal preference.

Dim strCriteria as string

strCriteria = "[BRAND_NAME]=" & chr$(34) & me.[BRAND_NAME] & chr$(34)
If ISNULL(DLookup("[BRAND_NAME]", "[BRAND_NAME]", strCriteria)) = False Then
    me.[Brand Name]Undo
    msgbox "Brand name already in use"
Else
    'this saves the record, same as DoCmd.RunCommand acCmdSaveRecord
     me.Dirty = false  
End If

Another way to do it instead of using DLOOKUP, would be to use DCOUNT() which would return the number of records that match your criteria.  With DCOUNT() you would test for >0, similar to:

If DCount("[BRAND_NAME]", "[BRAND_NAME]", strCriteria) > 0 Then
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

751 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