Dear All,
Thanks for a great forum!
My question is as such...
I have a form that has a few buttons...
One of them is Run Query.
The run query button has the following code.
The code basically opens certain queries.
My question is is there a way to test if the query exisits.
I then want to display the message box as above (MsgBxNoQuery).
I have done a crude version of this using the case statements.
But would like to, instead of always modifying the list, test if the query exists.
Any help will be greatly appreciated.
Thanks!
Thank you,
Kind regards
Triacona
Thanks for a great forum!
My question is as such...
I have a form that has a few buttons...
One of them is Run Query.
The run query button has the following code.
Code:
Private Sub CpRunQuery_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim SQL As String
Dim Lbx As ListBox, idx
Dim Pack As String
Dim stDocName As String
stDocName = ListCP ' might need to get removed
If IsNull(txtStartDate And txtEndDate) Then
If txtEndDate.Visible = True Then
DoCmd.RunMacro "MsgBoxNoDate"
Else: Select Case ListCP.ItemData(ListCP.ListIndex)
Case "CpSotLetter", "CpSotFsaXml", "SotView", "MissingRecordsCheckFsaXmlAndCpListResult", "CpCmbResults", "CpFoodPremTypeSearch"
DoCmd.OpenQuery stDocName, acNormal, acEdit
'------------------------------------------------------------------
Case "CpSotCertificate"
DoCmd.RunMacro "MsgBxNoQuery"
Case "CpListResults"
Set db = CurrentDb
Set qdf = db.QueryDefs("CpListResults")
Set Lbx = ListCpXresult
SQL = "SELECT UNI7LIVE_CPINFO.REFVAL, UNI7LIVE_CPINFO.TRADEAS, UNI7LIVE_CPINFO.ADDRESS AS Add, " & _
"UNI7LIVE_CPINFO.CPUSE AS MainUse, CpUseCodes.CODETEXT AS MainUseDsc, UNI7LIVE_CPINFO.CONTACT, " & _
"UNI7LIVE_CPUSES.CPUSE AS AllUses, CpUsesCodes.CODETEXT AS AllUsesDsc " & _
"FROM ((UNI7LIVE_CPINFO " & _
"INNER JOIN CpUseCodes ON UNI7LIVE_CPINFO.CPUSE = CpUseCodes.CODEVALUE) " & _
"INNER JOIN UNI7LIVE_CPUSES ON UNI7LIVE_CPINFO.KEYVAL = UNI7LIVE_CPUSES.PKEYVAL) " & _
"INNER JOIN CpUseCodes AS CpUsesCodes ON UNI7LIVE_CPUSES.CPUSE = CpUsesCodes.CODEVALUE " & _
"GROUP BY UNI7LIVE_CPINFO.REFVAL, UNI7LIVE_CPINFO.TRADEAS, UNI7LIVE_CPINFO.ADDRESS, UNI7LIVE_CPINFO.CPUSE, " & _
"CpUseCodes.CODETEXT , UNI7LIVE_CPINFO.CONTACT, UNI7LIVE_CPUSES.CPUSE, " & _
"CpUsesCodes.CODETEXT, UNI7LIVE_CPINFO.CLOSEDD "
For Each idx In Lbx.ItemsSelected
If Pack <> "" Then
Pack = Pack & " AND ((UNI7LIVE_CPINFO.CLOSEDD) Is Null) "
Pack = Pack & " OR ((UNI7LIVE_CPUSES.CPUSE) LIKE '" & Lbx.Column(0, idx) & "')"
Else
Pack = " HAVING ((UNI7LIVE_CPUSES.CPUSE) LIKE'" & Lbx.Column(0, idx) & "') "
End If
Next
If Pack <> "" Then
Pack = Pack & " AND ((UNI7LIVE_CPINFO.CLOSEDD) Is Null) " & _
" ORDER BY UNI7LIVE_CPINFO.TRADEAS;"
qdf.SQL = SQL & Pack
Else
Pack = Pack & " ORDER BY UNI7LIVE_CPINFO.TRADEAS;" & _
qdf.SQL = SQL & Pack
End If
Debug.Print SQL & Pack
DoCmd.OpenQuery ListCP, acViewNormal
DoCmd.Maximize
qdf.Close
Set qdf = Nothing
Set db = Nothing
Set Lbx = Nothing
'-----------------------------------------------------------------------------------
Case "CpListResultsInsLiab"
Set db = CurrentDb
Set qdf = db.QueryDefs("CpListResultsInsLiab")
Set Lbx = CpListInspLiab
SQL = "SELECT UNI7LIVE_CPINFO.REFVAL, UNI7LIVE_CPINFO.TRADEAS, UNI7LIVE_CPINFO.ADDRESS AS [Add], " & _
"UNI7LIVE_CPINFO.CPUSE AS MainUse, UNI7LIVE_CPINFO.CONTACT, UNI7LIVE_CPINSPLIAB.INSPTYPE, " & _
"UNI7LIVE_CPINSPLIAB.INSPECTION_TYPE, UNI7LIVE_CPINSPLIAB.CPFOODMAIN, CmbCpMainUseLiab.CODETEXT " & _
"FROM CmbCpMainUseLiab INNER JOIN (UNI7LIVE_CPINFO INNER JOIN UNI7LIVE_CPINSPLIAB " & _
"ON UNI7LIVE_CPINFO.KEYVAL = UNI7LIVE_CPINSPLIAB.PKEYVAL) ON CmbCpMainUseLiab.CODEVALUE = UNI7LIVE_CPINSPLIAB.CPFOODMAIN " & _
"GROUP BY UNI7LIVE_CPINFO.REFVAL, UNI7LIVE_CPINFO.TRADEAS, UNI7LIVE_CPINFO.ADDRESS, " & _
"UNI7LIVE_CPINFO.CPUSE, UNI7LIVE_CPINFO.CONTACT, UNI7LIVE_CPINSPLIAB.INSPTYPE, UNI7LIVE_CPINSPLIAB.INSPECTION_TYPE, " & _
"UNI7LIVE_CPINSPLIAB.CPFOODMAIN, CmbCpMainUseLiab.CODETEXT, UNI7LIVE_CPINFO.CLOSEDD "
For Each idx In Lbx.ItemsSelected
If Pack <> "" Then
Pack = Pack & " AND ((UNI7LIVE_CPINFO.CLOSEDD) Is Null) "
Pack = Pack & " OR ((UNI7LIVE_CPINSPLIAB.CPFOODMAIN) LIKE '" & Lbx.Column(0, idx) & "')" ' UNI7LIVE_CPINSPLIAB.CPFOODMAIN
Else
Pack = " HAVING ((UNI7LIVE_CPINSPLIAB.CPFOODMAIN) LIKE'" & Lbx.Column(0, idx) & "') "
End If
Next
If Pack <> "" Then
Pack = Pack & " AND ((UNI7LIVE_CPINFO.CLOSEDD) Is Null) " & _
" ORDER BY UNI7LIVE_CPINFO.TRADEAS;"
qdf.SQL = SQL & Pack
Else
Pack = Pack & " ORDER BY UNI7LIVE_CPINFO.TRADEAS;" & _
qdf.SQL = SQL & Pack
End If
Debug.Print SQL & Pack
DoCmd.OpenQuery ListCP, acViewNormal
DoCmd.Maximize
qdf.Close
Set qdf = Nothing
Set db = Nothing
Set Lbx = Nothing
End Select
End If
End If
End Sub
The code basically opens certain queries.
My question is is there a way to test if the query exisits.
I then want to display the message box as above (MsgBxNoQuery).
I have done a crude version of this using the case statements.
But would like to, instead of always modifying the list, test if the query exists.
Any help will be greatly appreciated.
Thanks!
Thank you,
Kind regards
Triacona