R
ryguy7272
I am trying to create a Query on the fly, using variables from a ListBox. I
did this before and it worked fine; the solution escapes me now. Below is
the code:
Option Compare Database
Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM tblLogin"
'Build the IN string by looping through the listbox
For i = 0 To lstLogin.ListCount - 1
If lstLogin.Selected(i) Then
strIN = strIN & "'" & lstLogin.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [strLogin] in (" & Left(strIN, Len(strIN) - 1) & ")"
MyDB.QueryDefs.Delete "qryLoginByID"
Set qdef = MyDB.CreateQueryDef("qryLoginByID", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qryLoginByID", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.lstLogin.ItemsSelected
Me.lstLogin.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", , "Selection
Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
DoCmd.Close
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_cmdClose_Click
End Sub
When I run the Query, all values are returned!!
This is the SQL:
SELECT *
FROM tblLogin;
That's why all variables are returned. What I can't figure out is why the
appropriate string is not passed to the Query...
I think this is the string now, but it seems to be incorrect:
strWhere = " WHERE [strLogin] in (" & Left(strIN, Len(strIN) - 1) & ")"
Can anyone see the error? I can't see it.
Regards,
Ryan---
did this before and it worked fine; the solution escapes me now. Below is
the code:
Option Compare Database
Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM tblLogin"
'Build the IN string by looping through the listbox
For i = 0 To lstLogin.ListCount - 1
If lstLogin.Selected(i) Then
strIN = strIN & "'" & lstLogin.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [strLogin] in (" & Left(strIN, Len(strIN) - 1) & ")"
MyDB.QueryDefs.Delete "qryLoginByID"
Set qdef = MyDB.CreateQueryDef("qryLoginByID", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qryLoginByID", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.lstLogin.ItemsSelected
Me.lstLogin.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", , "Selection
Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
DoCmd.Close
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_cmdClose_Click
End Sub
When I run the Query, all values are returned!!
This is the SQL:
SELECT *
FROM tblLogin;
That's why all variables are returned. What I can't figure out is why the
appropriate string is not passed to the Query...
I think this is the string now, but it seems to be incorrect:
strWhere = " WHERE [strLogin] in (" & Left(strIN, Len(strIN) - 1) & ")"
Can anyone see the error? I can't see it.
Regards,
Ryan---