Solved

Access - VB to test if a Record exists before adding new using Recordset

Posted on 2013-05-15
3
414 Views
Last Modified: 2013-06-09
Hello experts:

Sorry for this stupid question. Being a novice when I had to put my project down for several months I'm fuzzy on writing my code again.

Anyway, in my project I have a table of Zipcodes [sysdta_ZipCodes] and a table of State Codes [sysdta_FIPSStates].  I then have a table that relates them [sysctlZipFIPSState].

My table  [sysctlZipFIPSState] has the PKs from those two tables [ZipID] and [FIPSStateCode] set as primary keys.

I am trying to write code that looks at the recordset for the table [sysctlZipFIPSState] to see if that combo already exits before it tries to add it again and gives the user all that cryptic MS error that freaks them out.

    Dim lngZipID As Long, lngFIPSState As Long
    Dim rst1 As DAO.Recordset, lngCountID As Long
    
    lngZipID = Me.cboSelectZip.Column(0)
    lngFIPSState = Me.cboSelectState.Column(0)
    
    Set rst1 = CurrentDb.OpenRecordset("SELECT * FROM sysctl_ZipFIPSState WHERE [ZipID]=" & lngZipID & " AND [FIPSStateCode] =" & lngFIPSState)
    lngCountID = Nz(DCount("ZipID", "rst1"), 0)
    
    rst1.Close

Open in new window


This doesn't work as you'll know.  Am I using this wrong?
0
Comment
Question by:wlwebb
  • 2
3 Comments
 
LVL 11

Expert Comment

by:Goodangel Matope
ID: 39167582
I would suggest you use a stored procedure to update the record, you could then use this procedure from your VB code, and the procedure would do the checking. An example stored procedure is shown below:

CREATE PROCEDURE spAddZIPState
@ZipID INT,
@StateCode varchar(10)
AS

IF NOT EXISTS(SELECT * FROM sysctl_ZipFIPSState
                        WHERE [ZipID]= @ZipID AND [FIPSStateCode] = @StateCode)
INSERT INTO sysctl_ZipFIPSState [ZipID], [FIPSStateCode]
VALUES @ZipID, @StateCode


GO

Open in new window


All you need to do in the vb code is call the stored procedure spAddZIPState
0
 
LVL 11

Accepted Solution

by:
Goodangel Matope earned 500 total points
ID: 39167583
oops sorry left out brackets in the insert statement

CREATE PROCEDURE spAddZIPState
@ZipID INT,
@StateCode varchar(10)
AS

IF NOT EXISTS(SELECT * FROM sysctl_ZipFIPSState
                        WHERE [ZipID]= @ZipID AND [FIPSStateCode] = @StateCode)
INSERT INTO sysctl_ZipFIPSState ([ZipID], [FIPSStateCode])
VALUES (@ZipID, @StateCode)

Open in new window



GO
0
 

Author Closing Comment

by:wlwebb
ID: 39233319
Thank you
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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 …

757 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

20 Experts available now in Live!

Get 1:1 Help Now