Subform Question

T

Travis

Hello, I have a subform that grabs an employee's training from a table and
the title of the associated training id from another table that has the
training courses's info. How can I have the subform list all the training
courses titles but also only list the dates taken and due from the employee's
training table for the associated training course?
 
D

DStegon via AccessMonster.com

Put a IS Not Null where in the taken and due fields of the query that is
bound to your subform. As long as the two table are INNER Joined then you
will only be getting the info associated with the employee you are viewing,
thus yo need to "filter" out those that do not have a date in the taken or
due. You could use either AND or OR in the where clause of the query to get
the result set you want.
 
T

Travis

Maybe I explained my question wrong. What I was attempting to get at was, I
want to show all training courses for each employee, even if they've done it
or not, but I only want the training for each employee on the same page.
Example:

John Doe
Training Title Taken Due
1 12/12/09 12/12/10
2 03/12/09 03/12/12
3 - 03/31/09
4 - 03/31/09

Next Employee Record:

Jane Doe
Training Title Taken Due
1 09/25/09 09/25/10
2 02/19/09 02/19/12
3 09/29/09 09/29/10
4 04/02/09 04/02/10

Right now with what I have all it does is list the training titles and dates
for what they have on record in the Employee Training table.. my tables are
as follows:

Employees
-ID
-Name
-Etc

Training
-ID
-Title
-Etc

Employee Training
-ID
-Emp ID
-TNG ID
-Taken
-Due
 
D

DStegon via AccessMonster.com

Grouping like this is tough to do on forms and subforms because Access takes
you out of continuous forms to single when you place a subform on a form that
is setup as continous.

This type of data would be nicely shown in a Treeview where you add the
persons name as a node, then add all the training as nodes under the person
node and loop through each person adding the ncame node and then the training
node. Allowing you to exand the people that you were interested in seeing.

If you dont know how to do a treeview let me know and I will send you the
basic code. Fairly straight forward and once you see it you will be able to
use it over and over.
Maybe I explained my question wrong. What I was attempting to get at was, I
want to show all training courses for each employee, even if they've done it
or not, but I only want the training for each employee on the same page.
Example:

John Doe
Training Title Taken Due
1 12/12/09 12/12/10
2 03/12/09 03/12/12
3 - 03/31/09
4 - 03/31/09

Next Employee Record:

Jane Doe
Training Title Taken Due
1 09/25/09 09/25/10
2 02/19/09 02/19/12
3 09/29/09 09/29/10
4 04/02/09 04/02/10

Right now with what I have all it does is list the training titles and dates
for what they have on record in the Employee Training table.. my tables are
as follows:

Employees
-ID
-Name
-Etc

Training
-ID
-Title
-Etc

Employee Training
-ID
-Emp ID
-TNG ID
-Taken
-Due
Put a IS Not Null where in the taken and due fields of the query that is
bound to your subform. As long as the two table are INNER Joined then you
[quoted text clipped - 8 lines]
 
D

DStegon via AccessMonster.com

Just in case..... Create a form and put a single Treeview control on the form.
Name it Treeview0

copy this code into the form's module
Change the table names and field names to match your own

By the way... Using just "ID" in each table will GET REAL CONFUSING after a
while. Better to name the ID field Employee_ID in the Employee table than it
is to name it just ID. Once you start joining table after table with ID all
in them it will get confusing about which ID you are really looking at. Just
a thought....

Oh... I did not pend much time "formatting" the data... you can play with
that... spacing and such. I also put all the info in one line like you
showed, but you could just as easily have it listed one under another like
this

- Joe Smith
Title - 1
Taken - 1/1/2009
Due - 10/1/2009

making the data expandable at each level if you wished

-Joe Smith
- Title - 1
Taken - 1/1/2009
Due - 10/1/2009

code ************************************


Option Compare Database
Option Explicit
Public WithEvents TrVw As TreeView

Private Sub Form_Load()

Set TrVw = Me.TreeView0.Object
PopulateTree1

End Sub

Private Sub PopulateTree1()
'this will popluate the names
Dim EmplID As Long
Dim rst As New ADODB.Recordset
Dim nod As Node
Dim nod2 As Node

Set nod = TrVw.Nodes.Add(, , "TrSch", "Training")

nod.EnsureVisible
nod.Expanded = True

Set nod2 = TrVw.Nodes.Add(nod.Key, tvwChild, CStr("T1"), "Title" & "
" & "Taken" & " " & "Due")

'This will only give you employees that actually have a training record
With rst
.Open "SELECT DISTINCTROW Employees.EmployeeID, Employees.LastName,
Employees.FirstName " & _
"FROM Employees INNER JOIN [employee training] ON Employees.EmployeeID =
[employee training].Employee_ID " & _
"ORDER BY Employees.LastName;", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
Do Until .EOF
EmplID = !EmployeeID
With TrVw
'the node is added as Relative, Relation,Key,Text, image (not
using here)
Set nod2 = .Nodes.Add(nod.Key, tvwChild, CStr("E" & rst!
EmployeeID), rst!LastName & ", " & rst!FirstName)
PopulateTree2 EmplID, CStr("E" & rst!EmployeeID)
nod2.Expanded = True
End With
.MoveNext
Loop
End With

