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
|