Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to convert field data to string

Status
Not open for further replies.

bistec

Technical User
Dec 12, 2001
16
AU
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

---------------------------------------------
 
try changing this
"WHERE (((tblUnitAccessories.UnitID)=vUnitId));"

to this
"WHERE (((tblUnitAccessories.UnitID)= " & me.vUnitId & "));"

HTH
Dave
ToeShot@Hotmail.com
Today Is Tomorrows Yesterday. So Why Wait
 
Errr... No that didn't work.

The full error code is

Run-time Error '3061';

Too Few Parameters. Expected 1.

From what I can see I'm only passing 1
parameter to it.

Hmmmm....
 
I've tried that one too. Actually the dbOpenSnapshot was added out of desperation as was the setting of it as a DAO Recordset.

The SQL Statement works and returns 4 records (as it should)

 
Another consideration would be to make sure that you have the Microsoft DAO Object Library 3.X check and placed above the Microsoft ADO 2.X library Other wise you would need to use ADO instead of DAO Dave
ToeShot@Hotmail.com
Today Is Tomorrows Yesterday. So Why Wait
 
YEAH! fixed it.

I found this article

ACC2000: Errors Concatenating Variables or Controls (Q210244)

@Microsoft

and modified the following

strSQL = "SELECT tblUnitAccessories.Description FROM tblUnitAccessories WHERE ((tblUnitAccessories.UnitID)=" & vUnitId & ");"

That fixed it!

Thanks All
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top