S
stuman
I have an unbound form with several multi List boxes (only two shown below).
Once the User makes their selection(s) from any list box and hits command
button, the WHERE string is applied to qryByMake. I then apply those results
to qryByMake1. I am trying to simplify this process into one step and
eliminate the saved query 'qryByMNake1'. Using the second query is the only
way I have been able to make it work so far. Can anyone see or suiggest a
simpler method of code? Any help appreciated.
Private Sub cmdOK_Click()
On Error GoTo Err_cmdOK_Click
Dim varItem As Variant
Dim strWhere As String
Dim strWhere1 As String
Dim strWhere2 As String
Dim lngLen As Long
Dim strDelim As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQL3 As String
With Me!lstGroup
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere1 = strWhere1 & "'" & strDelim & .ItemData(varItem) &
strDelim & "',"
End If
Next varItem
End With
lngLen = Len(strWhere1) - 1
If lngLen > 0 Then
strWhere1 = "[PartName] IN (" & Left$(strWhere1, lngLen) & ") "
End If
With Me!lstClass
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere2 = strWhere2 & "'" & strDelim & .ItemData(varItem) &
strDelim & "',"
End If
Next varItem
End With
lngLen = Len(strWhere2) - 1
If lngLen > 0 Then
strWhere2 = "[CO] IN (" & Left$(strWhere2, lngLen) & ") "
End If
strWhere = strWhere1
If Len(strWhere) > 0 And Len(strWhere2) > 0 Then
strWhere = strWhere & " AND " & strWhere2
Else
strWhere = strWhere & strWhere2
End If
Set db = CurrentDb
'*** create the query based on the information on the form - Need to
Simplify Code Below ***
strSQL = "SELECT qryByMake.* FROM qryByMake "
strSQL = strSQL & " WHERE " & strWhere
Set qdf = db.QueryDefs("qryByMake1")
qdf.SQL = strSQL
strSQL1 = "SELECT qryByMake1.ProductID, qryByMake1.Number, qryByMake1.Make,
qryByMake1.Model, qryByMake1.Brand, qryByMake1.PartName, qryByMake1.JobGroup,
qryByMake1.CO, qryByMake1.From, qryByMake1.To, qryByMake1.GroupID,
qryByMake1.SupplierID, qryByMake1.CodeID INTO tblTempMake " & vbCrLf & _
"FROM qryByMake1;"
strSQL2 = "UPDATE tblProduct SET tblProduct.TM = 0;"
strSQL3 = "UPDATE tblProduct INNER JOIN tblTempMake ON tblProduct.ProductID
= tblTempMake.ProductID SET tblProduct.TM = -1;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL1
DoCmd.RunSQL strSQL2
DoCmd.RunSQL strSQL3
DoCmd.OpenForm "frmProduct", acFormDS, "", "(((tblSequence.Mfg)=""IMC / OP
PARTS"") AND ((TM)=-1))", , acNormal
DoCmd.SetWarnings True
Exit_cmdOK_Click:
Exit Sub
Err_cmdOK_Click:
If Err.Number = 3265 Then '*** if the error is the query is missing
Resume Next '*** then skip the delete line and resume on
the next line
Else
MsgBox Err.Description '*** write out the error and exit the sub
Resume Exit_cmdOK_Click
End If
End Sub
Once the User makes their selection(s) from any list box and hits command
button, the WHERE string is applied to qryByMake. I then apply those results
to qryByMake1. I am trying to simplify this process into one step and
eliminate the saved query 'qryByMNake1'. Using the second query is the only
way I have been able to make it work so far. Can anyone see or suiggest a
simpler method of code? Any help appreciated.
Private Sub cmdOK_Click()
On Error GoTo Err_cmdOK_Click
Dim varItem As Variant
Dim strWhere As String
Dim strWhere1 As String
Dim strWhere2 As String
Dim lngLen As Long
Dim strDelim As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQL3 As String
With Me!lstGroup
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere1 = strWhere1 & "'" & strDelim & .ItemData(varItem) &
strDelim & "',"
End If
Next varItem
End With
lngLen = Len(strWhere1) - 1
If lngLen > 0 Then
strWhere1 = "[PartName] IN (" & Left$(strWhere1, lngLen) & ") "
End If
With Me!lstClass
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere2 = strWhere2 & "'" & strDelim & .ItemData(varItem) &
strDelim & "',"
End If
Next varItem
End With
lngLen = Len(strWhere2) - 1
If lngLen > 0 Then
strWhere2 = "[CO] IN (" & Left$(strWhere2, lngLen) & ") "
End If
strWhere = strWhere1
If Len(strWhere) > 0 And Len(strWhere2) > 0 Then
strWhere = strWhere & " AND " & strWhere2
Else
strWhere = strWhere & strWhere2
End If
Set db = CurrentDb
'*** create the query based on the information on the form - Need to
Simplify Code Below ***
strSQL = "SELECT qryByMake.* FROM qryByMake "
strSQL = strSQL & " WHERE " & strWhere
Set qdf = db.QueryDefs("qryByMake1")
qdf.SQL = strSQL
strSQL1 = "SELECT qryByMake1.ProductID, qryByMake1.Number, qryByMake1.Make,
qryByMake1.Model, qryByMake1.Brand, qryByMake1.PartName, qryByMake1.JobGroup,
qryByMake1.CO, qryByMake1.From, qryByMake1.To, qryByMake1.GroupID,
qryByMake1.SupplierID, qryByMake1.CodeID INTO tblTempMake " & vbCrLf & _
"FROM qryByMake1;"
strSQL2 = "UPDATE tblProduct SET tblProduct.TM = 0;"
strSQL3 = "UPDATE tblProduct INNER JOIN tblTempMake ON tblProduct.ProductID
= tblTempMake.ProductID SET tblProduct.TM = -1;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL1
DoCmd.RunSQL strSQL2
DoCmd.RunSQL strSQL3
DoCmd.OpenForm "frmProduct", acFormDS, "", "(((tblSequence.Mfg)=""IMC / OP
PARTS"") AND ((TM)=-1))", , acNormal
DoCmd.SetWarnings True
Exit_cmdOK_Click:
Exit Sub
Err_cmdOK_Click:
If Err.Number = 3265 Then '*** if the error is the query is missing
Resume Next '*** then skip the delete line and resume on
the next line
Else
MsgBox Err.Description '*** write out the error and exit the sub
Resume Exit_cmdOK_Click
End If
End Sub