Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2006

Tip: Looking for answers? Try searching our database.

Updating a Query Def from .Net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert - 16 Jan 2006 15:16 GMT
I am generating a Crystal Report based on an Access Query that uses queries
that uses other queries ... One query needs to be filtered on the fly and it
is not the main query called by the report.  I cannot filter past this query
because its data is later grouped in order to be useful by the report.  What
I thought I could do is modify the 'sub' query that needs a WHERE clause
added.  However, I do not know how to alter Access queries held in my db.  I
tried to delete the query and recreate it with the same name by using Drop,
but that doesn't work - Drop is for tables.  I am having trouble seeing
outside the box here.  What I need basicly is to filter data for my Crystal
Report that is hard-coded to use a main query.
Brendan Reynolds - 16 Jan 2006 15:48 GMT
You were on the right track with DROP, here's an example ...

Module Module1

   Sub Main()

       Dim strCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=c:\usenet\db1.mdb;" & _
           "Persist Security Info=False;" & _
           "Jet OLEDB:Database Password=password"
       Dim cnn As New System.Data.OleDb.OleDbConnection(strCnn)
       Dim cmd As System.Data.OleDb.OleDbCommand
       Try
           cnn.Open()
           cmd = New System.Data.OleDb.OleDbCommand
           cmd.Connection = cnn
           cmd.CommandText = "DROP VIEW vTest"
           cmd.ExecuteNonQuery()
           cmd.CommandText = _
               "CREATE VIEW vTest AS SELECT * FROM Employees"
           cmd.ExecuteNonQuery()
           Console.ReadLine()
       Catch ex As Exception
           Console.WriteLine(ex.Message)
           Console.ReadLine()
       Finally
           If Not cnn Is Nothing Then
               If cnn.State <> ConnectionState.Closed Then
                   cnn.Close()
               End If
           End If
       End Try
   End Sub

End Module

Signature

Brendan Reynolds
Access MVP

>I am generating a Crystal Report based on an Access Query that uses queries
> that uses other queries ... One query needs to be filtered on the fly and
[quoted text clipped - 12 lines]
> Crystal
> Report that is hard-coded to use a main query.
Robert - 17 Jan 2006 00:21 GMT
Excellent!  That did it!  I ended up with a significant discovery - If you
have joins in your query Access generates an error telling you that it can
only create simple Views.  I then tried creating a query to sub the place of
my original that selected all the data from the original query.  This allowed
me to run the following:

Create <new query> View As Select * from <original query name>

In my other queries, I select off the new query rather than the original and
all is now functional - woo hoo!  Thank you - you get 10 for 10!  Robert

> You were on the right track with DROP, here's an example ...
>
[quoted text clipped - 48 lines]
> > Crystal
> > Report that is hard-coded to use a main query.
Brendan Reynolds - 17 Jan 2006 11:09 GMT
Joins aren't the problem, Robert. There are other restrictions, though. You
don't seem to be able to use an ORDER BY clause in a view. I tried using
SELECT TOP 100 PERCENT, which I understand is what you have to do to use
ORDER BY in a SQL Server view, but that doesn't seem to work in JET. You can
still use an ORDER BY clause, though, you just have to create a procedure
rather than a view ...

Module Module1

   Sub Main()

       Dim strCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=c:\usenet\db1.mdb;" & _
           "Persist Security Info=False;" & _
           "Jet OLEDB:Database Password=password"
       Dim cnn As New System.Data.OleDb.OleDbConnection(strCnn)
       Dim cmd As System.Data.OleDb.OleDbCommand
       Try
           cnn.Open()
           cmd = New System.Data.OleDb.OleDbCommand
           cmd.Connection = cnn
           cmd.CommandText = "DROP VIEW vTest"
           cmd.ExecuteNonQuery()
           cmd.CommandText = "DROP PROCEDURE pTest"
           cmd.ExecuteNonQuery()
           cmd.CommandText = _
               "CREATE VIEW vTest AS SELECT " & _
               "Customers.CustomerID, " & _
               "Orders.OrderID FROM Customers INNER JOIN Orders ON " & _
               "Customers.CustomerID = Orders.CustomerID"
           cmd.ExecuteNonQuery()
           Console.WriteLine("View created")
           Console.ReadLine()
           cmd.CommandText = _
               "CREATE PROCEDURE pTest AS SELECT " & _
               "Customers.CustomerID, " & _
               "Orders.OrderID FROM Customers INNER JOIN Orders ON " & _
               "Customers.CustomerID = Orders.CustomerID " & _
               "ORDER BY Customers.CustomerID DESC"
           cmd.ExecuteNonQuery()
           Console.WriteLine("Procedure created")
           Console.ReadLine()
       Catch ex As Exception
           Console.WriteLine(ex.Message)
           Console.ReadLine()
       Finally
           If Not cnn Is Nothing Then
               If cnn.State <> ConnectionState.Closed Then
                   cnn.Close()
               End If
           End If
       End Try
   End Sub

End Module

Signature

Brendan Reynolds
Access MVP

> Excellent!  That did it!  I ended up with a significant discovery - If you
> have joins in your query Access generates an error telling you that it can
[quoted text clipped - 66 lines]
>> > Crystal
>> > Report that is hard-coded to use a main query.
Robert - 18 Jan 2006 06:47 GMT
Brendan, That was one of the most eloquent answers I have had in a long time!
Thank you!  While I am aware that I could create a stored proc I did not
choose that way.  In Access a stored proc is not easily implemented/used by
Crystal Reports.  I wanted to keep my report, my code, and my database simple
and I tried the following instead:

1.) My original query would not recreate exactly as it was (it did have an
Order By clause by the way).  It contained multiple joins, some inner some
right.  

2.) I knew that something specific about my query may be the cause besides
the joins, but I wanted to back off from testing any tweaks to the query - I
decided to blame it on the joins ... excuses are more fun than the truth
sometimes ...

3.) I decided to "simplify" the query as the error message suggested.  
Instead, I inserted a new query that would all the fields from original and
add my filter to it instead.

4.) This worked well and I implemented the idea farther up the tree of
queries that my main query called.  I didn't need to change anything besides
add two new queries.  My report runs and my filters are working as desired.

Now I need to figure out how to create an installation app that includes all
my files and the crystal report engine as well ... that's for another forum
...

Thank you Brendan, I would love to work along side someone like you someday!
Good luck to you!

> Joins aren't the problem, Robert. There are other restrictions, though. You
> don't seem to be able to use an ORDER BY clause in a view. I tried using
[quoted text clipped - 122 lines]
> >> > Crystal
> >> > Report that is hard-coded to use a main query.
Brendan Reynolds - 18 Jan 2006 10:58 GMT
You're very welcome, Robert, and I'm glad it's working for you. I was just
trying to be accurate - you never know how many other people with similar
problems may be following a thread, or may read it later in the archives.

Signature

Brendan Reynolds
Access MVP

> Brendan, That was one of the most eloquent answers I have had in a long
> time!
[quoted text clipped - 175 lines]
>> >> > Crystal
>> >> > Report that is hard-coded to use a main query.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.