PC Review


Reply
 
 
=?Utf-8?B?Q2h1Y2syMTY=?=
Guest
Posts: n/a
 
      21st May 2007
Hi

I have the following code behind one of my forms to automatically fill in
the LastName, FirstName & Division when a hang tag number is typed into the
form. The data is retrieved from tblName which also has a “year” field in it.
Since tblName can contain many years worth of data I would like to only fill
the form with the current years data. I don’t know how to add the code to
only retrieve the data if the year field is equal to the current year .I hope
I’m making my question understandable ( sometimes I confuse myself ) any
help with this will be greatly appreciated .


Private Sub HangTag_AfterUpdate()
On Error GoTo Err_HangTag_AfterUpdate


Dim varX As Variant
Dim varY As Variant
Dim varZ As Variant

varX = DLookup("LastName", "tblName", "HangTag ='" & Me.HangTag & "'")
varY = DLookup("FirstName", "tblName", "HangTag ='" & Me.HangTag & "'")
varZ = DLookup("Division", "tblName", "HangTag ='" & Me.HangTag & "'")
Me.LastName = varX
Me.FirstName = varY
Me.Division = varZ

Exit_HangTag_AfterUpdate:
Exit Sub

Err_HangTag_AfterUpdate:
MsgBox Err.Description
Resume Exit_HangTag_AfterUpdate

End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?T2ZlciBDb2hlbg==?=
Guest
Posts: n/a
 
      21st May 2007
Every time you run the DlookUp is searching the table, so instead of running
dlookup 3 times for each field, open the table once with RecordSet

Something like

Private Sub HangTag_AfterUpdate()
On Error GoTo Err_HangTag_AfterUpdate


Dim MyDb as Dao.DataBase, MyRec As RecordSet
Set MyDb=CurrentDb
Set MyRec=MyDb.OpenRecordSet("Select * From TableNAme Where HangTag ='" &
Me.HangTag & "' And [Year] =" & Year(Date()))
If Not MyRec.Eof Then
Me.LastName = MyRec!LastName
Me.FirstName = MyRec!FirstName
Me.Division = MyRec!Division
End If

Exit_HangTag_AfterUpdate:
Exit Sub

Err_HangTag_AfterUpdate:
MsgBox Err.Description
Resume Exit_HangTag_AfterUpdate

End Sub

--
Good Luck
BS"D


"Chuck216" wrote:

> Hi
>
> I have the following code behind one of my forms to automatically fill in
> the LastName, FirstName & Division when a hang tag number is typed into the
> form. The data is retrieved from tblName which also has a “year” field in it.
> Since tblName can contain many years worth of data I would like to only fill
> the form with the current years data. I don’t know how to add the code to
> only retrieve the data if the year field is equal to the current year .I hope
> I’m making my question understandable ( sometimes I confuse myself ) any
> help with this will be greatly appreciated .
>
>
> Private Sub HangTag_AfterUpdate()
> On Error GoTo Err_HangTag_AfterUpdate
>
>
> Dim varX As Variant
> Dim varY As Variant
> Dim varZ As Variant
>
> varX = DLookup("LastName", "tblName", "HangTag ='" & Me.HangTag & "'")
> varY = DLookup("FirstName", "tblName", "HangTag ='" & Me.HangTag & "'")
> varZ = DLookup("Division", "tblName", "HangTag ='" & Me.HangTag & "'")
> Me.LastName = varX
> Me.FirstName = varY
> Me.Division = varZ
>
> Exit_HangTag_AfterUpdate:
> Exit Sub
>
> Err_HangTag_AfterUpdate:
> MsgBox Err.Description
> Resume Exit_HangTag_AfterUpdate
>
> End Sub
>

 
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
Function to lookup date on tab in excel and populate date on calen MGC Microsoft Excel Worksheet Functions 0 4th Feb 2010 04:48 AM
RE: Date LookUp Barb Reinhardt Microsoft Excel Worksheet Functions 0 3rd Oct 2008 12:06 AM
Re: Lookup MIN Date Pete_UK Microsoft Excel Worksheet Functions 0 25th Jul 2008 08:10 PM
date lookup on two column date range dim1ann@yahoo.com Microsoft Excel Programming 3 27th Oct 2005 07:26 PM
lookup a date from an array of date ranges if conditions are met nscanceran Microsoft Excel Misc 2 8th Nov 2003 03:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:21 PM.