Results 1 to 4 of 4

Thread: Sql Error

  1. #1
    New Member
    Join Date
    Nov 09
    Posts
    4

    Sql Error

    Hi,

    I'm trying to run a query and get the AutoIncrement ID Returned to me but i get this error "Too few parameters. Expected 3." On this line:

    Code:
    db.Execute ("INSERT INTO Loan([Copy ID], [Employee ID], [Loan Date], [Due Date]) VALUES (List2.Value, Combo4.Value, Date(), Calendar7.Value)")
    And this is my whole thing:
    Code:
    Private Sub AddLoan_Click()
    
     If List2.ListIndex = -1 Then
        MsgBox ("Please select a copy to loan out.")
     ElseIf Combo4.ListIndex = -1 Then
        MsgBox ("Please select the employee.")
     Else
        'Add the loan entry
        
        'Get Insert ID
        
                Dim db As DAO.Database
                Dim rs As DAO.Recordset
                
                Set db = DBEngine(0)(0)
                db.Execute ("INSERT INTO Loan([Copy ID], [Employee ID], [Loan Date], [Due Date]) VALUES (List2.Value, Combo4.Value, Date(), Calendar7.Value)")
                
                Set rs = db.OpenRecordset("SELECT @@IDENTITY AS LastID;")
                ShowIdentity = rs!LastID
                
            MsgBox (ShowIdentity)
            
                rs.Close
                
                
                Set rs = Nothing
                Set db = Nothing
        
        'Update the copy and set it to On Loan
        Dim updateString
        updateString = "UPDATE Copy SET [Availability]='On Loan' WHERE [Copy ID]=List2.Value"
    
     End If
     
    End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,564

    Re: Sql Error

    Welcome to VBForums

    The problem is that the items like List2.Value etc are items in your VB code - they do not exist inside the database tables.

    Rather than put the names of the controls into the SQL, you need to put in their values, and delimit them appropriately.

    For more information, see the following articles from our Database Development FAQs/Tutorials (at the top of the Database Development forum):

  3. #3
    New Member
    Join Date
    Nov 09
    Posts
    4

    Re: Sql Error

    Thank you very much!

    Do i also need to change the '[TabelNAme]' bits or are they valid?

    Thanks

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,564

    Re: Sql Error

    They are valid, because they refer to actual database items (tables and fields).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •