Frustrating Runtime error access 3021 (1 Viewer)

Kookie22

New member
Local time
Today, 09:19
Joined
Dec 22, 2011
Messages
1
Hey guys..

As of Monday a access document started producing a runtime error 3021.
I've tried googling how to fix this but can't figure out how to do it and have spent quite a few hours on this..

wondering if someone would be nice enough to lend some help.

When I run it in debug mode it references the rst.MoveFirst as the error.

here is the code:





msg = "Your new Schedule Year is: " & sched_year & Chr(10)
msg = msg & "The new Pay Period is: " & sched_period & Chr(10) & Chr(10)
msg = msg & "Click OK to create this schedule for" & Chr(10)
msg = msg & "all of your employees or Click CANCEL."

If MsgBox(msg, vbOKCancel) = vbCancel Then
Exit Sub
End If

str = "Select * From [Pay Period Dates] Where [Year_ID]=" & sched_year & " and [Pay Period]=" & sched_period
Set rst = dbs.OpenRecordset(str)

rst.MoveFirst
rst.MoveLast

If rst.RecordCount = 1 Then

str = "Select * From [Employees] Where [DEP_ID]='" & Forms![Login Screen]![Department] & "' and [Status]<>'" & "Terminated" & "'"
Set emp = dbs.OpenRecordset(str)
Set post = dbs.OpenRecordset("Employees Hours")

If emp.RecordCount = 0 Then
msg = "No Employess found in database!"
MsgBox msg, vbCritical, "Auto Create New Pay Period"
Exit Sub
End If

emp.MoveLast
emp.MoveFirst

Do Until emp.EOF

sched_date = rst![Start Date]
End_Date = rst![End Date]
sched_week = 0

Do Until sched_date > End_Date

post.AddNew
--

Thanks so much for your time :)
 

boblarson

Smeghead
Local time
Today, 09:19
Joined
Jan 12, 2001
Messages
32,059
Sounds like there are no records being returned for the criteria and so you can't move first if there is nothing. Also, just an FYI - when a recordset is opened, it is ALWAYS at the first so you do not need to use rst.MoveFirst before the rst.MoveLast but you may need to check for records first -

If rst.RecordCount > 0 Then

(you can do that without moving first because if there are no records it will be 0 but if there are it will at least be 1 even if you haven't moved last yet).
 

boblarson

Smeghead
Local time
Today, 09:19
Joined
Jan 12, 2001
Messages
32,059
Hmm, I was a bit slow on that response.

 

Users who are viewing this thread

Top Bottom