Solved

Test whether a name already exists

Posted on 2014-01-28
4
339 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 50

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions 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 Microsoft Ac…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

828 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