Ken, I am so sorry, and I appreciate your patience:
When I get a chance tonight, I will most certainly check out Northwind.
I
think I need to back up a few steps here. Lets just focus on the
relationships first, then we can work on that "OK" button. I did not
realize
you wanted me to take the relationship of [tblPOSITIONS].[POSITION ID]
away
from tblDAILYINFO. I guess I didn't understand that. I think you see
where
I want to go with this though, and maybe I need to backtrack or change
just a
couple of things around.
Ok, I will lay out my Positions for you. This is a database for a
wearhouse
invironment.
Positions are as follows: Blue, Brown, Orange, Red, Small Sort, Unload,
Yellow. There are multiple employees on "Blue" or on "Unload" etc
everyday.
Again, I will lay out the relevant tables:
tEMPLOYEES (PK)
EMPLOYEE ID
EMPLOYEE NAME
POSITION ID (Newly Added Relationship)
...so forth
tDAILYINFO
DAILYINFO ID (PK)
WorkDate
POSITION ID (FK) "number value"
...so forth
tPOSITIONS
POSITION ID
POSITION
I need the Employees to have a set position they *normally* have. Now,
they
can change around periodically, but that doesn't happen all that often,
unless absent or to fill in for another absent employee. Now, the
thing that
you wrote really caught my attention
"Having a
PositionID column in tblDailyInfo is fine as you'd presumably want
the rows
in this table to hold the employee's position at the time the row was
inserted rather than to be updated with their current position if it
changes,
but in situations like this its normal to also have a column in
tblEmployees
to hold their current position>"
I need this. I need to know what their current position is at the time
the
record was created. I also need that position to come up automatically
when
a new record is created, and that is what I have been trying to get for
a
while. I manually hit the drop-down sombo box and select each
employee's
position each day.
So with this said, maybe we can take a step back, and I won't try to
rush
anything.
Again, I greatly appreciate your patience Ken. It is very much
appreciated.
If I could conquer these things with your help, I will be able to
finish up
the database. Thank you.
-Scott Channell (e-mail address removed)
:
Scott:
You should just need to amend the SQL statement slightly:
Dim cmd As ADODB.Command
Dim strSQL As String
Dim varItem As Variant
Dim ctrl As Control
Set ctrl = Me.lstEmployees
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strSQL = "INSERT INTO tblDailyInfo(EmployeeID) " & _
"VALUES(" & ctrl.ItemData(varItem) & ")"
cmd.CommandText = strSQL
cmd.Execute
Next varItem
End If
Note that this relies on the BoundColumn property of the list box
referencing the column in its RowSource which is the EmployeeID.
Normally this would be the first column, but hidden by virtue of the
first dimension of the ColumnWidths property being zero, i.e. the
BoundColumn property would be 1, the ColumnWidths would be something
like 0cm;8cm and the RowSource would be:
SELECT EmployeeID, [Employee Name] FROM tblEmployees ORDER BY
[Employee Name];
Now that you are not inserting the employee's PositionID into
tblDailyInfo, but purely referencing it via the relationship with
tblEmployees this does of course mean that if an employee's position
subsequently changes, then there will be know way of knowing what
their position was at the time the row was inserted as the database
will only tell you their current position. So long as you are not
losing a significant piece of information as a result, this is fine,
but otherwise you'd need the column in both tables. In the case of
the analogous situation with unit prices in the sample Northwind
database, which I mentioned, Its obviously essential that there are
UnitPrice columns in both tables as you wouldn't want old invoices
showing the new prices whenever one changes - the auditors would soon
have something to say about it! In your case the position (no pun
intended!) is not so clear cut.
Ken Sheridan
Stafford, England
On Jan 9, 3:20 pm, channell <
[email protected]>
wrote:
Ken, Let's try this...
1. Let's not focus on the WorkDate, since that is a default value
and is
automatic. So I would assume we can leave it out of the VBA.
2. I just went in and created a new work date for one of my
employees, and
it put their Default Work Position in the now record automatically.
(I took
your advice and put the "POSITION ID" in tEMPLOYEES (tbl). That
worked
fabulously.
So really, after getting some of my stuff fixed, I just need the
Multi-Select List "OK" button to create new tDAILYINFO.DAILYINFO ID
numbers.
In essence, I just need a new record created for them, and my
system will
take care of the rest. Hope that makes sense.
I can't thank you enough for even making some things more cleare to
me! It
is amazingly wonderful now, and this is my last step before I am
officially
finished! Thank you Ken!
-Scott Channell (e-mail address removed)
:
Scott:
Firstly do you also have a PositionID column in tblEmployees?
Having a
PositionID column in tblDailyInfo is fine as you'd presumably
want the rows
in this table to hold the employee's position at the time the row
was
inserted rather than to be updated with their current position if
it changes,
but in situations like this its normal to also have a column in
tblEmployees
to hold their current position. In each table the column is
functionally
dependent on the key of the table, so no redundancy is involved.
Although
the context is different, in principle its analogous to having a
UnitPrice
column in both Products and OrderDetails, as in the sample
Northwind database.
With a PositionID column in tblEmployees the need for your second
list box
is avoided of course. In fact I don't see how it could work as
it would give
each selected employee the same position! Assuming a PositionID
column in
tblEmployees therefore, you should include the PositionID column
in the list
box's RowSource (it can be a hidden column) which might therefore
be like
this:
SELECT EmployeeID, PositionID, [Employee Name] FROM tblEmployees
ORDER BY
[Employee Name];
Its column Count property would be 3 and its ColumnWidths
property something
like 0cm;0cm;8cm (or equivalent in inches).
Also assuming it's the current date you want inserted as the work
date the
code for the button would go like this:
Dim cmd As ADODB.Command
Dim strSQL As String
Dim varItem As Variant
Dim ctrl As Control
Set ctrl = Me.lstEmployees
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strSQL = "INSERT INTO
tblDailyInfo(EmployeeID,WorkDate,Position) " & _
"VALUES(" & ctrl.ItemData(varItem) & ",#" & _
Format(VBA.Date,"yyyy-mm-dd") & "#," & _
ctrl.Column(1, varItem) & ")"
cmd.CommandText = strSQL
cmd.Execute
Next varItem
End If
I've assumed that PositionID in tblEmployees and Position in
tblDailyInfo
are both columns of number data type, referencing a numeric
primary key of
tblPositions.
Ken Sheridan
Stafford, England
:
I have a Mulit-Select List Box. I can select many employees.
What I need it
to do, if anyone can help me out, is have an "OK" button
available that I can
click (once my employees are selected) that will create new
WorkDay records
for them.
This would absolutely be the most useful thing to me.