Solved

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

Posted on 2013-05-15
3
436 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

821 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