Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Access - Code for a button

Posted on 2013-12-22
2
Medium Priority
?
413 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 31

Assisted Solution

by:hnasr
hnasr earned 750 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 48

Accepted Solution

by:
Dale Fye earned 750 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

Industry Leaders: 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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
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 …
Suggested Courses

704 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