Solved

Vbscript to prevent MS access duplicate entries

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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