问题
I am facing a huge problem! I am a police officer in a central police department in Athens Greece and i am trying to create a database that will automatically create Work Schedules for about 200 -300 police officers every day.
I have created 3 tables:
A) A table that contains all the information of 300 officers with their ID (as a primary key which is connected to |Table C") and other information such as address, when they were transferred to us etc . Call it "Table A"
B) A table with all the possible places that officers may needed
For Example a record in Column A [NOT THE HEADER] is "Police patrols" , Column B: "number of people we will put to that service" for which i created a query (with a tally table so it can repeat as many records as i want, so for example if i need 30 people on patrol it will create 30 unique records of this service in Table C and also i have set a input for the date so it can automatically input the date on the Table C.
C) Table C : Here i have information such as all the services that were created by the query with work time and also a column of the id of the officer that will be assigned to a specific place.
Now i am stuck trying to update, with a random sequence, the table C, specifically the id number column, with the id number column of all the police officers from Table A. (I haven't reached the random part just yet as every time i update the Table C it gets only the ID from the first record from Table A ignoring all the other records, so it puts the same id number to the 30 places i would need and it doesn't show all records from Table A)
WHERE (((TableC.DateofDuty)=[Date]));
I tried this with no luck as for every record in table C it gets the same id from Table A.
Also i tried this:
UPDATE TableC LEFT OUTER JOIN TableA ON TableC.ID= TableA.ID
SET TableC.ID = TableA.ID
WHERE (((TableC.DateofDuty)=[Date]));
UPDATE TableC SET TableC.ID= TableA.ID
WHERE (((TableC.DateofDuty)=[Date]));
or
UPDATE TableC LEFT OUTER JOIN TableA ON TableC.ID= TableA.ID
SET TableC.ID = TableA.ID
WHERE (((TableC.DateofDuty)=[Date]));
In the first line of code it updates all the records i wanted to be updated but with the same ID. In the second record it asks if i want to populate the specific, lets say 100, places i want, i press yes and no id is updated.
I have also tried Inner Join with no luck
I would really really appreciate your help
Edit: I just noticed my message was flagged as insufficient so i will put some more details
This is the table needed to be populated/Updated: TABLE C
Service Duty Date ID
ΜΕΤΑΓΩΓΕΣ 24/06/2019
ΜΕΤΑΓΩΓΕΣ 24/06/2019
ΜΕΤΑΓΩΓΕΣ 24/06/2019
From Table A
(Primary ID) Police Rank
250311 ΤΑΞΙΑΡΧΟΣ
288066 ΑΡΧΙΦΥΛΑΚΑΣ
288076 ΑΣΤΥΦΥΛΑΚΑΣ
So far i Have used the following code
UPDATE [TABLE C] INNER JOIN [TABLE A] ON [[TABLE C].ID = [TABLE A].ID
SET [TABLE C].ID= [TABLE A].ID
WHERE ((([TABLE A].DATE)=[INPUT DATE]));
Which updates 0 rows even though i enter the correct date on the input (24/06/2019) I even put 1/1/2019 just to make sure its not the input causing the problem
Second Try:
UPDATE [TABLE C] Left JOIN [TABLE A] ON [[TABLE C].ID = [TABLE A].ID
SET [TABLE C].ID= [TABLE A].ID
WHERE ((([TABLE A].DATE)=[INPUT DATE]));
which prompts me to update all the 3 records but updates nothing, so even though it says it is going to update, it does not.
Third Try
UPDATE [TABLE C] Right JOIN [TABLE A] ON [[TABLE C].ID = [TABLE A].ID
SET [TABLE C].ID= [TABLE A].ID
WHERE ((([TABLE A].DATE)=[INPUT DATE]));
But it returns 0 records...
4th try I have just the TABLE C
UPDATE [TABLE C] SET [TABLE C].ID= "Random Text"
WHERE ((([TABLE C].Date)=[input date]));
Which updates the ID of the date i put into the input just fine to "Random Text" But as soon as i put Table A back in to the game it returns 0 values
I will keep trying as long a i figure it out. I am sure it must be something really dumb i m missing out.
回答1:
You can use my function:
' Builds random row numbers in a select, append, or create query
' with the option of a initial automatic reset.
'
' 2018-09-11. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RandomRowNumber( _
ByVal Key As String, _
Optional Reset As Boolean) _
As Single
' Error codes.
' This key is already associated with an element of this collection.
Const KeyIsInUse As Long = 457
Static Keys As New Collection
On Error GoTo Err_RandomRowNumber
If Reset = True Then
Set Keys = Nothing
Else
Keys.Add Rnd(-Timer * Keys.Count), Key
End If
RandomRowNumber = Keys(Key)
Exit_RandomRowNumber:
Exit Function
Err_RandomRowNumber:
Select Case Err
Case KeyIsInUse
' Key is present.
Resume Next
Case Else
' Some other error.
Resume Exit_RandomRowNumber
End Select
End Function
in a query like this:
SELECT
ID,
[Order ID],
[Unit Price],
RandomRowNumber(CStr([ID])) AS RandowRow
FROM
[Order Details]
ORDER BY
RandomRowNumber(CStr([ID]));
It is explained in my article Random Rows in Microsoft Access.
If you don't have an account, browse for the link: Read the full article.
Code is also on GitHub: VBA.RowNumbers
回答2:
So i kind of figured it out Let me really quick do a recap We have :
TABLEA
ID | POLICE RANK | FULL NAME |
____________________________________
288066 | Const. | Chris Meli |
273111 | Serg. | John Do |
231444 | Const. | Bill Park |
298432 | Const. | Joe Park |
_____________________________________
which contains the info of the police officers and is connected to the ID field in TableC so even from the connection on TableA you can examine the duties every officer has been assigned to the previous days.
TABLEB
DUTY | Number of Police needed |
| for each service |
____________________________________
Patrol | 1 |
Guards | 1 |
Courts | 2 |
____________________________________
I put the number 1 and 2 just for the sake of simplicity. Normally TableA will contain more than 250 people and on TableB will be many Duties and the number of police needed will vary depending on the date and many other factors.
TABLEC
ID | DUTY | DATE |
____________________________________
| | |
| | |
| | |
| | |
_____________________________________
TableC will be populated From TableA (ID),TableB (Duty) and an input for the date i will be scheduling with the following appending query
INSERT INTO TABLEC ( DUTY, DATE, ID )
SELECT TABLEB.DUTY, [INPUT DATE], TABLEA.ID
FROM TABLEA, TABLEB INNER JOIN n ON n.n <= TABLEB.[Number of Police needed for each service];
(n is a numbers table and n.n is a column that has like 10000 numbers so don't pay attention to that) Now the appending query returns me the results i need but what happens is , it kind of multiplies the position needed with the officer's ID . So instead of having this:
TABLEC
ID | DUTY | DATE |
____________________________________
288066 | Patrol | 23/06/2019 |
273111 | Guards | 23/06/2019 |
231444 | Courts | 23/06/2019 |
298432 | Courts | 23/06/2019 |
_____________________________________
I have this:
TABLEC
ID | DUTY | DATE |
____________________________________
288066 | Patrol | 23/06/2019 |
288066 | Guards | 23/06/2019 |
288066 | Courts | 23/06/2019 |
288066 | Courts | 23/06/2019 |
273111 | Patrol | 23/06/2019 |
273111 | Guards | 23/06/2019 |
273111 | Courts | 23/06/2019 |
273111 | Courts | 23/06/2019 |
231444 | Patrol | 23/06/2019 |
231444 | Guards | 23/06/2019 |
231444 | Courts | 23/06/2019 |
231444 | Courts | 23/06/2019 |
298432 | Patrol | 23/06/2019 |
298432 | Guards | 23/06/2019 |
298432 | Courts | 23/06/2019 |
298432 | Courts | 23/06/2019 |
_____________________________________
Is there a way to connect TableA.ID and TableB.DUTY without being multiplied automatically?
来源:https://stackoverflow.com/questions/56712149/how-to-update-populate-empty-fields-in-a-table-with-values-from-a-b-with-a-rando