Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Vbscript to prevent MS access duplicate entries

Posted on 2011-03-16
4
Medium Priority
?
1,050 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 1400 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 600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
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.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 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