Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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 …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 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