Solved

MS Access - Code for a button

Posted on 2013-12-22
2
406 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
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now