나는이 질문이 2 년 된 것을 알고 있지만, 그것에 비틀 거려서 Access Subform 컨트롤을위한 SQL 쿼리를 사용하고자하는 사람들을 위해 (많은 SQL 쿼리/저장 프로 시저가 양식 레코드 원본) ... 나는 ADO가 잘 작동하는 것을 발견했습니다. 좋은 설명은 http://support.microsoft.com/kb/281998/EN-US/을 참조하십시오. 하위 폼에서이 작업을 수행하려면 하위 폼의 "열린 이벤트"이벤트에 코드를 삽입합니다. 폼의 이벤트는 아닙니다.
하나의 메모는 내가 믿는 새 레코드를 추가하기 위해 다른 코드를 사용해야하므로 하위 폼이 기본 폼에 바인딩되지 않습니다 (부모/자식이 손실 됨). 내 특정 하위 폼은 읽기 전용이므로 부모/자식 일 필요는 없습니다. 저장 프로 시저를 호출하여 부모/자식 효과를 다시 만들 때 매개 변수로 기본 폼의 레코드 ID를 사용합니다 (아래 예제의 코드 참조).).
또한 LockType adLockPessimistic 또는 adLockOptimisic을 지정하지 않으면이 코드가 작동하지 않습니다. adLockReadOnly가 작동하지 않습니다 (In Memory, Stand-Alone, Disconnected ADO Recordset 참조). 약간의 두통을 일으킬 수있는 "개체가 유효한 Recordset 속성이 아닙니다"오류가 발생합니다. :)
내 예제 코드 (I는 https://accessexperts.com/blog/2011/01/21/easy-adodb-recordsets-and-commands-in-access/에서 후안 소토의 보편적 인 ADO 연결 예제를 사용) :
Private Sub Form_Open(Cancel As Integer)
On Error GoTo ErrHandler
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim strPoint As String
'I use my Point_ID from my main form as a parameter to fill the subform to recreate the "parent/child" effect
strPoint = Forms!FRM_Vegetation_Strata!Point_ID
strSQL = "sp_Report_VegWorksheet '" & strPoint & "'"
'this method of calling the sub "OpenMyRecordset" is from Soto's example listed above
OpenMyRecordset rs, strSQL, rrOpenStatic, rrLockOptimistic, True
With rs
If .RecordCount = 0 Then
MsgBox "No records returned"
End If
End With
Set Me.Recordset = rs
ExitProcedure:
On Error Resume Next
Exit Sub
ErrHandler:
MsgBox Err.Description & " (" & Err.Number & ") encountered", vbOKOnly + vbCritical, "InitConnect"
Resume ExitProcedure
Resume
End Sub
하위 폼을 새로 고침 할 때 기본 양식 변경의 데이터 :
Private Sub cmdRefresh_Click()
Me.Form.Recordset.Requery
Set Me.Form.Recordset = Me.Form.Recordset
End Sub
그리고 경우 (이벤트 "닫기의"하위의) 형태 폐쇄
Private Sub Form_Unload(Cancel As Integer)
'Close the ADO connection we opened
On Error Resume Next
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = Me.Recordset.ActiveConnection
If cn.State = adStateOpen Then
cn.Close
End If
Set cn = Nothing
Set rs = Nothing
Set Me.Recordset = Nothing
End Sub