2014-10-01 2 views
0

QueryDef를 사용하면 " 변수 또는 With 블록 변수가 설정되지 않음"오류가 발생합니다. strSQL의 출력을 새 쿼리에 복사하면 제대로 작동합니다. 이 오류에 대한 해결책을 도우십시오.MS Access VBA QueryDef - With 블록 변수가 설정되지 않았습니다. 오류

다음 줄을 실행할 때 오류가 발생합니다.

Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL) 

당신은 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" 

Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL) 

DoCmd.OpenQuery "qryST_ReclassifyAttribute" 

End Sub 

답변

2

아래의 전체 코드를 참조하십시오.

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 

당신이 그것을 설정하면. 그것은 정상적으로 작동해야합니다!

관련 문제