Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem adding record to access database

Status
Not open for further replies.

hskr4evr

Technical User
Sep 17, 2002
35
US
Hello. I have a vbscript that adds records to a table in an access database.

The first run, of the vbscript, works just fine.

When I do a second run, of the vbscript, I get an error message stating "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.".

That makes sense.

What I am confused with is how do I "update" the record if the record already exists in the table?

Below is my script.

Thank you.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim intCounter, objComputer, objDomain, objFSO, objFileOutput

Const DOMAIN_NAME = "NE-ARNG"
Const FILE_OUTPUT_NAME = "Computers.txt"
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFileOutput = objFSO.CreateTextFIle(FILE_OUTPUT_NAME)
'objFileOutput.WriteLine "Computer Name"
Set objDomain = GetObject("WinNT://" & DOMAIN_NAME)

objDomain.Filter = Array("Computer")

'connect to DB
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
objConnection.Open "DSN=NetInventory.dsn;" ' place your DSN here
objRecordset.CursorLocation = adUseClient
objRecordset.Open "SELECT * FROM ComputerNames" , objConnection, _
adOpenStatic, adLockOptimistic

For Each objComputer In objDomain
intCounter = intCounter + 1
objFileOutput.WriteLine "\\" & objComputer.Name
' add new record
objRecordset.AddNew
' if you have in YourDomainTable column named ComputerName
objRecordset("ComputerName") = objComputer.Name
objRecordset.Update
Next

objRecordset.Close
objConnection.Close

'done
 
the SQL to update a record goes like this (for example):
Code:
"UPDATE tablename SET field1='John', field2='Smith', field3='UK' WHERE ID=100;"
Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top