Connecting Tech Pros Worldwide Help | Site Map

given an sql select query, how to get the data

  #1  
Old November 13th, 2005, 11:03 AM
enrio@online.no
Guest
 
Posts: n/a
I feel quite dumb having to ask this, but I cannot seem to find this in
the Microsft documentation pages.

In Access 2000, I create a string containing the sql query:

Dim MaxSerial As Integer
Dim Longyear As Integer
Longyear = year(Date)

Query = "select max(serialnumber) from participants " _
+ "where startyear = " + Str(Longyear)

I would like to have the result of this query in a basic variable.

MaxSerial = ????

I do not, in principle, want to create a form or window showing a table
of records, I just want to get a number in the variable, or NULL if
there is no row in 'participants' having non-null 'startyear'.

Thanks,
Enrique

  #2  
Old November 13th, 2005, 11:04 AM
Tom van Stiphout
Guest
 
Posts: n/a

re: given an sql select query, how to get the data


On 18 May 2005 04:22:47 -0700, "enrio@online.no" <enrio@online.no>
wrote:

Option 1: Use the DMax function. 1-line solution, but perhaps not very
fast for large (> 1000 rows) data sets.

Option 2: Use VBA code with DAO to capture the results in a recordset,
then read the value into your variable:
dim db as dao.database
dim rs as dao.recordset
set db = currentdb()
set rs = db.OpenRecordset(Query, dbOpenSnapshot)
MaxSerial = rs(0)
rs.close ' etc.

Option 3: Use a stored query, with a parameter.
The query:
select max(serialnumber) from participants where startyear =
[parStartYear]
The code:
dim db as dao.database
dim rs as dao.recordset
dim qd as dao.querydefs
set db = currentdb()
set qd = db.querydefs("MyQuery")
qd!parStartYear = Longyear
set rs = qd.OpenRecordset(dbOpenSnapshot)
MaxSerial = rs(0)
rs.close ' etc.

-Tom.

[color=blue]
>I feel quite dumb having to ask this, but I cannot seem to find this in
>the Microsft documentation pages.
>
>In Access 2000, I create a string containing the sql query:
>
> Dim MaxSerial As Integer
> Dim Longyear As Integer
> Longyear = year(Date)
>
> Query = "select max(serialnumber) from participants " _
> + "where startyear = " + Str(Longyear)
>
>I would like to have the result of this query in a basic variable.
>
> MaxSerial = ????
>
>I do not, in principle, want to create a form or window showing a table
>of records, I just want to get a number in the variable, or NULL if
>there is no row in 'participants' having non-null 'startyear'.
>
>Thanks,
>Enrique[/color]

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Query to transpose data from rows into columns Haas C answers 4 August 29th, 2008 05:05 AM
SQL query - duplicate records - different dates - how to get only latest information? clare at snyder.on.ca answers 24 March 25th, 2006 12:55 AM
Recordset to update queries - help! Dom Boyce answers 9 November 12th, 2005 07:02 PM
SQL query help please Bruce W...1 answers 14 July 20th, 2005 12:32 AM