Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2005, 04:28 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default search for duplicates before storing new record

I am writing a small DB in MS Access 2003 for personal use to organize some journal papers. when I make a new entry I need to make sure that there are no duplicate entries. I know this is easy for a single table colunm but I need to check 3 columns "Journal name", "Volume number" and "page number". There is no single unique colunm for me to work with.

this is how I am trying to do this.

using the "BeforeUpdate" event I check that the 3 required fields are occupied then I write a query using them.

strSql = "SELECT tbl_Papers.Journal, tbl_Papers.Volume, tbl_Papers.Page " & _
"FROM tbl_Papers " & _
"WHERE (((tbl_Papers.Journal)=" & Form!Journal.Value & ") AND " & _
"((tbl_Papers.Volume)= '" & Form!Volume.Value & "' ) AND " & _
"((tbl_Papers.Page)= '" & Form!Page.Value & "' ))"

this is where I am stuck. I can execute the query in VB using docmd.runsql but I don't know how to figure out from that if there are any duplicates.

can someone help?
 
Old March 31st, 2005, 04:52 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

One thing you can do is to enforce uniqueness on the table.
In the indexes dialog you can create an index that incorporates the three fields, asserting that it must be unique. Then you will be unable to save a record that would violate that. This will behave like the table you say you do not have, one with a single column to check.

Running a query through runSQL is meaningless in the undertaking here; it is for running queries that modify the data. You need to open a recordset with these criteria, and examine the recordset's properties.

When you open the recordset (let’s say it is called ‘r’), if both r.EOF and r.BOF are true, then no records were returned matching your criteria. You can go ahead and close that recordset, then save the contents of the form.

But if that recordset has a record in it (r.EOF is False), close the recordset, explain what the deal is in a MsgBox, followed by setting Cancel = True in the BeforeUpdate event.
Cancel is a variable that it initialized by Access, then passed into the BeforeUpdate event by reference. Because it is passed in by reference, if you change it, its value in the calling routine (the part of Access that called the BeforeUpdate routine) will be changed when Access get back there. When the BeforeUpdate event is through running, Access checks the value of that variable. If it has been changed to True, Then Access cancels the update.

If I were you, for ease of reading, I would change what you have to:
Code:
   strSql = "SELECT Journal, Volume, Page " & _
            "FROM   tbl_Papers " & _
            "WHERE  Journal =  " & Form!Journal.Value & " AND " & _
            "       Volume  = '" & Form!Volume.Value & "' AND " & _
            "       Page    = '" & Form!Page.Value & "'"
            In a 1-table query there is no potential for ambiguous field references, so fully-qualifying every field reference is unnecessary.
All of those parentheses (while Access always adds them in the query builder) do nothing to make the query run better, and they are really hard on the brain.
Queries ignore white-space, so you can add spaces to line things up for clarity. (Be careful not to add spaces that will wind up inside literal strings though!)
 
Old March 31st, 2005, 06:31 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for your help.
I just thought I would publish my final code in case it helps anyone else.

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim strSql As String
    Dim rsSearch As Recordset

    'check that title, journal, volume and page are entered
    If Form!Title.Value = 0 Or Form!Journal.Value = 0 Or _
    IsNull(Form!Volume.Value) Or IsNull(Form!Page.Value) Then
        MsgBox "New entries must at least have a Title, " & _
        "Journal, Volume and Page Number"
        Cancel = True
    Else
        'query to search for duplicate records
        strSql = "SELECT Journal, Volume, Page " & _
            "FROM tbl_Papers " & _
            "WHERE Journal = " & Form!Journal.Value & " AND " & _
            " Volume = '" & Form!Volume.Value & "' AND " & _
            " Page = '" & Form!Page.Value & "'"

        'open a recordset using the query
        Set rsSearch = CurrentDb.OpenRecordset(strSql, dbReadOnly)

        'check if duplicate exists
        If Not (rsSearch.EOF) And Not (rsSearch.BOF) Then
            If Form.NewRecord Then 'must be a new record to cancel
                MsgBox "Paper is already in the database"
                Cancel = True
            End If
        End If

        rsSearch.Close 'close recordset

    End If

End Sub
 
Old April 1st, 2005, 11:30 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

If you collect your terms
Code:
       If Not (rsSearch.EOF) And Not (rsSearch.BOF) Then
becomes
Code:
       If Not (rsSearch.EOF Or rsSearch.BOF) Then
You’re welcome; glad to help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and match record from two tabel delion Pro VB Databases 1 May 15th, 2007 11:51 AM
Search for a record using a form a_wahab Access 6 September 1st, 2006 12:03 PM
How do you open a specific record from a search? Szimmer9 VB How-To 1 March 31st, 2006 12:57 AM
code to search a record Rudner VB Databases Basics 1 November 17th, 2004 11:52 PM
How to search for (and select) a record? Haroldd Access 2 June 30th, 2003 06:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.