1

I am trying to insert a field 'SID' Value into tbl1, But to get the SID It must reference the forename and surname to another table (tbl2) to get the SID, I then have used the following SQL Statement and subsequent code.

    Dim sqlquery As String = "INSERT INTO tblPayments (StudentID,Payment,PaymentDate) VALUES (SELECT StudentID FROM tblStudents WHERE Forename = @Forename AND Surname = @Surname , Payment = @SPaid, PaymentDate = @todaysdate)"

    Dim sqlcommand As New OleDbCommand
    With sqlcommand

        .CommandText = sqlquery

        .Parameters.AddWithValue("@SPaid", Paidtxt.Text)
        .Parameters.AddWithValue("@todaysdate", Today.Date)
        .Parameters.AddWithValue("@Forename", Forenametxt.Text)
        .Parameters.AddWithValue("@Surname", Surnametxt.Text)

        .Connection = conn

        .ExecuteNonQuery()
    End With
    MsgBox("query executed, closing connection")
    conn.Close()

Yet, the SQLQuery is giving the error :

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: Syntax error in query expression 'SELECT StudentID FROM tblStudents WHERE Forename = @Forename AND Surname = @Surname'.

But I can not see what is wrong with the part of the statement that is specified as wrong, can someone tell me where its wrong please?

1 Answer 1

3

I think the error is very evident. The syntax for insert . . . select does not use the values keyword:

INSERT INTO tblPayments(StudentID, Payment, PaymentDate) 
    SELECT StudentID,  @SPaid, @todaysdate
    FROM tblStudents
    WHERE Forename = @Forename AND Surname = @Surname;
Sign up to request clarification or add additional context in comments.

1 Comment

OH Of course, I apologies for the stupidity I rarely use SQL. Thank you for the speed response!

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.