PC Review


Reply
Thread Tools Rate Thread

Delete a Query, or Empty it and Reload With New SQL?

 
 
ryguy7272
Guest
Posts: n/a
 
      1st Feb 2010
I’m wondering if it makes sense to delete a query which I build on the fly
each time I use. If I start with no Query, and then run my code, the Query
is loaded with SQL, which comes from several objects on a Form. This is all
driven by VBA. In the original example, the author did NOT delete the query,
but with the query present, I keep getting messages like this:
‘Error Description: Object ‘qryFilter’ already exists.’

If I manually delete the Query and rerun my code, everything works fine.
Would it make more sense to empty the query and then populate it with SQL?

Here is my code (works fine except that I keep getting that ‘query already
exists message’):

Option Compare Database
Option Explicit

Private Sub cmdRun_Click()

On Error GoTo cmdOK_Click_Err
Dim blnQueryExists As Boolean
Dim cat As New ADOX.Catalog
Dim cmd As New ADODB.Command
Dim qry As ADOX.View
Dim varItem As Variant
Dim strCust As String
Dim strTrader As String
Dim strTraderCondition As String

Dim strSQL As String
' Check for the existence of the stored query
blnQueryExists = False
Set cat.ActiveConnection = CurrentProject.Connection
For Each qry In cat.Views
If qry.Name = "qryFilter" Then
DoCmd.DeleteObject acQuery, "qryFilter"
Exit For
End If
Next qry
' Create the query if it does not already exist
If blnQueryExists = False Then
cmd.CommandText = "SELECT * FROM Trades"
cat.Views.Append "qryFilter", cmd
End If
Application.RefreshDatabaseWindow
' Turn off screen updating
DoCmd.Echo False
' Close the query if it is already open
If SysCmd(acSysCmdGetObjectState, acQuery, "qryFilter") = acObjStateOpen
Then
DoCmd.Close acQuery, "qryFilter"
End If
' Build criteria string for Office
For Each varItem In Me.lstCust.ItemsSelected
strCust = strCust & ",'" & Me.lstCust.ItemData(varItem) & "'"
Next varItem
If Len(strCust) = 0 Then
strCust = "Like '*'"
Else
strCust = Right(strCust, Len(strCust) - 1)
strCust = "IN(" & strCust & ")"
End If
' Build criteria string for Trader
For Each varItem In Me.lstTrader.ItemsSelected
strTrader = strTrader & ",'" & Me.lstTrader.ItemData(varItem) & "'"
Next varItem
If Len(strTrader) = 0 Then
strTrader = "Like '*'"
Else
strTrader = Right(strTrader, Len(strTrader) - 1)
strTrader = "IN(" & strTrader & ")"
End If

' Get condition

If Me.optAndTrader.Value = True Then
strTraderCondition = " AND "
Else
strTraderCondition = " OR "
End If

' Build SQL statement
strSQL = "SELECT * FROM Trades " & _
"WHERE Trades.[TDATE] Between [Forms]![QueryForm]![cboFrom] And
[Forms]![QueryForm]![cboTo] And Trades.[Cust] " & strCust & _
strTraderCondition & "Trades.[Trader] " & strTrader & ";"
' Apply the SQL statement to the stored query
cat.ActiveConnection = CurrentProject.Connection
Set cmd = cat.Views("qryFilter").Command
cmd.CommandText = strSQL
Set cat.Views("qryFilter").Command = cmd
Set cat = Nothing
' Open the Query
DoCmd.OpenQuery "qryFilter"
' If required the dialog can be closed at this point
' DoCmd.Close acForm, Me.Name
' Restore screen updating
cmdOK_Click_Exit:
DoCmd.Echo True
Exit Sub
cmdOK_Click_Err:
MsgBox "An unexpected error hass occurred." _
& vbCrLf & "Procedure: cmdOK_Click" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description:" & Err.Description _
, vbCritical, "Error"
Resume cmdOK_Click_Exit
End Sub

Private Sub optAndTrader_Click()
' Toggle option buttons
If Me.optAndTrader.Value = True Then
Me.optOrTrader.Value = False
Else
Me.optOrTrader.Value = True
End If
End Sub

Private Sub optOrTrader_Click()
' Toggle option buttons
If Me.optOrTrader.Value = True Then
Me.optAndTrader.Value = False
Else
Me.optAndTrader.Value = True
End If
End Sub

Finally, I already tried to delete the Query with this snippet of code:
' Check for the existence of the stored query
blnQueryExists = False
Set cat.ActiveConnection = CurrentProject.Connection
For Each qry In cat.Views
If qry.Name = "qryFilter" Then
DoCmd.DeleteObject acQuery, "qryFilter"
Exit For
End If
Next qry

That actually doesn’t do anything, and I can’t figure out why that doesn’t
work.

