Solved

Vbscript to prevent MS access duplicate entries

Posted on 2011-03-16
4
1,016 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

911 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

21 Experts available now in Live!

Get 1:1 Help Now