dbas to access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am moving from dbas to access. I have 2 tables - todays runners and past
results. identical structures. the results table may have numerous lines of
form for each of todays runners. I only want to return the most recent 4
formlines.
the following dbas.prg works ok.
select 3
use temp
select 2
use results index rname
select 1
use runners
do while .not. eof()
store name to aname
select 2
seek aname
copy next 4 to temp2 while name = aname
select 3
use temp ( new file with max of 4 formlines for any runner )
append from temp2
select 1
skip
enddo

if any one could help me achieve the same result in access i would be most
grateful
with queries I'm getting all results. with modules I don't know how
to work of 2 tables.
 
I am moving from dbas to access. I have 2 tables - todays runners and
past results. identical structures. the results table may have
numerous lines of form for each of todays runners. I only want to
return the most recent 4 formlines.

the following dbas.prg works ok.

select 3
use temp
select 2
use results index rname
select 1
use runners
do while .not. eof()
store name to aname
select 2
seek aname
copy next 4 to temp2 while name = aname
select 3
use temp
(new file with max of 4 formlines for any runner)
append from temp2
select 1
skip
enddo

if any one could help me achieve the same result in access i would be
most grateful with queries I'm getting all results. with modules I
don't know how to work of 2 tables.

Hi Mauken,

I think this article is what you are looking for:

http://support.microsoft.com/default.aspx?scid=kb;en-us;153747

Post back if you need additional assistance.

HTH
 
It is not clear if you are creating this temp data for reporting purposes.
If it is, then you don't need any code at all, but just build a query in the
query builder.

Further, the fact that you have runners, and past runners hints that these
two tables should in fact be one table, but with the addition of a field
that
sets if they are active. Even better would be to have a runners table, and
then a table of results for each runner (that way, only ONE instance
of the runners name would exist in the database).

Further, I notice that you are doing the relation via aname

What will happen if you have two John Smiths here? Using a text name to
build this relation, and fish up past data is a not a good idea.

As mentioned, it is not at all clear if you need this data transferred to a
temp table for reporting, or for some other needs.

Last, but not least, trying to take the exact code and logic from a non
relational system and use that SAME approach when you got a FABULOUS
relational database engine is NOT the way to go. In other words, you need to
change your approach here. A small change in your approach will eliminate
the need to write ANY of this code.

I not trying to rain on you here, but you need to start thinking in terms of
a relational system.

Further, the fact that your code grab 4 records assumes some order of data.
This could be assumed in dbaseIII etc, but it can NOT be assumed in modern
database
systems. That means if you add 4 records to a table, and then ask the data
engine to return those record,

THEY DO NOT NECESSARY COME BACK IN THE SAME
ORDER. I am not going to repeat that last statement, but read it again! It
is
a VERY important consideration when using new database systems, be they the
JET engine in ms-access, or a company wide server running oracle that order
is NOT preserved during data writes. This is conceptual change in our
industry
we had to make since products like dbase/FoxPro/clipper etc. were
popular.

So, really, using the EXACT same approach as dbase here is not so good
(however, for the sake of learning..I will post a solution in code).

Having said the above, I going to assume that you also have added a id
column (autonumber) to all your tables.

Also, looking at that dbase code, I don't see where table temp2 is cleared
after each use. I did a good stint in FoxPro for dos, but that was 13 years.
ago, so my dbase skills are a bit rusty.

And, in fact, you likely can built a query that returns the results,a nd not
use ANY code.

further, use of temp tables in ms-access is frowned upon. In dbaseIII etc,
each table is in fact a separate file. In ms-access, that table is created
in one file. This means that when you delete the table, the disk space is
not re-claimed until do a compact an repair. So, another new rule for
ms-access developers is to avoid temp tables like the plague.

So, really, we could just fire up the report builder, and build report
that gives us the results we want.

However, having said as much as is gentlemanlike like here on this
issue, lets just go ahead and write the code anyway....

we will have a table called tblRunners, tblHistory, and temp1 will wind up
with the ending data.

Sub CopyToTemp()

Dim rstRunners As DAO.Recordset
Dim strSql As String

' make our temp1 table
On Error Resume Next
CurrentDb.Execute "drop table temp1", dbFailOnError
On Error GoTo 0
CurrentDb.Execute "SELECT * INTO temp1 FROM tblRunners where id = 0;"


Set rstRunners = CurrentDb.OpenRecordset("tblRunners")

Do While rstRunners.EOF = False

strSql = "insert into temp1 " & _
"select top 4 * from tblHistory where Aname = '" & _
rstRunners!Aname & "' order by id DESC"

CurrentDb.Execute strSql

rstRunners.MoveNext
Loop

rstRunners.Close
Set rstRunners = Nothing

End Sub
 
by the way:

strSql = "insert into temp1 " & _
"select top 4 * from tblHistory where Aname = '" & _
rstRunners!Aname & "' order by id DESC"

the above will return the last 4 history items. Your dbase code as written
returns the first 4.
So, you could drop the DESC in the above to return the first 4...

strSql = "insert into temp1 " & _
"select top 4 * from tblHistory where Aname = '" & _
rstRunners!Aname & "' order by id"
 
many thanks for your prompt reply. You are right I have to stop thinking in
dbas. the code I enclosed works ok. copy next 4 overites previous temp2 file.
temp is just a holding file. aname is a temp variable.
Your code is a bit different to anything I have written so far but I am sure
it will do what I want. So thanks again.
 
the code you gave me to get the four most recent records for any horse works
perfect except when the name includes an apostophy, then it reports syntax
error, missing operator, a few names include 2 apostophies - gone'n'dunnit
for examply.
I hope there is an easy fix.
 
Back
Top