创建 ActiveX 控件下拉列表时 VBA 中出现类型不匹配错误
Type mismatch Error in VBA when creating ActiveX control drop down list
我的 sheet1 中有 3 个 ActiveX 组合框。
我在 This Workbook
中使用了一些代码来填充第一个组合框列表。然后我创建了一些函数来获取下一组 cascading
值的组合框。下面是函数:
Function CascadeChild(TargetChild As OLEObject)
Dim Myconnection As Connection
Dim cmd As ADODB.Command
Dim Myrecordset As Recordset
Dim Myworkbook As String
Dim strSQL As String
Set Myconnection = New ADODB.Connection
Set cmd = New ADODB.Command
Set Myrecordset = New ADODB.Recordset
'Identify the workbook you are referencing
Myworkbook = Application.ThisWorkbook.FullName
'Open connection to the workbook
Myconnection.Open "--"
Select Case TargetChild.Name
Case Is = "Directorate"
strSQL = "Select Distinct Directorate AS [TgtField] from DBTable Where Division = '" & Sheet1.Division.Value & "' or 'All' = '" & Sheet1.Division.Value & "'"
Case Is = "Area"
strSQL = "Select Distinct Area AS [TgtField] from DBTable Where ( Division = '" & Sheet1.Division.Value & "' or 'All' = '" & Sheet1.Division.Value & "') AND (Directorate = '" & Sheet1.Directorate.Value & "' or 'All' = '" & Sheet1.Directorate.Value & "')"
End Select
'Load the Query into a Recordset
Myrecordset.Open strSQL, Myconnection, adOpenStatic
'Fill the target child listbox
With TargetChild.Object
.Clear
Do
.AddItem Myrecordset![TgtField]
Myrecordset.MoveNext
Loop Until Myrecordset.EOF
.Value = .List(0) '<<Automatically selects the first value in the ListBox
End With
'Clean up
Myconnection.Close
Set Myrecordset = Nothing
Set Myconnection = Nothing
End Function
然后我在VBA的Sheet1中写了一些代码:
Private Sub Division_Change()
Call CascadeChild(ActiveSheet.OLEObjects(Sheet1.Directorate.Name))
End Sub
Private Sub Directorate_Change()
Call CascadeChild(ActiveSheet.OLEObjects(Sheet1.Area.Name))
End Sub
第一个组合框给出值,然后当我 select 来自 ActiveX 控件的值时,错误 MSG 填充
Runtime Error, type mismatch
此处出现调试模式错误.AddItem Myrecordset![TgtField]
任何帮助
尝试
.AddItem Myrecordset.Fields(0).Value
我的 sheet1 中有 3 个 ActiveX 组合框。
我在 This Workbook
中使用了一些代码来填充第一个组合框列表。然后我创建了一些函数来获取下一组 cascading
值的组合框。下面是函数:
Function CascadeChild(TargetChild As OLEObject)
Dim Myconnection As Connection
Dim cmd As ADODB.Command
Dim Myrecordset As Recordset
Dim Myworkbook As String
Dim strSQL As String
Set Myconnection = New ADODB.Connection
Set cmd = New ADODB.Command
Set Myrecordset = New ADODB.Recordset
'Identify the workbook you are referencing
Myworkbook = Application.ThisWorkbook.FullName
'Open connection to the workbook
Myconnection.Open "--"
Select Case TargetChild.Name
Case Is = "Directorate"
strSQL = "Select Distinct Directorate AS [TgtField] from DBTable Where Division = '" & Sheet1.Division.Value & "' or 'All' = '" & Sheet1.Division.Value & "'"
Case Is = "Area"
strSQL = "Select Distinct Area AS [TgtField] from DBTable Where ( Division = '" & Sheet1.Division.Value & "' or 'All' = '" & Sheet1.Division.Value & "') AND (Directorate = '" & Sheet1.Directorate.Value & "' or 'All' = '" & Sheet1.Directorate.Value & "')"
End Select
'Load the Query into a Recordset
Myrecordset.Open strSQL, Myconnection, adOpenStatic
'Fill the target child listbox
With TargetChild.Object
.Clear
Do
.AddItem Myrecordset![TgtField]
Myrecordset.MoveNext
Loop Until Myrecordset.EOF
.Value = .List(0) '<<Automatically selects the first value in the ListBox
End With
'Clean up
Myconnection.Close
Set Myrecordset = Nothing
Set Myconnection = Nothing
End Function
然后我在VBA的Sheet1中写了一些代码:
Private Sub Division_Change()
Call CascadeChild(ActiveSheet.OLEObjects(Sheet1.Directorate.Name))
End Sub
Private Sub Directorate_Change()
Call CascadeChild(ActiveSheet.OLEObjects(Sheet1.Area.Name))
End Sub
第一个组合框给出值,然后当我 select 来自 ActiveX 控件的值时,错误 MSG 填充
Runtime Error, type mismatch
此处出现调试模式错误.AddItem Myrecordset![TgtField]
任何帮助
尝试
.AddItem Myrecordset.Fields(0).Value