2012-06-11 35 views
1

我寫了下面的代碼綁定將DataCombo添加一個空白項到DataCombo中在VB6

With dtcType 
     Set .RowSource = recType 
      .ListField = "Type" 
      .BoundColumn = "TypeId" 
End With 

我想在DataCombo中的頂部添加空白項(後綁定DataCombo中 - 如此用戶也可以選擇一個空白項目)。我怎樣才能做到這一點?

回答

1

據我記得沒有直接的方法來添加項目做DataCombo。 你可以考慮兩種選擇。

如果您的SQL看起來更不那麼像這樣的:

SELECT Id, Code FROM my_table 

可以使用SELECT添加artifictial記錄不FROM子句UNION

SELECT -1 As Id, '' As Code 
UNION ALL 
SELECT Id, Code FROM my_table 

結合第二個選項是與Combobox和替換DataCombo使用這樣的方法填充物品:

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