Quote:
quote:
Hmm.. so it's not possible under VBA to use a Recordset returned from another function?
|
Sure it is. Your post indicated that you already had the class written, so I didn't bother writing it.
' ==================================
' clsSelectData
' ==================================
Public Function GetRecordset(ByRef objConnection As ADODB.Connection) As ADODB.Recordset
On Error GoTo Err_Handler
Dim cmd As New ADODB.Command
cmd.CommandText = "SELECT ID, FirstName, LastName FROM Table1"
cmd.CommandType = adCmdText
cmd.ActiveConnection = objConnection
Set GetRecordset = cmd.Execute
Set cmd = Nothing
Exit Function
Exit_Here:
On Error Resume Next
Exit Function
Err_Handler:
Err.Raise Err.Number, "clsSelectData::GetRecordset", Err.Description
Resume Exit_Here
End Function
' ==================================
' Form1
' ==================================
Private Sub Form_Load()
On Error GoTo ErrorHandler
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = CurrentProject.Connection
' ================================
' Instantiate clsSelectData object
' ================================
Dim objSelectData As clsSelectData
Set objSelectData = New clsSelectData
' ================================
' Get your recordset
' ================================
Set rst = objSelectData.GetRecordset(cnn)
' Configure combobox properties
With cboRecordset
.RowSourceType = "Value List"
.BoundColumn = 1
.ColumnCount = 2
.ColumnWidths = "0;1in"
End With
' Load combo box with recordset values.
With rst
.MoveFirst
Do Until .EOF
' The semi-colon in the string indicates a column break. The ID
' field is your BouondColumn, the concatenated last/first names
' are displayed. You CAN'T use a comma or semi-colon in place of
' the dash.
cboRecordset.AddItem !id & ";" & !LastName & " - " & !FirstName
.MoveNext
Loop
End With
' Ensure first item in Value List is displayed.
With cboRecordset
.DefaultValue = .ItemData(0)
End With
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
ErrorHandler:
If Not rst Is Nothing Then
If rst.State = adStateOpen Then rst.Close
End If
Set rst = Nothing
If Not cnn Is Nothing Then
If cnn.State = adStateOpen Then cnn.Close
End If
Set cnn = Nothing
If Err <> 0 Then
MsgBox Err.Source & " - " & Err.Description, , "Error"
End If
End Sub
HTH,
Bob