Hi Al,
This is a somewhat different approach that adds, in this case an employee to
a table in a document and also populates the listbox with the employees at
the same time. It will give you the details for each person on a separate
row rather than in columns (which would be difficult to work with):
Private Sub cmdAddEmployee_Click()
'Check that data has been entered into both the FirstName and the LastName
controls.
If Len(Trim(txtFirstName)) = 0 Or Len(Trim(txtLastName)) = 0 Then
MsgBox "You must enter both the first name and the last name of the
Employee"
Exit Sub
End If
'Get the path to the file containing the Training Tracker Data.
PathofSystemFiles =
System.PrivateProfileString("C:\RNMFormSettings.txt", "MacroSettings", _
"TrainingTrackerDataFileSource")
'Open the TrainingData File
Set sourcedoc = Documents.Open(PathofSystemFiles & "\TrainingData.doc",
, , False)
'Determine what action to take based on the caption that appears on the
button
If cmdAddEmployee.Caption = "Save" Then 'modify existing record
'Locate the row containing the employee
For j = 2 To sourcedoc.Tables(2).Rows.Count
Set myitem = sourcedoc.Tables(2).Cell(j, 1).Range
myitem.End = myitem.End - 1
If myitem = i Then
i = j
Exit For
End If
Next j
Else ' Add new record
i = sourcedoc.Tables(2).Rows.Count
Set myitem = sourcedoc.Tables(1).Cell(i, 1).Range
sourcedoc.Tables(2).Rows.Add
i = i + 1
sourcedoc.Tables(2).Cell(i, 1).Range.Text = Val(myitem) + 1
End If
'Enter the revised data into the Course Details table and Refresh the
ListCourses ListBox
sourcedoc.Tables(2).Cell(i, 2).Range.Text = txtEmployeeNumber
sourcedoc.Tables(2).Cell(i, 3).Range.Text = txtLastName
sourcedoc.Tables(2).Cell(i, 4).Range.Text = txtFirstName
sourcedoc.Tables(2).Cell(i, 5).Range.Text = TxtDepartment
sourcedoc.Tables(2).Cell(i, 6).Range.Text = txtPhone
sourcedoc.Tables(2).Cell(i, 7).Range.Text = txtEmail
sourcedoc.Save
'Sort the Employee list by Last Name, First Name
sourcedoc.Tables(2).Sort ExcludeHeader:=True, FieldNumber:="Column 3",
SortFieldType _
:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending,
FieldNumber2:="Column 4", _
SortFieldType2:=wdSortFieldAlphanumeric,
SortOrder2:=wdSortOrderAscending
'Populate the ListEmployees listbox
Call PopulateListEmployees
'Clear the fields in the FrameNewEmployeeDetails
txtEmployeeNumber = ""
txtFirstName = ""
txtLastName = ""
TxtDepartment = ""
txtPhone = ""
txtEmail = ""
'Enable the ListEmployees control
ListEmployees.Enabled = True
'Modify the caption of the FrameNewEmployeeDetails and hide it
FrameNewEmployeeDetails.Visible = False
FrameNewEmployeeDetails.Caption = "New Employee Details"
'Modify the caption of the cmdAddEmployee button and disable the buton
cmdAddEmployee.Caption = "Add Employee to List"
cmdAddEmployee.Enabled = False
'Enable the NewEmployee button
cmdNewEmployee.Enabled = True
'Hide the cmdCancel Button
cmdCancelEmployee.Visible = False
End Sub
This is the PopulateListEmployees routine that is called by the above code:
Sub PopulateListEmployees()
'Get the number or courses = number of rows in the table of course details
less one
i = sourcedoc.Tables(2).Rows.Count - 1
'Get the number of columns in the table of Employees
j = sourcedoc.Tables(2).Columns.Count
'Set the number of columns in the Listbox to match
'the number of columns in the table of Employees
ListEmployees.ColumnCount = j + 1
'Define an array to be loaded with the Employee data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(2).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
For m = 0 To i - 1
Set Lname = sourcedoc.Tables(2).Cell(m + 2, 3).Range
Lname.End = Lname.End - 1
Set Fname = sourcedoc.Tables(2).Cell(m + 2, 4).Range
Fname.End = Fname.End - 1
MyArray(m, 2) = Lname.Text & ", " & Fname.Text
Next m
For n = 2 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(2).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n + 1) = myitem.Text
Next m
Next n
'Load data into the ListCourses listbox
ListEmployees.List() = MyArray
'Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
Application.ScreenUpdating = True
End Sub
This is the code that you would need to delete an employee form the table in
the document as well as from the listbox:
Private Sub CmdDeleteEmployee_Click()
'Display a warning message
Dim Msg, Style, Title, Response, MyString
Msg = "You are about to delete the selected employee. Do you want to
continue ?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Warning" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes.
'Disable the cmdDeleteEmployee button
CmdDeleteEmployee.Enabled = False
'Get the Row Number for the Employee who is to be deleted.
i = ListEmployees.Column(0)
'Open the TrainingData File
Set sourcedoc = Documents.Open(PathofSystemFiles &
"\TrainingData.doc", , , False)
'Locate the row containing the employee
For j = 2 To sourcedoc.Tables(2).Rows.Count
Set myitem = sourcedoc.Tables(2).Cell(j, 1).Range
myitem.End = myitem.End - 1
If myitem = i Then
i = j
Exit For
End If
Next j
'Delete the row containing the data for the employee
sourcedoc.Tables(2).Rows(i).Delete
'Save and Close the TrainingData file
sourcedoc.Close wdSaveChanges
'Remove the item from the listEmployees
ListEmployees.RemoveItem (ListEmployees.ListIndex)
Else ' User chose No.
Exit Sub
End If
End Sub
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP