vbscript lesen schreiben oder updaten einer Access Datei

 

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
''''''''''''''''''''''''''''''''''''''''''''''''''''
positive Bewertung({{pro_count}})
Beitrag bewerten:
{{percentage}} % positiv
negative Bewertung({{con_count}})

DANKE für deine Bewertung!

Fragen / Kommentare


(sortiert nach Bewertung / Datum) [alle Kommentare(neueste zuerst)]

✍anonym
04.02.2010 08:53
User: Oleg 
---------------------------
  VbsEdit
  ---------------------------
  Line:	24
  Column:	0
  Error:	Current Recordset does not support updating. This may be a limitation of the provider, or of the selected locktype.
  Code:	800A0CB3
  Source:	ADODB.Recordset
  
  System:	Initialization failed because of an invalid line in the
  
  configuration file %1. The invalid line is the data.
✍Bernhard
gepostet am 04.02.2010 08:53
welches Betriebssytem ? / Office Version ?   ist die Benutzerkontensteuerung aktiv?

Beitrag erstellt von Bernhard

✍anonym
15.01.2010 07:01
User: Fritz 
Adding new db throws an exeption instead of creating a new db
  Microsoft JET Database Engine: Datei 'c:\test.mdb' not found 
  
  (Windows XP SP3, WSH 5.6)

Durch die weitere Nutzung der Seite stimmst du der Verwendung von Cookies zu Mehr Details