I’d sincerely appreciate help with this.

Thanks everyone!
Ryan--


--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      1st Feb 2010
On Mon, 1 Feb 2010 08:33:01 -0800, ryguy7272
<(E-Mail Removed)> wrote:

>If I manually delete the Query and rerun my code, everything works fine.
>Would it make more sense to empty the query and then populate it with SQL?
>


No.

A query *has no independent existance*. If you delete records from the query,
you're deleting records from the underlying table (permanently and without an
Undo option!)

And you don't "populate a query" with SQL. The SQL *IS* the query; that's how
it's stored in Access, as a SQL string (and in a binary compiled form derived
from the SQL string). The query is *NOT* the records that are displayed; the
query is a view, a way to select fields and records from a table or tables.

--

John W. Vinson [MVP]
 
Reply With Quote
 
ryguy7272
Guest
Posts: n/a
 
      2nd Feb 2010
Thanks John and Jim! I got it worked out. For anyone who is interested,
here is the final version of code:

Private Sub cmdRun_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim varItem As Variant
Dim strCust As String
Dim strTrader As String
Dim strTraderCondition As String

Set db = CurrentDb
Set qdf = db.QueryDefs("qryFilter")

' Close the query if it is already open
If SysCmd(acSysCmdGetObjectState, acQuery, "qryFilter") = acObjStateOpen
Then
DoCmd.Close acQuery, "qryFilter"
End If
' Build criteria string for Office
For Each varItem In Me.lstCust.ItemsSelected
strCust = strCust & ",'" & Me.lstCust.ItemData(varItem) & "'"
Next varItem
If Len(strCust) = 0 Then
strCust = "Like '*'"
Else
strCust = Right(strCust, Len(strCust) - 1)
strCust = "IN(" & strCust & ")"
End If
' Build criteria string for Trader
For Each varItem In Me.lstTrader.ItemsSelected
strTrader = strTrader & ",'" & Me.lstTrader.ItemData(varItem) & "'"
Next varItem
If Len(strTrader) = 0 Then
strTrader = "Like '*'"
Else
strTrader = Right(strTrader, Len(strTrader) - 1)
strTrader = "IN(" & strTrader & ")"
End If

' Get condition
If Me.optAndTrader.Value = True Then
strTraderCondition = " AND "
Else
strTraderCondition = " OR "
End If