End Sub

Private Sub PopulateTree2(EmployeeID As Long, NodKey As String)
'this will populate the training

Dim rst As New ADODB.Recordset

With rst
.Open "SELECT [employee training].Employee_ID, [employee training].
Trng_ID, [employee training].Taken, [employee training].Due, Training.Title "
& _
"FROM [employee training] INNER JOIN Training ON [employee training].
Trng_ID = Training.Training_ID " & _
"WHERE ((([employee training].Employee_ID)=" & EmployeeID & "));",
CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Do Until .EOF
With TrVw
.Nodes.Add NodKey, tvwChild, "", rst!Title & " " &
(Nz(rst!Taken, "-")) & " " & (Nz(rst!Due, "-"))
'no key need because there are no relationships below this
level to have a relation with
End With
.MoveNext
Loop
.Close
End With

End Sub


Code end ******************************
 
T

Travis21

I did this, although I am getting an error, excuse my ignorance i've never
got into the coding portion of access.

Error: User-Defined Type Not Defined
At: Dim rst As New ADODB.Recordset.

Thank you for all your help, it is appreciated.
Just in case..... Create a form and put a single Treeview control on the form.
Name it Treeview0

copy this code into the form's module
Change the table names and field names to match your own

By the way... Using just "ID" in each table will GET REAL CONFUSING after a
while. Better to name the ID field Employee_ID in the Employee table than it
is to name it just ID. Once you start joining table after table with ID all
in them it will get confusing about which ID you are really looking at. Just
a thought....

Oh... I did not pend much time "formatting" the data... you can play with
that... spacing and such. I also put all the info in one line like you
showed, but you could just as easily have it listed one under another like
this

- Joe Smith
Title - 1
Taken - 1/1/2009
Due - 10/1/2009

making the data expandable at each level if you wished

-Joe Smith
- Title - 1
Taken - 1/1/2009
Due - 10/1/2009

code ************************************

Option Compare Database
Option Explicit
Public WithEvents TrVw As TreeView

Private Sub Form_Load()

Set TrVw = Me.TreeView0.Object
PopulateTree1

End Sub

Private Sub PopulateTree1()
'this will popluate the names
Dim EmplID As Long
Dim rst As New ADODB.Recordset
Dim nod As Node
Dim nod2 As Node

Set nod = TrVw.Nodes.Add(, , "TrSch", "Training")

nod.EnsureVisible
nod.Expanded = True

Set nod2 = TrVw.Nodes.Add(nod.Key, tvwChild, CStr("T1"), "Title" & "
" & "Taken" & " " & "Due")

'This will only give you employees that actually have a training record
With rst
.Open "SELECT DISTINCTROW Employees.EmployeeID, Employees.LastName,
Employees.FirstName " & _
"FROM Employees INNER JOIN [employee training] ON Employees.EmployeeID =
[employee training].Employee_ID " & _
"ORDER BY Employees.LastName;", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
Do Until .EOF
EmplID = !EmployeeID
With TrVw
'the node is added as Relative, Relation,Key,Text, image (not
using here)
Set nod2 = .Nodes.Add(nod.Key, tvwChild, CStr("E" & rst!
EmployeeID), rst!LastName & ", " & rst!FirstName)
PopulateTree2 EmplID, CStr("E" & rst!EmployeeID)
nod2.Expanded = True
End With
.MoveNext
Loop
End With

End Sub

Private Sub PopulateTree2(EmployeeID As Long, NodKey As String)
'this will populate the training

Dim rst As New ADODB.Recordset

With rst
.Open "SELECT [employee training].Employee_ID, [employee training].
Trng_ID, [employee training].Taken, [employee training].Due, Training.Title "
& _
"FROM [employee training] INNER JOIN Training ON [employee training].
Trng_ID = Training.Training_ID " & _
"WHERE ((([employee training].Employee_ID)=" & EmployeeID & "));",
CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Do Until .EOF
With TrVw
.Nodes.Add NodKey, tvwChild, "", rst!Title & " " &
(Nz(rst!Taken, "-")) & " " & (Nz(rst!Due, "-"))
'no key need because there are no relationships below this
level to have a relation with
End With
.MoveNext
Loop
.Close
End With

End Sub

Code end ******************************
If you dont know how to do a treeview let me know and I will send you the
basic code. Fairly straight forward and once you see it you will be able to
use it over and over.
 
T

Travis

I'm thinking of other possible ways since for some reason I can not get this
treeview to work, is there a way to have a form that I can use to add new
courses to my Courses table (Course_ID[key], Course_Title,
Course_Description, Course_Frequency) but then add itself to the
Employee_Training table (Training_ID[key], Emp_ID, Cse_ID, Date_Done,
Date_Due) for each employee? So somehow adding that Course_ID to a new record
with each employee's Emp_ID in the Employee_Training table.

Basically:

