Solved

Vbscript to prevent MS access duplicate entries

Posted on 2011-03-16
4
1,012 Views
Last Modified: 2012-05-11
Hello,

I am trying to write a .vbs script that will add records to an access database. I can do that part alright however, I want to prevent a duplicates from being added.

If, in Access, i just don't allow duplicates, the vbscript will kick back an error about duplicates however I want the script to check if there is a duplicate and silently dismiss the error and stop processing. If there are no duplicates for that field in the database I want the script to add the fields.

My code to add the records is below. I want the script to check the database for the order number value being duplicate and if so just have msgbox say record already added or a record already added "overwrite existing record"

Many thanks
const adOpenStatic = 3
 const adLockOptimistic = 3
dim strcon
strcon ="DSN=PickPack"
Set objConnection = CreateObject ("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
dbasename = "M:\Mas200Customizations\PickPackDatabase\PickPack.mdb"
tblename = "PickPack"
objConnection.Open strcon

objRecordSet.Open "SELECT * FROM " & tblename , objConnection, adOpenStatic, adLockOptimistic

objRecordSet.AddNew()
objRecordSet.Fields.Item("OrderNumber").Value = SO_Shipping_bus_InvoiceNo
objRecordSet.Fields.Item("Picker").Value = SO_Shipping_bus_UDF_Picker
objRecordSet.Fields.Item("Packer").Value = SO_Shipping_bus_UDF_Shipper
objRecordSet.Fields.Item("Shipper").Value = SO_Shipping_bus_ShipperID
objRecordSet.Update()
objRecordSet.Close()
objConnection.Close()

Msgbox "Shipper,Picker, and Packer Successfully Added To Database!"

Open in new window

0
Comment
Question by:badgermike
  • 2
4 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 350 total points
ID: 35150787
try this



const adOpenStatic = 3
 const adLockOptimistic = 3
dim strcon
strcon ="DSN=PickPack"
Set objConnection = CreateObject ("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
'add this
Set objChkRecordSet = CreateObject("ADODB.Recordset")
dbasename = "M:\Mas200Customizations\PickPackDatabase\PickPack.mdb"
tblename = "PickPack"
objConnection.Open strcon

objRecordSet.Open "SELECT * FROM " & tblename , objConnection, adOpenStatic, adLockOptimistic
'add this
objChkRecordSet.open "select * from " & tblname & " where OrderNumber='" & SO_Shipping_bus_InvoiceNo & "'", objConnection

if not objChkRecordSet.eof then
 if (Msgbox ("Record already added or a record already added ""overwrite existing record""!",vbyesno))= vbno then
objChkRecordSet.Close()
exit sub
end if

objRecordSet.AddNew()
objRecordSet.Fields.Item("OrderNumber").Value = SO_Shipping_bus_InvoiceNo
objRecordSet.Fields.Item("Picker").Value = SO_Shipping_bus_UDF_Picker
objRecordSet.Fields.Item("Packer").Value = SO_Shipping_bus_UDF_Shipper
objRecordSet.Fields.Item("Shipper").Value = SO_Shipping_bus_ShipperID
objRecordSet.Update()
objRecordSet.Close()
objConnection.Close()

Msgbox "Shipper,Picker, and Packer Successfully Added To Database!"

Open in new window

0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 35150955
The problem with that aproach is that IF it is a multi user application and more than one person tries to post the same entry for any reason at the same time, it "Could" be allowed. You have no transaction locking to stop it. You could do your check, person B do their check and then you create a record and then person B creates a record.

You would be far better off using the databases own check methids and have a unique key and do correct error checking. It is in proncipal no different but it is a lot more accurate.
0
 
LVL 37

Assisted Solution

by:Neil Russell
Neil Russell earned 150 total points
ID: 35150978
Also to speed things up use..

objRecordSet.Open "SELECT * FROM " & tblename & " WHERE 1=0", objConnection, adOpenStatic, adLockOptimistic

instead of

objRecordSet.Open "SELECT * FROM " & tblename , objConnection, adOpenStatic, adLockOptimistic

This will return an empty recordset and as you are adding and not using any of the selected data you dont want 50,000 records returned in the query.
0
 
LVL 15

Author Closing Comment

by:badgermike
ID: 35155844
Thank you both!

I think this will work perfectly as this script is linked to a custom button in our ERP software on our shipping module in which only one person uses.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

744 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

11 Experts available now in Live!

Get 1:1 Help Now