PC Review


Reply
Thread Tools Rate Thread

Criteria in expression builder

 
 
Dean Collins
Guest
Posts: n/a
 
      22nd Sep 2003
Hi,

I have a Form that is populated with a query. There ia a
text box representing each Year below.

-------------------------
2000 2001 2002 2003

------------------------------
My query produces the data in the following format:

Dept, Year, Number
ABT 2000 435
ABT 2001 420
ABT 2002 209
ALV 2000 89
ALV 2001 109
ALV 2003 106

Is there an expression that can be used to populate each
field using a Dept and Year criteria in the control
source of each text box? without a criteria the first
data in the query is chosen. If this is not possible then
can some one show me an alternative way to achieve the
same results.

many thanks
Dean Collins


 
Reply With Quote
 
 
 
 
Tim Ferguson
Guest
Posts: n/a
 
      22nd Sep 2003
"Dean Collins" <(E-Mail Removed)> wrote in
news:099901c380f4$876ba460$(E-Mail Removed):

> Is there an expression that can be used to populate each
> field using a Dept and Year criteria in the control
> source of each text box?


=DLookUp("[Number]","MyTable","Dept=""ABT"" and [Year]=2000")

etc... By the way, Number and Year are really inadvisable names for fields
because, although being legal, they are also used as SQL key words and may
cause bugs that can be very hard to track.

> If this is not possible then
> can some one show me an alternative way to achieve the
> same results.
>

It is possible, but it may be more efficient to intercept the On Current
event and get all the values in one go:

Private Sub Form_Current()
' editors note: this is untested air code: treat with caution!!

' objects
Dim db as Database
Dim strSQL as String
Dim rs as Recordset

' create the query: note the projection and ordering
strSQL = "SELECT [Year], [Number] FROM MyTable " & _
"WHERE Dept = """ & Me!txtDept & """ " & _
" AND [Year] IN "2000,2001,2002,2003) " & _
"ORDER BY [Year];"

' open the query: forward snapshots are fastest
Set db=CurrentDB()
Set rs=db.OpenRecordset(strSQL, dbOpenSnapshot, dbForwardOnly)

' read the records; omit loop if there weren't any
Do While Not rs.EOF
' put the data in the controls
' I am assuming the controls are txt2000, txt2001, etc.
' you may want to put in some formatting here too
Me.Controls("txt" & rs![Year]).Value = rs![Number]

' get the next record
rs.MoveNext

Loop

' all done: tidy up
rs.Close

End Sub


Hope that makes some kind of sense

Tim F

 
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
Expression Builder Criteria Jaz Microsoft Access 1 27th Mar 2007 09:25 PM
Expression Builder available in Code Builder? =?Utf-8?B?RnJhbms3Nw==?= Microsoft Access VBA Modules 3 10th Mar 2005 07:37 PM
expression builder erased my expression =?Utf-8?B?U3RlZmFubw==?= Microsoft Access 1 6th Dec 2004 10:17 PM
criteria in expression builder Dean Collins Microsoft Access Queries 1 22nd Sep 2003 09:06 PM
criteria in expression builder Dean Collins Microsoft Access Form Coding 0 22nd Sep 2003 11:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:37 AM.