Minneslucka Posted June 30, 2003 Posted June 30, 2003 I can open and read from my database, but I can't 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 Quote
Moderators Robby Posted June 30, 2003 Moderators Posted June 30, 2003 this line MyCommand.CommandText = "SELECT @@IDENTITY" is replacing the rprevious Insert line, remove it and it should work, however I don't 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. Quote Visit...Bassic Software
Minneslucka Posted June 30, 2003 Author Posted June 30, 2003 I think it's INPUT INTO that's wrong... The code doesn't report any error, but my database don't update either... Quote
a_jam_sandwich Posted July 1, 2003 Posted July 1, 2003 @@IDENTITY Works fine in access2000+ 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 Quote Code today gone tomorrow!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.