質問

I have a record set created mannually as follows

Dim rs As ADODB.Recordset rs.Open Dim Fields() As String

Fields(0) = ""
Fields(1) = "January"
Fields(2) = "February"
Fields(3) = "March"
Fields(4) = "April"
Fields(5) = "May"
rs.AddNew Fields
rs.Close

I've been trying to bind it to my combobox as follows

    combo1.RowSource = rs
    combo1.BoundColumn = "Fields"
    Set rs = Nothing

I see compiliation error at: combo1.RowSource = rs

Please help me in binding this recordset to my combo box. Thanks in advance

役に立ちましたか?

解決

I assume you are playing with a DataCombo based on the odd set of things mentioned in your question above.

You're a bit off with your attempt to create a fabricated Recordset. You also need a "destination" for selected or entered data so you need a DataSource and DataField.

While many fear and loathe VB6 data binding, there really isn't much to it. It helps to have taken one of the formal VB6 courses covering the topic but sadly those haven't been offered for quite a long time. The textbooks can still be had, though these days it seems to be a lot to expect anyone to do any actual study.

Here I have several Command buttons, a DataCombo, and a multiline TextBox:

Option Explicit

Private rsValues As ADODB.Recordset
Private rsData As ADODB.Recordset

Private Sub NewEnabled(ByVal Enable As Boolean)
    DataCombo1.Enabled = Enable
    cmdSave.Enabled = Enable
    cmdCancel.Enabled = Enable
    cmdNew.Enabled = Not Enable
    cmdDump.Enabled = Not Enable
End Sub

Private Sub cmdCancel_Click()
    rsData.CancelUpdate
    NewEnabled False
End Sub

Private Sub cmdDump_Click()
    With rsData
        Text1.Text = vbNullString
        Text1.SelText = "Records: " & CStr(.RecordCount) & vbNewLine
        If .RecordCount > 0 Then
            .MoveFirst
            Do Until .EOF
                Text1.SelText = CStr(.AbsolutePosition) _
                              & ": " & CStr(!Month.Value) & vbNewLine
                .MoveNext
            Loop
        End If
    End With
End Sub

Private Sub cmdNew_Click()
    rsData.AddNew
    rsData!Month.Value = vbNullString
    NewEnabled True
End Sub

Private Sub cmdSave_Click()
    rsData.Update
    NewEnabled False
End Sub

Private Sub Form_Load()
    Dim Month As Integer

    Set rsValues = New ADODB.Recordset
    With rsValues
        .CursorLocation = adUseClient
        .Fields.Append "MonthName", adVarWChar, 255
        .Open
        .AddNew Array(0), Array(vbNullString)
        For Month = 1 To 12
            .AddNew Array(0), Array(MonthName(Month))
        Next
    End With
    Set rsData = New ADODB.Recordset
    With rsData
        .CursorLocation = adUseClient
        .Fields.Append "Month", adVarWChar, 255
        .Open
    End With
    With DataCombo1
        .ListField = "MonthName"
        Set .RowSource = rsValues
        .DataField = "Month"
        Set .DataSource = rsData
    End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
    rsData.Close
    rsValues.Close
End Sub

This works just fine. However it may not be what you were after at all. That is really hard to tell from some code in a vacuum, especially when it appears to be air code that isn't close to correct.

他のヒント

Before you go any further with this, take a look at this. It's pretty simple to bind the control by hand, and avoids all sorts of subtle headaches that you will encounter in VB6 data binding. I have used data binding only once in the many thousands of lines of vb6 code I have delivered to customers, and that was to work around a very esoteric bug in Microsoft's DataRepeater control.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top