Template for Tutorial 10

advertisement
Template for Tutorial 10
Excel p 598 2, 4
Access p 624 1, 2
Excel page 598 ex 2
Public Sub SplitAddress()
'declare variables and assign address to Worksheet object variable
Dim intLoc1 As Integer, intLoc2 As Integer, shtConsul As Worksheet, _
rngCell As Range
Set shtConsul = _
Application.Workbooks("t10-ex-e2d.xls").Worksheets("consultants")
'enter headings Street, City and Zip
shtConsul.Range("b1:d1").Font.Bold = True
'beginning in cell B2, separate each full address into street, city, and zip
Set rngCell = shtConsul.Range("b2")
Do Until rngCell.Value =
'find location of first comma see page 594 part 4.
intLoc1 = InStr( )
'find location of second comma, start looking at intLoc1 + 1
intLoc2 = InStr( )
'assign zip code to appropriate cell in column D
rngCell.Offset(columnoffset:=2).Value = _
Mid( )
'assign city to appropriate cell in column C
rngCell.Offset(columnoffset:=1).Value = _
Mid( )
'assign street to current cell
rngCell.Value = Left( )
'assign the address of the cell in the next row to the rngCell variable
Set rngCell = rngCell.Offset( )
Loop
'adjust the width of columns A and B
shtConsul.Columns("b:d").AutoFit
End Sub
Excel 4 Use existing code, add a Do … Loop
Access p 624 1, 2
1. rstAdjFac.RecordCount holds the number of records. Use this fact to change the
EOF loop to a For Next loop. Change CursorType to adOpenKeyset
Here is the code from the Lesson. Adjust it to solve
Public Function LocateInstructor()
'declare variables and assign address to object variables
Dim strCourseNum As String, strFound As String
Dim cnnAdjFac As ADODB.Connection, rstAdjFac As ADODB.Recordset
Set cnnAdjFac = Application.CurrentProject.Connection
Set rstAdjFac = New ADODB.Recordset
'enter course number
strCourseNum = InputBox(prompt:="Enter the 3-character course number:", _
title:="Course Number")
'open recordset
rstAdjFac.Open Source:="adjunctfaculty", ActiveConnection:=cnnAdjFac, _
CursorType:=adOpenForwardOnly, LockType:=adLockPessimistic
'search for course number in each record's CanTeach field
Do Until rstAdjFac.EOF = True
If InStr(1, rstAdjFac.Fields("canteach"), strCourseNum) > 0 Then
'display current record's name and phone number
MsgBox prompt:=rstAdjFac.Fields("name").Value _
& vbNewLine & vbNewLine & rstAdjFac.Fields("phone").Value, _
buttons:=vbOKOnly + vbInformation, title:="CIS" & strCourseNum
'assign value to strFound variable
strFound = "Y"
End If
'move record pointer to next record
rstAdjFac.MoveNext
Loop
'check if an instructor was found
If strFound <> "Y" Then
MsgBox prompt:="No instructors were found.", _
buttons:=vbOKOnly + vbInformation, title:="CIS" & strCourseNum
End If
'close the recordset and disassociate object variable from object
rstAdjFac.Close
Set rstAdjFac = Nothing
End Function
2. Complete the template below.
Public Function GetFullName()
'declare variables and assign address to object variables
Dim strName As String, cnnFaculty As ADODB.Connection, rstFaculty As
ADODB.Recordset
Set cnnFaculty = Application.CurrentProject.Connection
Set rstFaculty = New ADODB.Recordset
'get first characters in the last name
strName = InputBox(prompt:="Enter first characters in the last name:", title:="Name")
'open the recordset, complete source field
rstFaculty.Open Source:= , ActiveConnection:=cnnFaculty, _
CursorType:=adOpenForwardOnly, LockType:=adLockPessimistic
'search for characters
Do
If rstFaculty.Fields("lastname").Value Like strName & "*" Then
MsgBox prompt _
‘ print first and last names using record set fields
buttons:=vbOKOnly + vbInformation, title:="Full Name"
End If
‘move to next record
Loop
'close the recordset and disassociate object variable from object
rstFaculty.Close
Set rstFaculty = Nothing
Download