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 / Reports / Printing / March 2008

Tip: Looking for answers? Try searching our database.

Populating report from code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Aino - 21 Mar 2008 15:10 GMT
Hi

I am really not sure to get this done.

Based on input from a form, I process a query, ending up assigning my
desired results to variables inside a loop.
Now I would like to display the results in a report.

At first I thought I could just add textboxes as desired to the report
inside the loop before opening it: Nope.
Then I thougth of using listboxes and adding the results as new lines
to those from the loop: No can do.
Instead I thougth I would create a new recordset, fill it with my
results, and then use this as the recordsouce: Can't be done.

Could someone please point me in the rigth direction?

Here is the code:
===============================
Private Sub btnOK_Click()

   Dim dbs             As DAO.Database
   Dim rstAvailTimes   As DAO.Recordset
   Dim rstCars            As DAO.Recordset
   Dim rstAvailCar        As DAO.Recordset
   Dim strCriteria     As String
   Dim strSQL          As String
   Dim strCar             As String
   Dim strDay          As String
   Dim datFrom         As Date
   Dim datTo           As Date
   Dim datMaxForCar       As Date
   Dim datMinForCar       As Date
   Dim datTimeMax      As Date
   Dim datTimeMin      As Date
   Dim strTimeMax      As String
   Dim strTimeMin      As String
   Dim strAdrMax       As String
   Dim strAdrMin       As String

   On Error GoTo Err_btnOK_Click

   strDay = cmbDay
   datFrom = txtFrom
   datTo = txtTo

   Set dbs = CurrentDb
   strCriteria = "[Day] = '" & strDay & "' AND NOT [Car] IN " _
       & "(SELECT [Car] FROM [qryTimes] WHERE [Day] = '" & strDay _
       & "' AND [TimeMax] > #" & datFrom & "# AND [TimeMin] < #" &
datTo & "#)"
   strSQL = "SELECT * FROM [qryTimes] WHERE " & strCriteria
   Set rstAvailTimes = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
   If rstAvailTimes.EOF Then
       MsgBox "There are no available cars between kl. " & datFrom &
" og kl. " & datTo & " on a " & strDay
       GoTo Exit_btnOK_Click
   End If

   strSQL = "SELECT DISTINCT [Car] FROM [qryTimes] WHERE " &
strCriteria
   Set rstCars = dbs.OpenRecordset(strSQL)
   rstCars.MoveFirst
   Do While Not rstCars.EOF
       datTimeMax = #12:00:00 AM#
       datTimeMin = #11:59:00 PM#
       strTimeMax = ""
       strTimeMin = ""
       strAdrMax = ""
       strAdrMin = ""
       strCar = rstCars![Car]
       rstAvailTimes.Filter = "[Car]= " & strCar
       Set rstAvailCar = rstAvailTimes.OpenRecordset

       With rstAvailCar
           .MoveFirst
           Do While Not .EOF
               datMaxForCar = rstAvailCar![TimeMax]
               datMinForCar = rstAvailCar![TimeMin]
               If datMaxForCar <= datFrom Then
                   If datMaxForCar > datTimeMax Then
                       datTimeMax = datMaxForCar
                       strAdrMax = rstAvailCar![AdrMax]
                   End If
               ElseIf datMinForCar >= datTo Then
                   If datMinForCar < datTimeMin Then
                       datTimeMin = datMinForCar
                       strAdrMin = rstAvailCar![AdrMin]
                   End If
               End If
               .MoveNext
           Loop
       End With 'rstAvailCar

       If datTimeMax <> #12:00:00 AM# Then strTimeMax = datTimeMax
       If datTimeMin <> #11:59:00 PM# Then strTimeMin = datTimeMin

'            Debug.Print strCar & vbTab & strTimeMax & vbtab &
strAdrMax & vbTab & strTimeMin & vbTab & strAdrMin
       rstCars.MoveNext
   Loop

   GoTo Exit_btnOK_Click

Err_btnOK_Click:
   MsgBox Err.Description

Exit_btnOK_Click:
   If Not rstAvailCar Is Nothing Then
       rstAvailCar.Close
       Set rstAvailCar = Nothing
   End If
   If Not rstAvailTimes Is Nothing Then
       rstAvailTimes.Close
       Set rstAvailTimes = Nothing
   End If
   If Not rstCars Is Nothing Then
       rstCars.Close
       Set rstCars = Nothing
   End If
   If Not dbs Is Nothing Then
       dbs.Close
       Set dbs = Nothing
   End If

