MS Access VBA QueryDef - 使用Block变量未设置错误

时间:2014-10-01 10:06:04

标签: vba ms-access-2010

使用QueryDef时,我收到以下错误“对象 变量或没有设置块变量“。当我将strSQL的输出复制到一个新的查询时,它工作正常。请协助解决此错误。

运行以下行时发生错误;

Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL)

请参阅下面的完整代码

Private Sub ComboReclassify_AfterUpdate()

Dim dbs As Database
Dim strSQL As String
Dim strQueryName As String
Dim qryDef As QueryDef

strQueryName = "qryST_ReclassifyAttribute"

Dim attr As String
Dim ValueID As Integer
attr = [Forms]![frm_tblST_AttributesReclassification]![ComboItemAttributes]
ValueID = [Forms]![frm_tblST_AttributesReclassification]![ComboReclassify]

strSQL = "UPDATE dbo_tblST_DepartmentsAttributes SET  " & (attr) & " = " & ValueID & " WHERE dbo_tblST_DepartmentsAttributes.id = 1"

Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL)

DoCmd.OpenQuery "qryST_ReclassifyAttribute"

End Sub

1 个答案:

答案 0 :(得分:2)

您似乎错过了设置dbs对象。

Private Sub ComboReclassify_AfterUpdate()
    Dim dbs As Database
    Dim strSQL As String
    Dim strQueryName As String
    Dim qryDef As QueryDef

    strQueryName = "qryST_ReclassifyAttribute"

    Dim attr As String
    Dim ValueID As Integer
    attr = [Forms]![frm_tblST_AttributesReclassification]![ComboItemAttributes]
    ValueID = [Forms]![frm_tblST_AttributesReclassification]![ComboReclassify]

    strSQL = "UPDATE dbo_tblST_DepartmentsAttributes SET  " & (attr) & " = " & ValueID & " WHERE dbo_tblST_DepartmentsAttributes.id = 1"

    'You have not set the dbs object. That is the problem
    Set dbs = CurrentDB
    Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL)

    DoCmd.OpenQuery "qryST_ReclassifyAttribute"
End Sub

设置后。它应该正常工作!