Trying to take the data out a field from several records and create a string using VB. e.g.
Description (field)
--------------------
Lounge
Chair
Television
etc
I want to get this output as a string variable
Lounge, Chair, Television, etc
I've been trying to get the following working but I keep getting a 3061 error on the recordset(strSQL) statement.
-------------------------------
Function GetAccessory(vUnitId As Integer) as String
'Purpose: Return the accessories installed on a unit.
'Arguments: vUnitID = the unit to report on.
'Return: List in String.
Dim db As Database 'CurrentDb()
Dim rsAccess As DAO.Recordset 'Various recordsets.
Dim strSQL As String 'SQL statement.
Dim accessStr As String 'List of Accessories.
If Not IsNull(vUnitId) Then
'Initialize: Validate and convert parameters.
Set db = CurrentDb()
accessStr = ""
'Get the accessories for unit.
strSQL = "SELECT tblUnitAccessories.Description FROM tblUnitAccessories " & _
"WHERE (((tblUnitAccessories.UnitID)=vUnitId));"
Set rsAccess = db.OpenRecordset(strSQL, dbOpenSnapshot)
If rsAccess.RecordCount > 0 Then
rsAccess.MoveFirst
Do While Not Null
accessStr = accessStr & " " & rsAccess!Description
rsAccess.MoveNext
Loop
End If
rsAccess.Close
'Assign the return value
GetAccessory = accessStr
End If
Set rsAccess = Nothing
Set db = Nothing
Exit Function
End Function
---------------------------------------------
Description (field)
--------------------
Lounge
Chair
Television
etc
I want to get this output as a string variable
Lounge, Chair, Television, etc
I've been trying to get the following working but I keep getting a 3061 error on the recordset(strSQL) statement.
-------------------------------
Function GetAccessory(vUnitId As Integer) as String
'Purpose: Return the accessories installed on a unit.
'Arguments: vUnitID = the unit to report on.
'Return: List in String.
Dim db As Database 'CurrentDb()
Dim rsAccess As DAO.Recordset 'Various recordsets.
Dim strSQL As String 'SQL statement.
Dim accessStr As String 'List of Accessories.
If Not IsNull(vUnitId) Then
'Initialize: Validate and convert parameters.
Set db = CurrentDb()
accessStr = ""
'Get the accessories for unit.
strSQL = "SELECT tblUnitAccessories.Description FROM tblUnitAccessories " & _
"WHERE (((tblUnitAccessories.UnitID)=vUnitId));"
Set rsAccess = db.OpenRecordset(strSQL, dbOpenSnapshot)
If rsAccess.RecordCount > 0 Then
rsAccess.MoveFirst
Do While Not Null
accessStr = accessStr & " " & rsAccess!Description
rsAccess.MoveNext
Loop
End If
rsAccess.Close
'Assign the return value
GetAccessory = accessStr
End If
Set rsAccess = Nothing
Set db = Nothing
Exit Function
End Function
---------------------------------------------