我编写了以下代码来绑定datacombo
With dtcType
Set .RowSource = recType
.ListField = "Type"
.BoundColumn = "TypeId"
End With
我想在DataCombo顶部添加一个空白项(绑定datacombo后 - 这样用户也可以选择一个空白项)。我怎么能这样做?
答案 0 :(得分:1)
据我记得,没有直接的方法来添加项目DataCombo
。
您可以考虑两种选择。
如果您的SQL看起来更像这样:
SELECT Id, Code FROM my_table
您可以使用SELECT
添加人工记录,而FROM
子句与UNION
SELECT -1 As Id, '' As Code
UNION ALL
SELECT Id, Code FROM my_table
第二个选项是将DataCombo
替换为Combobox
,并使用以下方法填充项目:
Public Sub Populate(ByRef pData As ADODB.Recordset, _
ByVal pIdField As String, _
ByVal pDataField As String, _
Optional pEmptyItem As Boolean = False, _
Optional pEmptyItemId As Long = -2, _
Optional pEmptyItemCaption As String = "")
If m_ComboboxControl Is Nothing Then Exit Sub
If pEmptyItem Then
m_ComboboxControl.AddItem pEmptyItemCaption
m_ComboboxControl.ItemData(m_ComboboxControl.NewIndex) = pEmptyItemId
End If
With pData
If Not (.BOF And .EOF) Then
Do While Not .EOF
Select Case TypeName(pData.Fields(pDataField).Value)
Case "Date"
m_ComboboxControl.AddItem Format(pData.Fields(pDataField), "DD-MM-YYYY")
Case Else
m_ComboboxControl.AddItem pData.Fields(pDataField)
End Select
m_ComboboxControl.ItemData(m_ComboboxControl.NewIndex) = CLng(pData.Fields(pIdField))
.MoveNext
Loop
End If
End With
End Sub