0

I have encounter a problem where it tell me I have an Invalid SQL statement; expected 'DELETE','INSERT','PROCEDURE', 'SELECT', or 'UPDATE'.

I am trying to insert data that use foreign key.

Table pemain contain ID MABOPA PKBM MBA MBEIA PPPBBM MAPIM

Table Ahli contain ID company_name name address poscode state email phone fax company_reg website remarks pemain

where field pemain in table Ahli is the foreign key of table pemain

the code im using are

    Dim Pos As Integer
    Dim Pemain As Integer

    Int32.TryParse(TxtBoxPoscode.Text, Pos)
    Int32.TryParse(TxtBoxPemainId.Text, Pemain)

    Access.AddParam("@MABOPA", TextBox1.Text)
    Access.AddParam("@PKBM", TextBox2.Text)
    Access.AddParam("@MBA", TextBox3.Text)
    Access.AddParam("@MBEIA", TextBox4.Text)
    Access.AddParam("@PPPBBM", TextBox5.Text)
    Access.AddParam("@MAPIM", TextBox6.Text)

    Access.AddParam("@companyname", TxtBoxComName.Text)
    Access.AddParam("@name", TxtBoxName.Text)
    Access.AddParam("@address", TxtBoxAdd.Text)
    Access.AddParam("@poscode", Pos)
    Access.AddParam("@state", CboBoxState.Text)
    Access.AddParam("@email", TxtBoxEmail.Text)
    Access.AddParam("@phone", TxtBoxPhone.Text)
    Access.AddParam("@fax", TxtBoxFax.Text)
    Access.AddParam("@companyreg", TxtBoxComName.Text)
    Access.AddParam("@web", TxtBoxWebsite.Text)
    Access.AddParam("@remarks", TxtBoxRemarks.Text)
    Access.AddParam("@pemain", Pemain)

    'Execute Insert Command

                  Access.ExecQuery(
                 "START TRANSACTION;" & _
                 "INSERT INTO pemain (MABOPA, PKBM, MBA, MBEIA, PPPBBM, MAPIM);" & _
                 "VALUES (@MABOPA, @PKBM, @MBA, @MBEIA, @PPPBBM, @MAPIM);" & _
                 "DECLARE @NewID INT;" & _
                 "SELECT @NewID = SCOPE_IDENTITY();" & _
                 "INSERT INTO Ahli (company_name, name, address, poscode, state, email, phone, fax, company_reg, website, remarks, pemain);" & _
                 "VALUES (@companyname, @name, @address, @poscode, @state, @email, @phone, @fax, @companyreg, @web, @remarks, @NewID);" & _
                 "COMMIT;")

It would be a really great if anyone could help me.

1
  • 1
    Your code is T-SQL and is far away from what Access SQL understands. You will have to rewrite this completely using Access SQL. Commented Mar 7, 2017 at 8:08

1 Answer 1

2

Neither the Jet nor ACE OLE DB providers support multiple SQL statements per command. If you want to execute multiple SQL statements then you must execute multiple commands (or the same command more than once with different SQL statements) and if you want to wrap them in a transaction then you call BeginTransaction on your OleDbConnection to create an OleDbTransaction.

By the way, I'm not sure that SCOPE_IDENTITY exists in Access. I could be wrong but I think that that's specific to SQL Server. I think Access requires using @@IDENTITY.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, Access uses @@IDENTITY. The whole query needs writing to be honest, Access will quake in dear trying to work out what to do with that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.