我收到错误:
"对象不支持此属性或方法"
...当我尝试运行此代码时:
Dim db As DAO.Database
Dim mySQL As String
Set db = currentdb
mySQL = "SELECT tbl1.pID, tbl1.sID, tbl1.Type, tbl1.Description, tbl1.Amount, tbl1.Delete, tbl1.Approve, tbl2.sName FROM tbl2 INNER JOIN tbl1 ON tbl2.sID = tbl1.sID WHERE pID = " & Forms.frmEdit1.cboP & " And Delete = False;"
db.Execute mySQL, dbFailOnError
Set db = Nothing
似乎错误出现在最后一行(当我使用即时窗口时失败的地方)我也尝试了单引号' False'。
编辑:有效的Recordsource
SELECT tbl1.[pID],
tbl1.[sID],
[tbl1].Type,
[tbl1].Description,
[tbl1].Amount,
[tbl1].Delete,
tbl1.Approve,
tbl2.sName
FROM tbl2 INNER JOIN tbl1 ON tbl2.sID = tbl1.sID
WHERE pID = Forms![frmEdit1].cboP
And [Delete] = False;
答案 0 :(得分:0)
逻辑上,向Execute
发送SELECT语句就是说,'根据此条件选择值,但不要让我知道结果',这没有多大意义。填充未绑定的子表单需要手动完成 - 这是什么使它不受约束':
Sub LoadByID(Optional ByVal ID)
Dim RS As DAO.Recordset, SQL As String
If IsMissing(ID) Then ID = Forms!frmEdit1.cboP
SQL = " SELECT tbl1.sID, tbl1.Type, tbl1.Description, " + _
" tbl1.Amount, tbl1.Delete, tbl1.Approve, tbl2.sName " + _
" FROM tbl2 INNER JOIN tbl1 ON tbl2.sID = tbl1.sID " + _
" WHERE pID = " & ID & " And [Delete] = False;"
Set RS = CurrentDb.OpenRecordset(SQL)
' Assuming no match is an error; if it isn't, rewrite accordingly
If RS.EOF Then Err.Raise 999, "LoadByID", "Er, no selection..."
txtID.Value = RS!ID
txtDescription.Value = RS!Description
' and so on...
End Sub
我已经允许不通过组合框指定ID以使该方法更容易测试。另请注意,如果表单允许编辑,那么您将需要更多代码,然后保存任何更改的值。
修改强>
以上只假设一条回程记录。如果不止一个,循环如下:
Sub LoadByID(Optional ByVal ID)
Dim RS As DAO.Recordset, SQL As String
If IsMissing(ID) Then ID = Forms!frmEdit1.cboP
SQL = " SELECT tbl1.sID, tbl1.Type, tbl1.Description, " + _
" tbl1.Amount, tbl1.Delete, tbl1.Approve, tbl2.sName " + _
" FROM tbl2 INNER JOIN tbl1 ON tbl2.sID = tbl1.sID " + _
" WHERE pID = " & ID & " And [Delete] = False;"
Set RS = CurrentDb.OpenRecordset(SQL)
' add code to clear current display here...
While Not RS.EOF
' add code to load the individual values here...
RS.MoveNext
Wend
End Sub