2015-05-03 247 views
0

我得到這個錯誤在Microsoft Access:運行時錯誤424 ..對象所需

運行時錯誤424對象所需

而下面是我在後端使用VBA代碼

VBA代碼

Private Sub Command87_Click() 
If Me.phase <> "" Then 
    Me.lst_caseItems.RowSource = " " & _ 
     "SELECT tbl_master.fld_masterID, " & _ 
      "tbl_master.fld_masterActionNo AS ActionNumber, " & _ 
      "tbl_master.fld_masterStudyActionNo AS StudyActionNo, tbl_master.fld_masterIssuedTo," & _ 
      "tbl_master.fld_masterRev AS Rev, tbl_phases.fld_phase AS Phase, tbl_studies.fld_study AS Study, " & _ 
     "FROM ((tbl_master LEFT JOIN tbl_studies ON tbl_master.fld_studyID = ' " & tbl_studies.fld_studyID & " ') LEFT JOIN tbl_phases ON tbl_master.fld_phaseID = ' " & tbl_phases.fld_phaseID & " ') LEFT JOIN tbl_locations ON tbl_master.fld_locationID = ' " & tbl_locations.fld_locationID & " ';" 
End If 
End Sub 
+1

你肯定'phase'變量和'lst_caseItems'are在'Command87_Click(的情況下可用)'?使用調試器斷點來查看執行代碼時可用的內容。 P.S .: _backend_和_codebehind_是不同的術語,在這種情況下它是代碼隱藏的。 P.P.S .: 87按鈕? 0_o –

回答

0

在你當前的代碼,V:一個按鈕BA認爲tbl_studies.fld_studyID是一個變量,因此要求一個對象。

使用您當前的代碼;

' " & tbl_studies.fld_studyID & " ' 

"關閉SQL語句,那麼你的符號tbl_studies或等告訴VBA在變量tbl_studies.fld_studyID的附加價值,然後與符號後面"重新打開VBA內的SQL語句(等編譯所有的流量爲必要)。由於您沒有名爲studyID的變量,因此它看起來像是您所需對象的原因。

要解決這個問題,您可能需要一個變量分配給您想要的ID(如dim X as long然後x = studyIDyouwanthere)或正確地寫你的查詢,使其鏈接到研究表字段studyID。仔細看看它,看起來你只需要在VBA窗口中正確地編寫查詢,以便它在語法上是正確的(在VBA的解釋中),請嘗試以下內容;

Private Sub Command87_Click() 
If Me.phase <> "" Then 
    Me.lst_caseItems.RowSource = " " & _ 
     "SELECT tbl_master.fld_masterID, " & _ 
      "tbl_master.fld_masterActionNo AS ActionNumber, " & _ 
      "tbl_master.fld_masterStudyActionNo AS StudyActionNo, tbl_master.fld_masterIssuedTo," & _ 
      "tbl_master.fld_masterRev AS Rev, tbl_phases.fld_phase AS Phase, tbl_studies.fld_study AS Study, " & _ 
     "FROM ((tbl_master LEFT JOIN tbl_studies ON tbl_master.fld_studyID = tbl_studies.fld_studyID) LEFT JOIN tbl_phases ON tbl_master.fld_phaseID = tbl_phases.fld_phaseID) LEFT JOIN tbl_locations ON tbl_master.fld_locationID = tbl_locations.fld_locationID;" 
End If 
End Sub 

當您在查詢中加入VBA你不加入基於變量的值,你只能使用他們爲您select部分或having/where/group BY不上加入。

+0

嘿標記...使用你的代碼沒有錯誤,但當我運行代碼時,我沒有得到任何值傳遞給me.lst_CaseItems.RowSource ...請看到,這是發生在按鈕的單擊事件,所以是vba sql允許按鈕點擊訪問 – Zeeshan

+0

我認爲它關於access-vba,'tbl_studies.fld_studyID'是'tbl_studies'表中的一個字段;)。 –

+0

你可能需要添加一個me.lst_caseItems.refresh後設置rowsource Zeeshan –

相關問題