Solved

MS Access - Code for a button

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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