Solved

MS Access - Code for a button

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

770 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