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 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