Solved

Test whether a name already exists

Posted on 2014-01-28
4
336 Views
Last Modified: 2014-01-28
I have a form with a textbox "Blockname" which is bound to a field "Block" in table "tblBlocks".
Block is supposed to be a unique name.
When I fill in a new "Block", before I commence filling in other applicable data, I want a warning like "This name already exists. You need to enter a uniqe name or press "Escape" to exit."
I will probably use a "before update" action. Is that right?
What does the VBA look like that tests if the name already exists?
Thanks.
0
Comment
Question by:Fritz Paul
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39814594
Before Update code would look like this:

IF DCount("*","tblBlock", "BlockName = '" & Me.txtBlock & "'") > 0 then
      msgbox  "This name already exists. You need to enter a uniqe name or press 'Escape' to exit."
      Cancel = True
      Me.txtBlock.Undo
End IF

Open in new window

0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 39814595
Hi,

Maybe by using a validation rule with Isnull(Dlookup()) otr DCount() in your table

Regards
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39814597
The above is assuming that Block is a text field.  If it is numeric, you would omit the single quote delimiters:

IF DCount("*","tblBlock", "BlockName = " & Me.txtBlock ) > 0 then
      msgbox  "This name already exists. You need to enter a unique name or press 'Escape' to exit."
      Cancel = True
      Me.txtBlock.Undo
End IF

Open in new window

0
 

Author Closing Comment

by:Fritz Paul
ID: 39814626
Thanks!
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

746 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