麻烦从sql表填充组合框

时间:2015-02-25 14:20:19

标签: vb6

需要一些小的帮助。我有一个问题是因为我的组合框不会填满我所引用的表格。这是我的代码:

Public Function GetCodeDesc(sCode As String) As String
    Dim strSQL As String, strAnswer As String
    Dim objGetInfo As New ADODB.Recordset

    strSQL = "SET ANSI_NULLS OFF"
    Set objGetInfo = New ADODB.Recordset
    objGetInfo.Open strSQL, objConnection, adOpenForwardOnly, adLockOptimistic

    strSQL = "SELECT DISTINCT * FROM SH_RAWOCODES WHERE CODEX='" & sCode   & "'       ORDER BY CODEX"
    Set objGetInfo = New ADODB.Recordset
    objGetInfo.Open strSQL, objConnection, adOpenForwardOnly, adLockOptimistic

    If Not objGetInfo.EOF And Not objGetInfo.BOF Then
        strAnswer = objGetInfo.Fields(28)
    End If

    strSQL = "SET ANSI_NULLS ON"
    Set objGetInfo = New ADODB.Recordset
    objGetInfo.Open strSQL, objConnection, adOpenForwardOnly, adLockOptimistic

    GetCodeDesc = strAnswer
End Function

谁能告诉我哪里可能出错了?

1 个答案:

答案 0 :(得分:0)

您没有显示任何将项目添加到组合框的代码,因此我猜您没有。我也没有在你的样本中看到你希望返回的记录超过1条,所以我将在一个组合框例程中提供一个通用的东西。

If objGetInfo.EOF = False And objGetInfo.BOF = False Then
    objGetInfo.MoveFirst 'redundant, but just to be sure we're on the first record
    Combobox1.Clear 'clear any current items
    Do While objGetInfo.EOF = False
        Combobox1.AddItem objGetInfo.Fields("MyField").Value
        objGetInfo.MoveNext 'move to the next record
    Loop
End If