I think you are trying to put the cart slightly before the horse here, but sometimes it's difficult to communicate properly via a forum.
First, you want to find out if the user has any milestones in your query. Depending on the results of that inquiry, then you use the Staff ID.
Here is a guess at what it might look like, but you will probably have to clean it up to suit your exact situation.
- Private Sub Form_Load()
-
On Error GoTo EH
-
Dim db As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim strSQL As String
-
Dim TheDate As Date
-
Dim lngStaff As Long
-
Dim fMilestone As Boolean
-
-
lngStaff = Me.ID_Staff
-
fMilestone = Nz(DLookup("ID_Staff", "qryvw_milestones", _
-
"ID_Staff = " & lngStaff), False)
-
-
If fMilestone Then
-
strSQL = "SELECT max(Milestone_Date) as Dte " & _
-
"FROM qryvw_milestones " & _
-
"WHERE ((Milestone_Date<=now()) " & _
-
"AND (ID_staff = " & lngStaff & "))"
-
Set db = CurrentDb
-
Set rst = db.OpenRecordset(strSQL)
-
With rst
-
If Not .RecordCount = 0 Then
-
TheDate = .Fields(0)
-
Me.Milestone_Date.SetFocus
-
DoCmd.FindRecord TheDate
-
Me.Milestone_Date.SetFocus
-
End If
-
.Close
-
End With
-
db.Close
-
Set rst = Nothing
-
Set db = Nothing
-
Else
-
With Me!frmvw_milestones.Form
-
.Filter = "ID_Staff = 0"
-
.FilterOn = True
-
End With
-
End If
-
-
Exit Sub
-
EH:
-
MsgBox "There was an error loading the form!" & _
-
vbCrLf & vbCrLf & _
-
"Number: " & Err.Number & vbCrLf & _
-
"Desc.: " & Err.Description & vbCrLf & vbCrLf & _
-
"Please contact your Database Administrator.", _
-
vbCritical, "WARNING!"
-
Exit Sub
-
End Sub
Notice lines 8-11. See how we have identified the employee, then we use that to look up that employee's milestones. If there are none assigned to that employee, the
fMilestone
flag is set to false. Then, we use that result to figure out what to do. If the mployee has milestones (and you can modify the
DLookup
criteria to match whatever you need to suit your purposes), then we go to that particular record. If not, then we filter the subform to show no records.
A couple additional thoughts (to help you out in the long-term, bigger scheme of things).
First, when posting your code, try, if at all possible, to post the entire procedure. Unless the procedure is huge and you are only having issues with a tiny portion of it. Yours is small enough that seeing the whole procedure is helpful. And
always do a fresh copy and paste directly from your VBA editor.
Second, I have added "Error Handling" to your code (the
On Error GoTo EH
at the beginning of your code and
EH
at the end of your Code). This is a good habit to get into with all of your procedures (I am a notorious "harpy" about error handling on this forum, but I can't over emphasize the need for it). This keeps your code from "breaking" or simply dying in the middle of a user's session.
I've made some changes with how you manage your recordset. Notice lines 28 and 30-32. You should always close all your recordsets and DBs, and then set them to nothing. This frees up computer resources and can prevent other problems within your DB engine. Always another good habit to get into, not to mention that having extraneous declared objects open can cause the DB to sometimes get confused.
Finally, notice line 22. Even though you may
KNOW that you will have at least one record in your recordset (and probably in your case only one record), it is also another good habit to check to see if there are any records before you start using the recordset's values. failing to do so can cause an error in those rare cases when there is no record.
Probably more than you wanted at this time, but these principles will help you along the way no mater how far you go with your Access/VBA projects.
Let me know how these changes worked out.