Solved

Vbscript to prevent MS access duplicate entries

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

Accepted Solution

by:
Rey Obrero (Capricorn1) 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

777 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