End Sub
===================

Best regards
Aino
Marshall Barton - 21 Mar 2008 22:09 GMT
>Based on input from a form, I process a query, ending up assigning my
>desired results to variables inside a loop.
>Now I would like to display the results in a report.
[]
>Here is the code:
>===============================
[quoted text clipped - 80 lines]
>        rstCars.MoveNext
>    Loop

Reports provide theit own looping by executing the detail
section's event procedure for each record in the report's
record source.  This can be controled by using the
NextRecord property.  In your kind of unbound report
scenario. you need to open the record set in the report's
open event and the process one recordset record in the
detail section's Format event.  This is kind of tricky and
is very rarely necessary.

From your code, I don't see why you can not construct your
query in the report's Open event and just assign the SQL
statement to the report's Record source property.  Then the
report can be normally without mucking about with
recordsets.

If you can find a way to do it, the recommended approach is
to just construct the final WHERE clause in the form and
then use that in the OpenReport method's WhereCondition
argument.

Signature

Marsh
MVP [MS Access]

Aino - 22 Mar 2008 07:21 GMT
Hi Marsh

The problem is, that in my report I want to combine a subset of the
original query by combining data from different records in each report
line.
The result of the original query is a list of cars, where and when
each of their routes start, where and when there route stops and day
of the week. None of the fields are unique:
Day   Car    TimeMin    AdrMin    TimeMax    AdrMax

After the user has entered a day and a timeinterval, for which he
needs a car, the report should give a list of available cars, when and
where they stop their previous route (Max, if any on that day) and
when and where they should start their next route (Min, if any on that
day). This mean, that the max-data comes from one record, min-data
from another. There can be other data (routes) for the same car and
day, which should be ignored, since they are not relevant for the
given time interval.

I have no idea how to construct an sql or a where-clause, that can
select these data. But since I needed a query, 3 recordsets and some
code to achieve my results, I guess it just shows I am not that
familiar with SQL.

I hope you (or someone else) will take the time to guide me.

Best regards
Aino
Marshall Barton - 23 Mar 2008 20:32 GMT
>The problem is, that in my report I want to combine a subset of the
>original query by combining data from different records in each report
[quoted text clipped - 17 lines]
>code to achieve my results, I guess it just shows I am not that
>familiar with SQL.

After pondering this for a serious length of time, I think I
see where you're coming from and it is a tough problem for
me too.

I think I would first try to arrange an appropriate query
for the report.  The query would rely on a form with a combo
box to select the day and two text boxes for the start and
end times that the car is needed.  The form would also have
a button to open the report.

I think a query along these lines will return the cars that
are available on the specified day and time.  (The subquery
finds cars that are busy and the Left Join and Is Null
criteria excludes them.)

Query GetCars:
SELECT Q.Day, Q.Car, Q.TimeMax, Q.AdrMax,
                                                Q.TimeMin, Q.AdrMin
FROM yourquery As Q LEFT JOIN
            (SELECT X.Car
            FROM yourquery As X
            WHERE X.Day = Forms!theform.txtDay
                    And X.TimeMin < Forms!theform.txtEnd
                    And X.TimeMax > Forms!theform.txtStart
            ) As T
    ON Q.Car = T.Car
WHERE Q.Day = Forms!theform.txtDay
        And T.Car Is Null
        And X.TimeMax < Forms!theform.txtStart

Then, I think another query can then find the data for the
previous and next route:

SELECT Day, Car, TimeMax, AdrMax, TimeMin, AdrMin
FROM GetCars As G
WHERE (TimeMax = (SELECT Max(X.TimeMax
                                            FROM GetCars As X
                                            WHERE X.Car = G.Cars)
                Or TimeMax Is Null)
        And (TimeMin  = (SELECT Min(X.TimeMin
                                            FROM GetCars As X
                                            WHERE X.Car = G.Cars)
                Or TimeMin Is Null)

I worry that with all those subqueries, it might be kind of
slow.

Unfortunately, after I spent a couple of hours setting up
the tables and queries to test all that, Access (2003)
silently closed and I lost all that work.  I will be tied up
for the rest of the day so I will leave it to you to test
this idea and decide if you want to pursue it or go back to
using code to calculate the data (which may be even more
complicated).

Or maybe someone else will get interested in the problem and
jump in with a cleaner/efficient idea.

Signature

Marsh
MVP [MS Access]

Aino - 24 Mar 2008 12:06 GMT
I am grateful that you are taking the time to try to solve this.

> I think a query along these lines will return the cars that
> are available on the specified day and time.  (The subquery
> finds cars that are busy and the Left Join and Is Null
> criteria excludes them.)
> ...

That was the same I was going for with my strCriteria in the code:
strCriteria = "[Day] = '" & strDay & "' AND NOT [Car] IN " _
       & "(SELECT [Car] FROM [qryTimes] WHERE [Day] = '" & strDay _
       & "' AND [TimeMax] > #" & datFrom & "# AND [TimeMin] < #" &
datTo & "#)"
I am not sure though, why you have the last "And X.TimeMax < Forms!
theform.txtStart".

> Then, I think another query can then find the data for the
> previous and next route:
[quoted text clipped - 9 lines]
>                WHERE X.Car = G.Cars)
>                Or TimeMin Is Null)

