Solved

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

Posted on 2013-05-15
3
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

615 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