Script Examples vbscript autoit php batch

vbscript to read and write or update an Access mdb database


how to read and write an Access Database in vbscript includes an update function: if the Entry exists Records are updated if not a new entry is generated

read
reads an table of an Access Database you have to fill in the right Field names: rs.Fields("Spalte1")

Set conn = CreateObject("ADODB.Connection")
' Connect to the database
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.mdb"
conn.Open strConnect

StrSQL = "Select * from daten"
Set rs = conn.Execute(StrSQL)
Do While not rs.EOF
    wscript.echo "Spalte1:" & rs.Fields("Spalte1") & " Spalte2:"& rs.Fields("Spalte2")
    rs.MoveNext
Loop


Update or add new
the sub function updateornew searches Row1 for an existing Variable:
if exists the entry is updated if not it will make a new entry in the database
the function addnewentry always makes a new entry in the database


'''''''''''''''''''''''''''''''''''''''''''''''''''''
'Variables:
'''''''''''''''''''''''''''''''''''''''''''''''''''''
const databasepath = "c:\test.mdb"
const database = "daten"
const row1 = "Spalte1"

'''''''''''''''''''''''''''''''''''''''''''''''''''''
'Database Connection
'''''''''''''''''''''''''''''''''''''''''''''''''''''
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & databasepath
objConnection.Open strconn
'''''''''''''''''''''''''''''''''''''''''''''''''''''


'''''''''''''''''''''''''''''''''''''''''''''''''''''
'Main Program
'''''''''''''''''''''''''''''''''''''''''''''''''''''
'call addnewentry("Row1entry","Row2entry","Row3entry")
call updateornew("Row1 new or update eintry","Row2","Row3")
'''''''''''''''''''''''''''''''''''''''''''''''''''''


'''''''''''''''''''''''''''''''''''''''''''''''''''''
'close Database
'''''''''''''''''''''''''''''''''''''''''''''''''''''
objConnection.Close
'''''''''''''''''''''''''''''''''''''''''''''''''''''


'''''''''''''''''''''''''''''''''''''''''''''''''''''
'sub add new Entry into Database
'''''''''''''''''''''''''''''''''''''''''''''''''''''
sub addnewentry (var1,var2,var3)
objRecordset.Open "SELECT * FROM " & database , objConnection, _
     3, 3
   objRecordset.AddNew
     objRecordset(0) = var1
     objRecordset(1) = var2
objRecordset(2) = var3
     objRecordset.Update
objRecordset.Close
end sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''


''''''''''''''''''''''''''''''''''''''''''''''''''''
'sub find and update or make new
''''''''''''''''''''''''''''''''''''''''''''''''''''
sub updateornew (var1,var2,var3)
objRecordset.Open "SELECT * FROM " & database , objConnection, _
     3, 3
strSearchCriteria = row1 & " = '" & var1 & "'"
objRecordSet.Find strSearchCriteria
If objRecordset.EOF Then
    Wscript.Echo "Record " & var1 & " cannot be found in row " & row1 & " add new...."
   objRecordset.AddNew
     objRecordset(0) = var1
     objRecordset(1) = var2
objRecordset(2) = var3
Else
   Wscript.Echo "Record " & var1 & " found in row " & row1 & " updating...."
   objRecordset(1) = var2
   objRecordset(2) = var3
End If
     objRecordset.Update
objRecordset.close
end sub
''''''''''''''''''''''''''''''''''''''''''''''''''''
<<< vbscript to create access mdb database and tables vbscript um mehrere IP-Adressen bzw ein Subnet zu pingen >>>


neue Themen

Stichwortsuche auf dieser Seite:
04.07.2009 07:34 zZ 1 Leser online
Powered by cms.libe.net 2004-2009