I am sorry to say, it doesn't work. I do not get all the cars
available. TimeMax and TimeMin are never null in GetCars. And the
times (and adresses) I want returned, are not neccessarily max and min
for the given car on the given day. If for instance the car drives 5
routes that day, and is available in the requested time interval
between route 2 and 3, I would like returned TimeMax and AdrMax for
route 2 (so I know when and where it is available from) and TimeMin
and AdrMin for route 3 (so I know when it is no longer available, and
where it needs to be at that time).

> ...so I will leave it to you to test
> this idea and decide if you want to pursue it or go back to
> using code to calculate the data (which may be even more
> complicated).

Actually the code in my original post gave the correct results, only
since they came out as variables, I could only present them in the
immediate window or a message box.

I am beginning to think that if I can get the results into a none-
editable form by creating an adhoc recordset, maybe I should use this
instead of a report. After all, I can give the form a white background
and set all fields to locked, maybe I can make the user think it is a
report?

> Or maybe someone else will get interested in the problem and
> jump in with a cleaner/efficient idea.

Any help is more than welcome!

Best regards
Aino
Marshall Barton - 24 Mar 2008 21:00 GMT
>I am grateful that you are taking the time to try to solve this.
>
[quoted text clipped - 11 lines]
>I am not sure though, why you have the last "And X.TimeMax < Forms!
>theform.txtStart".

My thought was to also eliminate any records where the
MaxTime was before the start time since they seem like they
are irrelevant to the question.

>> Then, I think another query can then find the data for the
>> previous and next route:
[quoted text clipped - 12 lines]
>I am sorry to say, it doesn't work. I do not get all the cars
>available. TimeMax and TimeMin are never null in GetCars.

You're right, there won't be any Null times, so you can get
rid of those  Or expressions.  Like I said Access went south
before I could test all that stuff so I never got a chance
to test it.  The idea I was working toward is to eliminate
the entries with availablity that does not include the time
requested.

>                                                                              And the
>times (and adresses) I want returned, are not neccessarily max and min
[quoted text clipped - 4 lines]
>and AdrMin for route 3 (so I know when it is no longer available, and
>where it needs to be at that time).

Right.  That was the intent of the
    And X.TimeMax < Forms!theform.txtStart
criteria in the first query.

>> ...so I will leave it to you to test
>> this idea and decide if you want to pursue it or go back to
[quoted text clipped - 10 lines]
>and set all fields to locked, maybe I can make the user think it is a
>report?

If you have the desired recordset, then assigning it to a
form's Recordset property might be all you need.

Signature

Marsh
MVP [MS Access]

Aino - 30 Mar 2008 15:13 GMT
> If you have the desired recordset, then assigning it to a
> form's Recordset property might be all you need.

By now I have given up on this as well.
Since it is a fabricated recordset, it is not possible to use it for a
form.

I have tried setting certain attributes as recommended by Lyle
Fairfield in <http://groups.google.fr/group/comp.databases.ms-access/
msg/ae637cf71a8d8360>.

I have tried going though a stream as described by Florian Esser in
<http://groups.google.fr/group/microsoft.public.access.formscoding/msg/
d59c62f523b1fbbc>

I have tried a sub converting the fabricated recordset to something
looking like a bound recordset, code delivered by Steve Jorgensen in
<http://groups.google.com/group/comp.databases.ms-access/msg/
de67ac684186964e>

All of it to no avail. I am using MS Access 2000. I believed the
difficult part would be to get the right data, I would never have
thought that getting the data presented in something else than a
message box would be the killing part!

It seems I have to construct an SQL to solve this. I just feel very
disillusioned rigth now.

Best regards
Aino
 
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.