Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-15
3
Medium Priority
?
471 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

972 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