Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
-->
TitleCreate a Recordset and attach it to a DBCombo control
KeywordsDBCombo, Recordset, bound column, ADO, database
CategoriesControls, Database
 
This example makes a Recordset holding the data you want bound to the DBCombo. It then attaches it to the DBCombo, optionally setting the control's BoundColumn.

Thanks to R. Karthik.

 
Public Sub setData_Cbo(ByRef DataCombo As Variant, table As _
    String, lstfield As String, AdoCon As Connection, _
    Optional boundcol As String, Optional Filter As String)

    'author:Karthik  mail4karthik@indiatimes.com
    'Date:10.05.2002
    'Sets the Rowsource and List field of Datacombo
    'Parameters Datacombo,Name of Table/Query,Field name to
    ' list,Optional Field code to     'bound   ' and ADO
    ' Connection
    'filter - the string that comes after Where clause in 
    ' a Sql select  to filter the recordset, when      '
    ' you need only part of records
    'in Future: Replace the AdoCon argument with a
    ' Connection String

    Dim Trs As Recordset
    Set Trs = New Recordset
    Dim StrSql As String

    Trs.CursorLocation = adUseClient

    If IsEmpty(boundcol) = True Or Len(boundcol) = 0 Then
        StrSql = "Select " & lstfield & " from " & table
    Else
        StrSql = "Select " & lstfield & ", " & boundcol & " " & _
            "from " & table
    End If

    If Len(Trim(Filter)) > 0 Then   '"Where Clause
        StrSql = StrSql & " Where " & Filter
    End If

    StrSql = StrSql & " order by " & lstfield 'Order By

    On Error GoTo objError   ' if connection not passed
    If AdoCon.State = adStateOpen Then
        Trs.Open StrSql, AdoCon
        Set DataCombo.RowSource = Trs
    End If
    
ContObjErr:
    On Error GoTo 0

    DataCombo.Text = ""
    DataCombo.ListField = lstfield
    
    If Len(boundcol) > 0 Then
        DataCombo.BoundColumn = boundcol
    Else
        DataCombo.BoundColumn = ""
    End If
    Exit Sub
    
objError:
    If Err.Number = 91 Then 'object not set
        'continue the folowing code
        GoTo ContObjErr
    Else
        MsgBox Err.Description, vbExclamation, Err.Number & _
            Err.Source
        Exit Sub
    End If
End Sub
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated