I have a listbox, which I set the row source on click of a filter type button on a form. The row source I set is a public function, which takes 2 parameters. Originally I tried a stored procedure, but found that you cannot set the row source for a listbox to a stored proc. I converted this to dynamic sql. here is the function:
Here is the on click event of a command button, which should set the row source for the list box:
The .rowsource is in bold red. Can anyone see what I am doing wrong? The specific error I get is
"Run-time error ' 13':
Type mismatch"
misscrf
It is never too late to become what you could have been ~ George Eliot
Code:
Public Function AssignedMediaFiles(ByVal pkey As String, AID As Integer)
Dim objConn As ADODB.connection
Dim objRS As ADODB.Recordset
Dim strSQL
Set objConn = CreateObject("ADODB.Connection")
objConn.connectionString = "Driver={SQL Server Native Client 11.0};Server=LIT-SQL;Database=123456Inventories;Trusted_Connection=yes;"
Set objRS = CreateObject("ADODB.Recordset")
objConn.Open
strSQL = "WITH SourceStuff AS (SELECT a.ID AS AID, asd.ID AS ASDID, a.MediaTag, asd.txtSourceTag, fl.id AS FLID, asd.txtSourceTag AS AssignedSources " & _
"FROM [123456ProjectManagement].[dbo].[tblMediaTrackingNew] a (NOLOCK) " & _
"JOIN [123456ProjectManagement].[dbo].[tblMediaSourceDetail] asd (NOLOCK) ON a.ID = asd.FKMediaTag " & _
"JOIN [123456ProjectManagement].[dbo].[tblMediaSourceInventory] asi (NOLOCK) ON asd.ID = asi.FKSource " & _
"JOIN [123456Inventories].[dbo].[tbl" & pkey & "FileList] fl (NOLOCK) ON asi.FKInventory = fl.id) " & _
"SELECT DISTINCT t1.AID, t1.FLID, STUFF((SELECT DISTINCT '; ' + /*(NULLIF(AssignedSources,''),*/ AssignedSources " & _
"FROM SourceStuff t2 WHERE t1.FLID = t2.FLID ORDER BY '; ' + AssignedSources FOR XML PATH(''), TYPE).value('.','VARCHAR(MAX)'),1,1,'') AS AssignedSources " & _
"FROM SourceStuff t1 (NOLOCK) " & _
"JOIN [123456Inventories].[dbo].[tbl" & pkey & "FileList] fl (NOLOCK) ON t1.FLID = fl.id " & _
"JOIN [123456ProjectManagement].[dbo].[tblMediaSourceInventory] asi (NOLOCK) ON fl.id = asi.FKInventory " & _
"JOIN [123456ProjectManagement].[dbo].[tblMediaSourceDetail] asd (NOLOCK) ON asi.FKSource = asd.ID " & _
"JOIN [123456ProjectManagement].[dbo].[tblMediaTrackingNew] a (NOLOCK) ON asd.FKMediaTag = a.ID " & _
"WHERE a.ID = " & AID & " AND t1.AssignedSources IS NOT NULL"
With objRS
.Source = strSQL
.ActiveConnection = objConn
.CursorType = adOpenForwardOnly
.LockType = adLockReadOnly
End With
objRS.Open , , , , adCmdText
If Not objRS.EOF Then
AssignedMediaFiles = objRS.GetRows
End If
objRS.Close
Set objRS = Nothing
End Function
Here is the on click event of a command button, which should set the row source for the list box:
Code:
Private Sub tgAssigned_Click()
Dim strSQL As String
If Me.tgAssigned = -1 Then
Me.tgAssigned.Caption = "Viewing Assigned"
Me.lstInv.Visible = True
[COLOR=#EF2929][b]Me.lstInv.RowSource = AssignedAssetFiles(Me.Bgroup, Me.AssetID)[/b][/color]
Else
Me.tgAssigned.Caption = "Viewing All"
Me.lstInv.Visible = False
End If
End Sub
The .rowsource is in bold red. Can anyone see what I am doing wrong? The specific error I get is
"Run-time error ' 13':
Type mismatch"
misscrf
It is never too late to become what you could have been ~ George Eliot