' Build SQL statement
strSQL = "SELECT * FROM Trades " & _
"WHERE (Trades.[TDATE] Between [Forms]![QueryForm]![cboFrom]
And [Forms]![QueryForm]![cboTo]) And (Trades.[Cust] " & strCust & _
strTraderCondition & "Trades.[Trader] " & strTrader & ");"

' Open the Query
qdf.SQL = strSQL
DoCmd.OpenQuery "qryFilter"
Set qdf = Nothing
Set db = Nothing
End Sub

--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"JimBurke via AccessMonster.com" wrote:

> I didn't look through all of your code, but it sounds like you're defining a
> query to access when you could just be running the SQL and not actually
> creating a query. Is there a reason you're creating a query, or am I
> misinterpreting? You can just execute an SQL statement without actually
> defining a new query.
>
> ryguy7272 wrote:
> >I’m wondering if it makes sense to delete a query which I build on the fly
> >each time I use. If I start with no Query, and then run my code, the Query
> >is loaded with SQL, which comes from several objects on a Form. This is all
> >driven by VBA. In the original example, the author did NOT delete the query,
> >but with the query present, I keep getting messages like this:
> >‘Error Description: Object ‘qryFilter’ already exists.’
> >
> >If I manually delete the Query and rerun my code, everything works fine.
> >Would it make more sense to empty the query and then populate it with SQL?
> >
> >Here is my code (works fine except that I keep getting that ‘query already
> >exists message’):
> >
> >Option Compare Database
> >Option Explicit
> >
> >Private Sub cmdRun_Click()
> >
> > On Error GoTo cmdOK_Click_Err
> > Dim blnQueryExists As Boolean
> > Dim cat As New ADOX.Catalog
> > Dim cmd As New ADODB.Command
> > Dim qry As ADOX.View
> > Dim varItem As Variant
> > Dim strCust As String
> > Dim strTrader As String
> > Dim strTraderCondition As String
> >
> > Dim strSQL As String
> >' Check for the existence of the stored query
> > blnQueryExists = False
> > Set cat.ActiveConnection = CurrentProject.Connection
> > For Each qry In cat.Views
> > If qry.Name = "qryFilter" Then
> > DoCmd.DeleteObject acQuery, "qryFilter"
> > Exit For
> > End If
> > Next qry
> >' Create the query if it does not already exist
> > If blnQueryExists = False Then
> > cmd.CommandText = "SELECT * FROM Trades"
> > cat.Views.Append "qryFilter", cmd
> > End If
> > Application.RefreshDatabaseWindow
> >' Turn off screen updating
> > DoCmd.Echo False
> >' Close the query if it is already open
> > If SysCmd(acSysCmdGetObjectState, acQuery, "qryFilter") = acObjStateOpen
> >Then
> > DoCmd.Close acQuery, "qryFilter"
> > End If
> >' Build criteria string for Office
> > For Each varItem In Me.lstCust.ItemsSelected
> > strCust = strCust & ",'" & Me.lstCust.ItemData(varItem) & "'"
> > Next varItem
> > If Len(strCust) = 0 Then
> > strCust = "Like '*'"
> > Else
> > strCust = Right(strCust, Len(strCust) - 1)
> > strCust = "IN(" & strCust & ")"
> > End If
> >' Build criteria string for Trader
> > For Each varItem In Me.lstTrader.ItemsSelected
> > strTrader = strTrader & ",'" & Me.lstTrader.ItemData(varItem) & "'"
> > Next varItem
> > If Len(strTrader) = 0 Then
> > strTrader = "Like '*'"
> > Else
> > strTrader = Right(strTrader, Len(strTrader) - 1)
> > strTrader = "IN(" & strTrader & ")"
> > End If
> >
> >' Get condition
> >
> > If Me.optAndTrader.Value = True Then
> > strTraderCondition = " AND "
> > Else
> > strTraderCondition = " OR "
> > End If
> >
> >' Build SQL statement
> > strSQL = "SELECT * FROM Trades " & _
> > "WHERE Trades.[TDATE] Between [Forms]![QueryForm]![cboFrom] And
> >[Forms]![QueryForm]![cboTo] And Trades.[Cust] " & strCust & _
> > strTraderCondition & "Trades.[Trader] " & strTrader & ";"
> >' Apply the SQL statement to the stored query
> > cat.ActiveConnection = CurrentProject.Connection
> > Set cmd = cat.Views("qryFilter").Command
> > cmd.CommandText = strSQL
> > Set cat.Views("qryFilter").Command = cmd
> > Set cat = Nothing
> >' Open the Query
> > DoCmd.OpenQuery "qryFilter"
> >' If required the dialog can be closed at this point
> > ' DoCmd.Close acForm, Me.Name
> >' Restore screen updating
> >cmdOK_Click_Exit:
> > DoCmd.Echo True
> > Exit Sub
> >cmdOK_Click_Err:
> > MsgBox "An unexpected error hass occurred." _
> > & vbCrLf & "Procedure: cmdOK_Click" _
> > & vbCrLf & "Error Number: " & Err.Number _
> > & vbCrLf & "Error Description:" & Err.Description _
> > , vbCritical, "Error"
> > Resume cmdOK_Click_Exit
> >End Sub
> >
> >Private Sub optAndTrader_Click()
> >' Toggle option buttons
> > If Me.optAndTrader.Value = True Then
> > Me.optOrTrader.Value = False
> > Else
> > Me.optOrTrader.Value = True
> > End If
> >End Sub
> >
> >Private Sub optOrTrader_Click()
> >' Toggle option buttons
> > If Me.optOrTrader.Value = True Then
> > Me.optAndTrader.Value = False
> > Else
> > Me.optAndTrader.Value = True
> > End If
> >End Sub
> >
> >Finally, I already tried to delete the Query with this snippet of code:
> >' Check for the existence of the stored query
> > blnQueryExists = False
> > Set cat.ActiveConnection = CurrentProject.Connection
> > For Each qry In cat.Views
> > If qry.Name = "qryFilter" Then
> > DoCmd.DeleteObject acQuery, "qryFilter"
> > Exit For
> > End If
> > Next qry
> >
> >That actually doesn’t do anything, and I can’t figure out why that doesn’t
> >work.
> >
> >I’d sincerely appreciate help with this.
> >
> >Thanks everyone!
> >Ryan--
> >

>
> --
> Jim Burke
>
> Message posted via AccessMonster.com
> http://www.accessmonster.com/Uwe/For...dules/201002/1
>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Rows with Empty Cells with empty column 1 Scott Microsoft Excel Programming 6 2nd Oct 2006 11:57 PM
Mount NTFS to A empty; Unable to Delete a empty directory amontse@gmail.com Windows XP Help 2 9th Mar 2006 01:44 AM
delete empty rows by query? =?Utf-8?B?Y2hlcnJ5bmljaA==?= Microsoft Access 3 18th Oct 2004 07:50 PM
Delete/Reload Bill Windows XP Internet Explorer 0 11th May 2004 03:37 AM
XP remove, delete, reload Lynda Windows XP Basics 3 11th Jan 2004 10:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:15 AM.