Create Course Record (Title, Description, Frequency)
Get Course_ID
New_Course_ID = Course_ID
For each Employee_ID
Date_Taken = "-"
Date_Due = Today
Create Employee_Training record (Employee_ID, New_Course_ID,
Date_taken,Date_Due)
 
D

DStegon via AccessMonster.com

Travis21 said:
I did this, although I am getting an error, excuse my ignorance i've never
got into the coding portion of access.

Error: User-Defined Type Not Defined
At: Dim rst As New ADODB.Recordset.

Thank you for all your help, it is appreciated.

You probably have not referenced ADO in your db. To do that:

Open your db
Press ALT + F11 (brings up coding window)
Click on Tools
References
Find Microsoft ADO Ext 2.8 for DDL and Security and Check it
Find Microsoft ActiveX Data Objects 2.1 Library and Check it
Click on OK
(you may want to make sure you have Microsoft Windows Common Controls 6.0
(SP6) ) also checked.

Once you have clicked on that, reference to those dll or ActiveX components
will be allows. Sorry, forgot that Access only has DAO as a reference when
initially started.

Your project will now compile - Click on Debug > Compile. Project will now
compile and bring up any other compiler errors.
 
T

Travis

There are a lot of lines that are coming up saying "Expected: End of
Statement" mainly the .open lines and the final .nodes line
 
T

Travis

If you could please email me at [email protected], I can email you back
with what I have so far, maybe you can see what im doing wrong that way
rather than just seeing my posts? Thank you again.
 
D

DStegon via AccessMonster.com

Sure. Create a form, bound to the Courses table or unbound. Your choice.
Since much of what you want to do is to add this record to the employees
table then either one will work just fine.

Here is my suggestion:
Create a form that is bound (just in case LL is looking in on me) where you
have all the current courses listed. Have a Text box in the page header and
a Command button. Label the text box "New Course" and have it unbound. On
the click event of the command button put the following code. Again, I am
using ADO not DAO so reference ADO in your project or change code accordingly
to use DAO (which I am not a fan of).

Private Sub Command8_Click()
Dim rst As New ADODB.Recordset
Dim rstEmploy As New ADODB.Recordset
Dim rstEmployTrng As New ADODB.Recordset
Dim CourseID As Long

If Len(Me.textNewCourse & "") = 0 Then
MsgBox "You have not typed anything to add", vbOKOnly, "No Course to Add"
Me.textNewCourse.SetFocus
Exit Sub
Else
With rst
.Open "Select * from Training WHERE Title='" & Me.textNewCourse &
"';", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If Not .EOF Then 'test to see if this course already exists
MsgBox "This Course Title already exists", vbOKOnly, "Action
Not Allowed"
Me.textNewCourse.SetFocus
Exit Sub
Else
.AddNew
!Title = Me.textNewCourse
'add any other things you want to add
!otherStuff = "Blah"
CourseID = !Training_ID
.Update
End If
.Close

End With

'open employee_training table so that we can add records per employee
rstEmployTrng.Open "employee_training", CurrentProject.Connection,
adOpenKeyset, adLockOptimistic

'now open employee table so we can lopp through each record
With rstEmploy 'you could use the same rst as before since it is closed
but doing this to keep confusion down
.Open "Select * FROM Employees", CurrentProject.Connection,
adOpenKeyset, adLockOptimistic
Do Until .EOF 'loops until the End Of File - stepped past the
last records
'now add record
rstEmployTrng.AddNew
rstEmployTrng!Employee_ID = !EmployeeID
rstEmployTrng!Trng_ID = CourseID
rstEmployTrng!Due = DateValue(Now()) 'get todays date and
takes just the date part
rstEmployTrng.Update 'adds the records
.MoveNext 'moves to the next employee in the table
Loop
.Close
End With
rstEmployTrng.Close
Me.textNewCourse = ""

End If

Me.Requery

End Sub




I'm thinking of other possible ways since for some reason I can not get this
treeview to work, is there a way to have a form that I can use to add new
courses to my Courses table (Course_ID[key], Course_Title,
Course_Description, Course_Frequency) but then add itself to the
Employee_Training table (Training_ID[key], Emp_ID, Cse_ID, Date_Done,
Date_Due) for each employee? So somehow adding that Course_ID to a new record
with each employee's Emp_ID in the Employee_Training table.

Basically:

Create Course Record (Title, Description, Frequency)
Get Course_ID
New_Course_ID = Course_ID
For each Employee_ID
Date_Taken = "-"
Date_Due = Today
Create Employee_Training record (Employee_ID, New_Course_ID,
Date_taken,Date_Due)
 
D

DStegon via AccessMonster.com

This is because the forum window will not let long line be pasted and when
you copy and paste you are losing the original format. You have to take
lines that were "wrapped" to a new line and put tham all back as ONE line.
There are a lot of lines that are coming up saying "Expected: End of
Statement" mainly the .open lines and the final .nodes line
[quoted text clipped - 22 lines]
Your project will now compile - Click on Debug > Compile. Project will now
compile and bring up any other compiler errors.
 
Top