Write to Access 2000

Minneslucka

New member
Joined
Jun 30, 2003
Messages
2
Location
Sweden
I can open and read from my database, but I cant write to it, what could be wrong with this code:

Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "/Allt I Allo.mdb;User ID=Admin;Mode=Share Deny None")
myConnection.Open()
Dim MyCommand As New OleDbCommand("INSERT INTO test (hej1, hej2) VALUES (hej3, hej4)", MyConnection)
MyCommand.CommandText = "SELECT @@IDENTITY"
MsgBox("Last ID was : " & MyCommand.ExecuteScalar())
MyConnection.Close()
MyCommand.Dispose()

Greetings /Ale
 
this line MyCommand.CommandText = "SELECT @@IDENTITY"
is replacing the rprevious Insert line, remove it and it should work, however I dont think that you can use @@IDENTITY in Access.
You can do another Select to get TOP 1 from the table with Order By IDfield DESC.
 
@@IDENTITY Works fine in access2000+

Code:
Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "/Allt I Allo.mdb;User ID=Admin;Mode=Share Deny None") 
myConnection.Open() 
Dim MyCommand As New OleDbCommand("INSERT INTO test (hej1, hej2) VALUES (hej3, hej4)", MyConnection) 
 This is your missing command your need to run the INSERT Query before getting your ID
MyCommand.ExecuteNonQuery()
 Get the Last INSERT ID
MyCommand.CommandText = "SELECT @@IDENTITY" 
MsgBox("Last ID was : " & MyCommand.ExecuteScalar()) 
MyConnection.Close() 
MyCommand.Dispose()

Andy
 
Back
Top