I am having a problem with an employee roster i've created. Each employee can be assigned any of 9 jobs. The database keeps a history of the jobs employees have performed. I am attempting to have the DB suggest jobs for the current day, based on the job history. I need it to take into account jobs they have not performed within the week, or if all jobs have been performed, the one not performed within the longest span.
The form is set up, so that once I have entered the employees working for the day, I would click the button and it would loop through employees, suggesting jobs.
Basically "Mainsql" is supposed to return the last 7 jobs an employee was assigned. Then the rest of the code is supposed to check that as a recordset(rs) for the requirements and assign the new value.
It is then supposed to loop through the subform (rs2) performing the above task.
I'm going to keep working on it, but all this trial and error is giving a headache. Right now the code returns incorrect information, and never touches the code past line 31.
Maybe someone can offer some comments on the code and point me in the right direction? I'll give table information in the next post, I just don't want to make this one post too long (a bad habit of mine). Maybe someone has a database that does this kind of thing that I can examine.
Here is the code i'm using for the looping part: It looks really ugly, my apologies.
I'm sorry the code is more than 20 lines.
I'd appreciate any help I receive. -
-
Set rs = DB.OpenRecordset(Mainsql)
-
-
hall = 1
-
-
rs.MoveFirst
-
-
Do Until rs.EOF
-
-
hall = 1
-
rs.FindFirst (Nz(rs![JobID2]) = hall)
-
-
If rs.NoMatch = True Then
-
-
Do Until rs.NoMatch = False Or hall = hallmax
-
-
hall = hall + 1
-
-
rs.FindFirst (Nz(rs![JobID2]) = hall)
-
-
Loop
-
-
Else
-
rs.MoveNext
-
-
End If
-
Loop
-
-
If rs.NoMatch = True Then
-
-
Forms!frm_assignments!frm_assignments_subform.Form!Combo8 = hall
-
-
Else
-
-
rs.Filter = "DateWorked <= sheetdate -6"
-
Set rsfilter = rs.OpenRecordset
-
Forms!frm_assignments!frm_assignments_subform.Form!Combo8 = rsfilter![JobID2]
-
rs.Close
-
rsfilter.Close
-
End If
-
-
rs2.MoveNext
-
-
Loop
-
-
Set rs2 = Nothing
-
End Sub
-
The Key, as I see it, is to accurately populate a Recordset will all Jobs performed by a specific Employee by Date Descending, up to a MAXIMUM Number of Jobs as defined by a CONSTANT, as in: - Dim MyDB As DAO.Database
-
Dim rstJobs As DAO.Recordset
-
Dim strSQL As String
-
Dim lngEmpID As Long
-
Dim intNumOfJobs As Integer
-
Const conMAX_JOBS As Byte = 7
-
-
lngEmpID = 1 'Unique Employee ID
-
-
'Return ALL jobs performed by an Employee (Unique) listed chronologically
-
'by Date Descending (latest conMAX_JOBS for each Employee)
-
strSQL = "SELECT tblJobs.EmpID, tblJobs.JobNum, tblJobs.Date " & _
-
"FROM tblJobs WHERE tblJobs.EmpID = " & lngEmpID & _
-
" ORDER BY tblJobs.Date DESC;"
-
-
Set MyDB = CurrentDb
-
Set rstJobs = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
-
-
Debug.Print "Emp#", "Job#", "Date", "Sequence"
-
Debug.Print "-----------------------------------------------------"
-
-
With rstJobs
-
'Loop through the Recordset as long as there are Records and the Number of
-
'Records does not exceed the Value of the Constant conMAX_JOBS
-
Do While Not .EOF And intNumOfJobs <> conMAX_JOBS
-
intNumOfJobs = intNumOfJobs + 1
-
'Major processing of Data will occur here
-
Debug.Print ![EmpID], ![JobNum], Format$(![Date], "mm/dd/yyyy"), CStr(intNumOfJobs)
-
.MoveNext
-
Loop
-
End With
-
-
rstJobs.Close
-
Set rstJobs = Nothing
Sample OUTPUT based on Test Data: - Emp# Job# Date Sequence
-
-----------------------------------------------------
-
1 1 12/21/2011 1
-
1 4 09/21/2011 2
-
1 5 09/04/2011 3
-
1 9 08/18/2011 4
-
1 7 07/11/2011 5
-
1 8 05/16/2011 6
-
1 3 03/30/2011 7
5 1705
The Key, as I see it, is to accurately populate a Recordset will all Jobs performed by a specific Employee by Date Descending, up to a MAXIMUM Number of Jobs as defined by a CONSTANT, as in: - Dim MyDB As DAO.Database
-
Dim rstJobs As DAO.Recordset
-
Dim strSQL As String
-
Dim lngEmpID As Long
-
Dim intNumOfJobs As Integer
-
Const conMAX_JOBS As Byte = 7
-
-
lngEmpID = 1 'Unique Employee ID
-
-
'Return ALL jobs performed by an Employee (Unique) listed chronologically
-
'by Date Descending (latest conMAX_JOBS for each Employee)
-
strSQL = "SELECT tblJobs.EmpID, tblJobs.JobNum, tblJobs.Date " & _
-
"FROM tblJobs WHERE tblJobs.EmpID = " & lngEmpID & _
-
" ORDER BY tblJobs.Date DESC;"
-
-
Set MyDB = CurrentDb
-
Set rstJobs = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
-
-
Debug.Print "Emp#", "Job#", "Date", "Sequence"
-
Debug.Print "-----------------------------------------------------"
-
-
With rstJobs
-
'Loop through the Recordset as long as there are Records and the Number of
-
'Records does not exceed the Value of the Constant conMAX_JOBS
-
Do While Not .EOF And intNumOfJobs <> conMAX_JOBS
-
intNumOfJobs = intNumOfJobs + 1
-
'Major processing of Data will occur here
-
Debug.Print ![EmpID], ![JobNum], Format$(![Date], "mm/dd/yyyy"), CStr(intNumOfJobs)
-
.MoveNext
-
Loop
-
End With
-
-
rstJobs.Close
-
Set rstJobs = Nothing
Sample OUTPUT based on Test Data: - Emp# Job# Date Sequence
-
-----------------------------------------------------
-
1 1 12/21/2011 1
-
1 4 09/21/2011 2
-
1 5 09/04/2011 3
-
1 9 08/18/2011 4
-
1 7 07/11/2011 5
-
1 8 05/16/2011 6
-
1 3 03/30/2011 7
Try something like this: - SELECT TOP 7 J.JobName, E.JobDate
-
FROM tblAvailableJobs AS J
-
LEFT JOIN tblEmpJobs AS E
-
ON J.JobID = E.JobID
-
WHERE EmpID = 1234
-
ORDER BY Nz(E.JobDate, #1/1/1900#) ASC;
You may have to fiddle around with first getting distinct jobs with max date for the employee first. But this is a proof of concept.
ADezii and Rabbit, thank you for your responses. You guys were right, the key is in populating the recordset correctly. From what you gave me I was able to finish the project 99% of the way. However, Now there is another problem, but it seems to just be a syntax issue. Here are snippets of the new code i'm using. In strSQL2, there seems to be a problem with my formatting using the variables "intshift" and "intshiftnum". I keep thinking it is "intshiftnum" but no matter what I change I still get the syntax error when I try to run it.
From what you all gave me, the code grew pretty complex (for me), I don't think i've ever hit F8 so many times(to step through the code).
At any rate, your help is appreciated. On a side note, do you guys have any good resources either online or book form about code formatting? Everyone else makes theirs look good, while mine is a headache to look through. I'd love to make it easier for whoever has to mess with my code later.
Thank you! -
Dim int1st As Integer
-
Dim int2nd As Integer
-
Dim intShift As String
-
Dim intShiftNum As Integer
-
-
int1st = rstForm![1stshift]
-
int2nd = rstForm![2ndshift]
-
-
If int1st = -1 Then
-
-
intShift = "1stshift"
-
intShiftNum = int1st
-
-
Else
-
-
If int2nd = -1 Then
-
-
intShift = "2ndshift"
-
intShiftNum = int2nd
-
-
End If
-
End If
-
-
If intNumOfJobs = conMAX_JOBS And inthall < 10 Then
-
-
strSQL2 = "SELECT tbl_assignments.AssociateID, tbl_assignments.JobID2, tbl_assignments.JobID1, tbl_assignments.DateWorked " & _
-
"FROM tbl_assignments WHERE tbl_assignments.DateWorked = #" & sheetdate & "#" & _
-
" AND tbl_assignments." & intShift & " = " & intShiftNum & _
-
" ORDER BY tbl_assignments.DateWorked DESC;"
-
-
-
" AND tbl_assignments." & intShift & " = " & intShiftNum &
You cannot reference Field Names in an SQL Statement as you have attempted.
It would appear that I cannot, thanks for helping me see the issue. I just wanted to avoid extra if statements. I was able to circumvent the issue and complete the project.
Thank you for your help!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jim in Arizona |
last post by:
This doesn't make any sense to me. I'm hoping some SQL guru out there knows
the answer.
I'm making a help desk type database system. There's only two tables, one
called TTickets and the other...
|
by: xmldig |
last post by:
dot net 2.0
I have a gridview and I want to add a seperator line between rows of
varying col1 values... Here is an example.
col1 col 2
1 A
1 B
1 C
<<INSERT SEPERATOR BAR HERE>>
|
by: Materialised |
last post by:
Hi All
I have been searching the net on this for the last couple of hours to no
avail.
I need to fill a datagridview control based on the values entered in 2 text
boxes.
However I cant for...
|
by: Materialised |
last post by:
Hi All
I have been searching the net on this for the last couple of hours to no
avail.
I need to fill a datagridview control based on the values entered in 2 text
boxes.
However I cant for...
|
by: igendreau |
last post by:
I need to have a form where my users can enter job info. One thing
they need to enter is "Regular Hours". What I need to do is then
calculate "Regular Cost" which =Regular Hours x Regular Rate...
|
by: dmeyr |
last post by:
Hello,
I am new to Access and am having difficulty with a Dlookup function. I have a form that I wish to autopopulate 10 fields with values based on two criteria which are also fields on the form....
|
by: convexcube |
last post by:
To keep a record of training levels for different tasks, I have 18 option groups with 4 options values each: 0 labelled as "None", 1 labelled as "Trainee", 2 labelled as "Competent" and 3 labelled as...
|
by: servantofone |
last post by:
I'm using Access 2003. I'm building a query and wish to display all records with values in a certain field (HIST) made up of all zeros. The field has a range of text values including alpha-numeric...
|
by: nassim.bouayad.agha |
last post by:
Hello,
I am seeking for information about conditional XSD validation based on
elements values,but unfortunatly,I could not find a clear answer.Let's
say that I have three XML elements named...
|
by: LSGKelly |
last post by:
I am working on a query that I would like to get the top 4 values based on a group. Here is the SQL:
SELECT qRenPercRept1.CountOfccName, qRenPercRept1.Merged, qRenPercRept1.ccName,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |