Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Access Query to Get Values

I have an old access 97 database that has multiple fields per record.

Client1_EntryDate
Client2_EntryDate
Client3_EntryDate
Earliest_EntryDate

I want to run a query that can look at the 3 columns, Client1_EntryDate, Client2_EntryDate and Client3_EntryDate then get me the earliest date out of the 3 so that I can update it into the field Earliest_EntryDate field.

so for eg,  if Record 1 had

Client1_EntryDate = 12/07/2001
Client2_EntryDate = 18/01/1998
Client3_EntryDate = 15/03/2005

then I want to update Record1 Earliest_EntryDate with the value of 18/01/1998

This is 1 query that will give me the earliest values. Then I will also want to run a similar query that updates another field but gives me the latest.

Client1_LastDate
Client2_LastDate
Client3_LastDate
Latest_EntryDate

This will opposed to looking at the earliest date will update the record with the latest date.
We are only looking within the record as all fields required to lookup reside within a record.  Please also note that its possible some values will be null which is that case the value will be ignored.

Eg

Client1_EntryDate = 12/07/2001
Client2_EntryDate =  Blank
Client3_EntryDate = 15/03/2005

then I want to update Earliest_EntryDate with the value of 12/07/2001
0
GenieMaster
Asked:
GenieMaster
  • 2
1 Solution
 
Dale FyeCommented:
I use a function for this.  It accepts as many parameters of whatever type you want to compare and will return the minimum value within that set:

What you should be able to do is:

Update yourTable Set Earliest_Entry = fnMin([Client1_EntryDate], [Client2_EntryDate], [Client3_EntryDate])

you might want to add:

WHERE [Earliest_Entry] IS NULL

Public Function fnMin(ParamArray ValList() As Variant) As Variant

   Dim intLoop As Integer
   Dim myVal As Variant
   
   For intLoop = LBound(ValList) To UBound(ValList)
      If Not IsNull(ValList(intLoop)) Then
         If IsEmpty(myVal) Then
            myVal = ValList(intLoop)
         ElseIf ValList(intLoop) < myVal Then
            myVal = ValList(intLoop)
         End If
      End If
   Next
   fnMin = myVal
   
End Function

Open in new window

0
 
GenieMasterAuthor Commented:
Perfect, Just what I needed. Thanks :)
0
 
Dale FyeCommented:
Glad to help.

BTW, really easy to modify that to give you the Max values.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now