简体   繁体   中英

Syntax error (missing operator) in query expression - VBA

This is my code:

    M_BKID = DMax("BK_ID", "BookingMain")
    FSQL = " UPDATE Q_HrsToBeRefund_Writeable " & _
           " SET BD_ToBeRefund = False, BD_Refunded = True, BD_RefundedRef = " & M_BKID & " " & _
           " ORDER BY BD_Dt DESC LIMIT " & M_Refunded & " "
           
    Debug.Print FSQL            ' ********************************
    DoCmd.RunSQL FSQL

I get an error

Syntax error (missing operator) in query expression '184 ORDER BY BD_Dt DESC Limit 3

Can anyone help me solve this?

MS Access definitely doesn't support LIMIT in UPDATE . I don't think it supports TOP either. But you can do:

UPDATE Q_HrsToBeRefund_Writeable as 
    SET BD_ToBeRefund = False,
        BD_Refunded = True,
        BD_RefundedRef = ?
    WHERE <primary key> IN (SELECT TOP (M_Refunded) <primary key>
                            FROM Q_HrsToBeRefund_Writeable
                            ORDER BY BD_Dt DESC
                           );

Note:

  • This assumes your table has a primary key.
  • Note the use of ? . That is an indication that you should use a parameter for this value, rather than munging the query string.
  • I'm not sure if the number of rows can be passed in as a parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM