database
  • vba
  • where
  • access
  • 2016-11-09 209 views 0 likes 
    0

    我得到錯誤幾個參數。預計1.此VBA代碼,我不知道如何正確進入它...訪問VBA - 哪裏條款

    Dim rs_invoice As DAO.Recordset 
    Set rs_invoice = CurrentDb.OpenRecordset("SELECT * FROM order_tbl WHERE invoice_no Is Null AND company_name='" _ 
    & Me.cmb_start_company.Column(1) & "' AND shiped=" & "True") 
    
    
    If Not (rs_invoice.EOF And rs_invoice.BOF) Then 
        rs_invoice.MoveFirst 
        Do Until rs_invoice.EOF = True 
         rs_invoice.Edit 
         rs_invoice!invoice_no = invoice_number 
         rs_invoice.Update 
    
         rs_invoice.MoveNext 
        Loop 
    Else 
        'No records 
    End If 
    
    'Finish 
    
    
    rs_invoice.Close 'Close the recordset 
    Set rs_invoice = Nothing 'Clean up 
    

    感謝

    +0

    未能進行你用'UPDATE order_tbl SET invoice_no =「&invoice_number&」WHERE invoice_no是Null AND company_name ='「&Me.cmb_start_company.Column(1)&」'AND shiped = True'行來做同樣的事情。 ?儘管我主張使用SQL參數而不是字符串連接。 –

    +0

    謝謝,但我真的不知道如何使用此代碼你可以發佈樣品嗎?謝謝 – Pecurka

    +0

    我對VBA不熟悉,但它看起來像[在ADO.NET中使用Command.Prepare的VBA相當於什麼](http://stackoverflow.com/a/1746897/1115360)可以幫助您。 –

    回答

    0

    我不是那麼熟悉接入/ DAO,但你串聯參數值補充到您的查詢。使用ADO命令文本會是這個樣子,而不是:

    Const sql As String = "SELECT * FROM order_tbl WHERE invoice_no Is Null AND company_name = ? AND shiped = True" 
    

    ?是一個參數值的佔位符;而不是直接致電OpenRecordset(sql),您需要製作一個Command並添加一個合適的Parameter

    似乎DAO處理這個有點不同,請參閱Parameterized queries in Access - 當然其中一個答案適用於此。因爲我不喜歡隨機即席查詢撞擊着我的數據庫,我可能會與QueryDefs辦法去this answer

    Dim qdf As Querydef 
    Dim rst As Recordset 
    
    'then we'll open up the query: 
    Set qdf = CurrentDB.QueryDefs(qryname) 
    
    'Now we'll assign values to the query using the parameters option: 
    qdf.Parameters(0) = qryStartDate 
    qdf.Parameters(1) = qryEndDate 
    
    'Now we'll convert the querydef to a recordset and run it 
    Set rst = qdf.OpenRecordset 
    

    這將是:

    Dim query As QueryDef 
    Set query = CurrentDB.QueryDefs("TheQuery") 
    
    query.Parameters(0) = Me.cmb_start_company.Column(1) 
    
    Dim result As Recordset 
    Set result = query.OpenRecordset 
    
    +0

    剛剛意識到,我不知道錯誤消息是如何說預計參數*,因爲它是一個即席查詢。儘管如此,一種不同的,更強大的方法不會傷害。 –

    +0

    我用新的查詢完成了它。感謝所有:) – Pecurka

    1

    只是猜測。你可以試試這個:

    "SELECT * FROM order_tbl WHERE (invoice_no Is Null) AND (company_name='" _ 
    & Me.cmb_start_company.Column(1) & "')AND (shiped=" & "True") 
    

    選項2:

    dim str_test as string 
    str_test =  "SELECT * FROM order_tbl WHERE (invoice_no Is Null) AND (company_name='" & Me.cmb_start_company.Column(1) & "')AND (shiped=" & "True") 
    
    debug.print str_test 
    

    你在即時窗口中得到什麼?

    選項3:

    "SELECT * FROM order_tbl WHERE (invoice_no Is Null) AND (company_name='" _ 
        & Me.cmb_start_company.Column(1) & "')AND (shiped=True)" 
    

    選項4: 然後用樣品這裏,它應該工作。 https://msdn.microsoft.com/en-us/library/bb243786(v=office.12).aspx也許吧。只要確保你設定記錄同樣的方式:

    Dim dbsNorthwind As DAO.Database 
    Dim rstProducts As DAO.Recordset 
    Dim strSQL As String 
    
    Set dbsNorthwind = CurrentDb 
    strSQL = "SELECT * FROM Products WHERE Discontinued = No " & _ 
         "ORDER BY ProductName" 
    Set rstProducts = dbsNorthwind.OpenRecordset(strSQL) 
    
    +0

    我現在已經測試和我有同樣的錯誤:/ – Pecurka

    +0

    @Pecurka - 見選項2 – Vityata

    +0

    我立即窗口 SELECT * FROM order_tbl WHERE(invoice_no IS NULL)得到這個AND(COMPANY_NAME =」 Bogdanic GmbH doo')AND(shiped = True) – Pecurka

    相關問題