Results 1 to 7 of 7
  1. #1
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501

    Update query in VB

    I am trying to run the following code from a button in a continuous form. The button is inline with the record

    Private Sub AssignInvoice_Click()
    Dim dbs As Database
    Dim qdf As QueryDef


    Set dbs = CurrentDb
    dbs.Execute "Update tbl_ImportedRepairs " _
    & "SET tbl_ImportedRepairs.Invoice = 'me.[UpdateInvoice]'" _
    & "WHERE tbl_ImportedRepairs.Batch = Me.Batch AND PaymentRespID=me.PaymentRespId;"

    dbs.Close
    End Sub





    I get the error Run-Time error '3061' Too few parameters. Expected 2

    I am trying to run an update query to update any item in the database that matches the Batch and PaymentRespId in the current record to be the value of UpdateInvoice which is a unbound text box in the row.

    The Invoice Field is text field and the Batch and PaymentRespId fields are number

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Maybe you can store your SQL statement in a string variable to help you troubleshoot.

    Dim strSQL as string

    strSQL = "Update tbl_ImportedRepairs " _
    & "SET tbl_ImportedRepairs.Invoice = 'me.[UpdateInvoice]'" _
    & "WHERE tbl_ImportedRepairs.Batch = Me.Batch AND PaymentRespID=me.PaymentRespId;"
    debug.print strSQL


    Then you can look at your string in the Immediate Window Ctrl+G

    The problem you are having is that you are including Controls/Variables within your string, literally. You need to concatenate them. So you are missing a couple ampersands and maybe a quotation here and there.

  3. #3
    rivereridanus is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Posts
    71
    I'm not sure that that is going to give you what you want. I would typically do something like:

    Dim newinvoice As String, batch As String, payid As String
    newinvoice = Me.UpdateInvoice
    batch = Me.batch
    payid = Me.payid
    DoCmd.RunSQL("UPDATE tbl_ImportedRepairs SET tbl_ImportedRepairs.Invoice = '" + newinvoice + "' WHERE tbl_ImportedRepairs.Batch = " + batch + " AND tbl_ImportedRepairs.PaymentRespID = " + payid)

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I agree with itsMe. You can see what Access renders from your strSQL with the Debug.Print strSQL
    That way you can check your SQL syntax before actually running the query.

  5. #5
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    When i run this one

    Dim NewInvoice As String, TrinityBatch As String, payid As String
    NewInvoice = Me.NewInvoice
    batch = Me.TrinityBatch
    payid = Me.PaymentRespID

    DoCmd.RunSQL ("UPDATE tbl_ImportedRepairs SET tbl_ImportedRepairs.TrinityInvoice = '" + NewInvoice + "' WHERE tbl_ImportedRepairs.TrinityBatch = " + batch + " AND tbl_ImportedRepairs.PaymentRespID = " + payid)

    I get Run-time error '13" Type Mismatch and the debug goes to line

    DoCmd.RunSQL ("UPDATE tbl_ImportedRepairs SET tbl_ImportedRepairs.TrinityInvoice = '" + NewInvoice + "' WHERE tbl_ImportedRepairs.TrinityBatch = " + batch + " AND tbl_ImportedRepairs.PaymentRespID = " + payid)

    I tried manually assigning the invoice
    NewInvoice = 1500 or NewInvoice = "IN1500" but neither worked

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I am not at a computer right now but...

    Try concatenating with the ampersand rather than the plus sign. It looks like batch is a number type and SQL may be trying to do some math. Might have some issues with placement of your quotations too.

    Employ the debug.print technique previously suggested.

  7. #7
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    cool looks like the ampersand did the trick it is working now.
    Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-27-2014, 09:31 AM
  2. Replies: 7
    Last Post: 08-26-2013, 06:05 AM
  3. Replies: 1
    Last Post: 05-17-2013, 12:53 PM
  4. Replies: 2
    Last Post: 08-30-2012, 07:59 AM
  5. Replies: 4
    Last Post: 11-10-2011, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums