473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Suggesting new values based on varying historical values

6
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Set rs = DB.OpenRecordset(Mainsql)
  3.  
  4. hall = 1
  5.  
  6. rs.MoveFirst
  7.  
  8. Do Until rs.EOF
  9.  
  10. hall = 1
  11. rs.FindFirst (Nz(rs![JobID2]) = hall)
  12.  
  13. If rs.NoMatch = True Then
  14.  
  15. Do Until rs.NoMatch = False Or hall = hallmax
  16.  
  17. hall = hall + 1
  18.  
  19. rs.FindFirst (Nz(rs![JobID2]) = hall)
  20.  
  21. Loop
  22.  
  23. Else
  24. rs.MoveNext
  25.  
  26. End If
  27. Loop
  28.  
  29. If rs.NoMatch = True Then
  30.  
  31. Forms!frm_assignments!frm_assignments_subform.Form!Combo8 = hall
  32.  
  33. Else
  34.  
  35. rs.Filter = "DateWorked <= sheetdate -6"
  36. Set rsfilter = rs.OpenRecordset
  37. Forms!frm_assignments!frm_assignments_subform.Form!Combo8 = rsfilter![JobID2]
  38. rs.Close
  39. rsfilter.Close
  40. End If
  41.  
  42. rs2.MoveNext
  43.  
  44. Loop
  45.  
  46. Set rs2 = Nothing
  47. End Sub
  48.  
Jul 6 '11 #1

✓ answered by ADezii

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:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstJobs As DAO.Recordset
  3. Dim strSQL As String
  4. Dim lngEmpID As Long
  5. Dim intNumOfJobs As Integer
  6. Const conMAX_JOBS As Byte = 7
  7.  
  8. lngEmpID = 1        'Unique Employee ID
  9.  
  10. 'Return ALL jobs performed by an Employee (Unique) listed chronologically
  11. 'by Date Descending (latest conMAX_JOBS for each Employee)
  12. strSQL = "SELECT tblJobs.EmpID, tblJobs.JobNum, tblJobs.Date " & _
  13.          "FROM tblJobs WHERE tblJobs.EmpID = " & lngEmpID & _
  14.          " ORDER BY tblJobs.Date DESC;"
  15.  
  16. Set MyDB = CurrentDb
  17. Set rstJobs = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
  18.  
  19. Debug.Print "Emp#", "Job#", "Date", "Sequence"
  20. Debug.Print "-----------------------------------------------------"
  21.  
  22. With rstJobs
  23.   'Loop through the Recordset as long as there are Records and the Number of
  24.   'Records does not exceed the Value of the Constant conMAX_JOBS
  25.   Do While Not .EOF And intNumOfJobs <> conMAX_JOBS
  26.     intNumOfJobs = intNumOfJobs + 1
  27.       'Major processing of Data will occur here
  28.       Debug.Print ![EmpID], ![JobNum], Format$(![Date], "mm/dd/yyyy"), CStr(intNumOfJobs)
  29.         .MoveNext
  30.   Loop
  31. End With
  32.  
  33. rstJobs.Close
  34. Set rstJobs = Nothing
Sample OUTPUT based on Test Data:
Expand|Select|Wrap|Line Numbers
  1. Emp#          Job#          Date          Sequence
  2. -----------------------------------------------------
  3.  1             1            12/21/2011    1
  4.  1             4            09/21/2011    2
  5.  1             5            09/04/2011    3
  6.  1             9            08/18/2011    4
  7.  1             7            07/11/2011    5
  8.  1             8            05/16/2011    6
  9.  1             3            03/30/2011    7

5 1705
ADezii
8,834 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstJobs As DAO.Recordset
  3. Dim strSQL As String
  4. Dim lngEmpID As Long
  5. Dim intNumOfJobs As Integer
  6. Const conMAX_JOBS As Byte = 7
  7.  
  8. lngEmpID = 1        'Unique Employee ID
  9.  
  10. 'Return ALL jobs performed by an Employee (Unique) listed chronologically
  11. 'by Date Descending (latest conMAX_JOBS for each Employee)
  12. strSQL = "SELECT tblJobs.EmpID, tblJobs.JobNum, tblJobs.Date " & _
  13.          "FROM tblJobs WHERE tblJobs.EmpID = " & lngEmpID & _
  14.          " ORDER BY tblJobs.Date DESC;"
  15.  
  16. Set MyDB = CurrentDb
  17. Set rstJobs = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
  18.  
  19. Debug.Print "Emp#", "Job#", "Date", "Sequence"
  20. Debug.Print "-----------------------------------------------------"
  21.  
  22. With rstJobs
  23.   'Loop through the Recordset as long as there are Records and the Number of
  24.   'Records does not exceed the Value of the Constant conMAX_JOBS
  25.   Do While Not .EOF And intNumOfJobs <> conMAX_JOBS
  26.     intNumOfJobs = intNumOfJobs + 1
  27.       'Major processing of Data will occur here
  28.       Debug.Print ![EmpID], ![JobNum], Format$(![Date], "mm/dd/yyyy"), CStr(intNumOfJobs)
  29.         .MoveNext
  30.   Loop
  31. End With
  32.  
  33. rstJobs.Close
  34. Set rstJobs = Nothing
Sample OUTPUT based on Test Data:
Expand|Select|Wrap|Line Numbers
  1. Emp#          Job#          Date          Sequence
  2. -----------------------------------------------------
  3.  1             1            12/21/2011    1
  4.  1             4            09/21/2011    2
  5.  1             5            09/04/2011    3
  6.  1             9            08/18/2011    4
  7.  1             7            07/11/2011    5
  8.  1             8            05/16/2011    6
  9.  1             3            03/30/2011    7
Jul 6 '11 #2
Rabbit
12,516 Expert Mod 8TB
Try something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 7 J.JobName, E.JobDate
  2. FROM tblAvailableJobs AS J
  3.    LEFT JOIN tblEmpJobs AS E
  4.    ON J.JobID = E.JobID
  5. WHERE EmpID = 1234
  6. 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.
Jul 6 '11 #3
Hembd
6
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!

Expand|Select|Wrap|Line Numbers
  1. Dim int1st As Integer
  2. Dim int2nd As Integer
  3. Dim intShift As String
  4. Dim intShiftNum As Integer
  5.  
  6. int1st = rstForm![1stshift]
  7. int2nd = rstForm![2ndshift]
  8.  
  9. If int1st = -1 Then
  10.  
  11. intShift = "1stshift"
  12. intShiftNum = int1st
  13.  
  14. Else
  15.  
  16.     If int2nd = -1 Then
  17.  
  18.     intShift = "2ndshift"
  19.     intShiftNum = int2nd
  20.  
  21.     End If
  22. End If
  23.  
  24.   If intNumOfJobs = conMAX_JOBS And inthall < 10 Then
  25.  
  26.   strSQL2 = "SELECT tbl_assignments.AssociateID, tbl_assignments.JobID2, tbl_assignments.JobID1, tbl_assignments.DateWorked " & _
  27.         "FROM tbl_assignments WHERE tbl_assignments.DateWorked = #" & sheetdate & "#" & _
  28.         " AND tbl_assignments." & intShift & " = " & intShiftNum & _
  29.          " ORDER BY tbl_assignments.DateWorked DESC;"
  30.  
  31.  
  32.  
Jul 9 '11 #4
ADezii
8,834 Expert 8TB
" AND tbl_assignments." & intShift & " = " & intShiftNum &
You cannot reference Field Names in an SQL Statement as you have attempted.
Jul 9 '11 #5
Hembd
6
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!
Jul 10 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
0
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>>
0
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...
6
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...
2
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...
1
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....
7
convexcube
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...
7
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...
3
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...
7
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.