Presentation is loading. Please wait.

Presentation is loading. Please wait.

Find, filter etc with connection to Access code internally

Similar presentations


Presentation on theme: "Find, filter etc with connection to Access code internally"— Presentation transcript:

1 Find, filter etc with connection to Access code internally
Please see speaker notes for additional information! This will show the internal code to deal with the FIND program covered in a prior class.

2 Revision of PrFIndetc.vbp
Please note that this is a revised version. There is no binding of data to the database through properties. It is all done in the code.

3 Revision of PrFIndetc.vbp
Dim conStudentrel00 As ADODB.Connection Dim rsStudent00 As ADODB.Recordset Dim rsStucourse00 As ADODB.Recordset Dim rsMajor00 As ADODB.Recordset Dim rsCourse00 As ADODB.Recordset Dim the ADODB.Connection and the ADODB.Recordsets. Private Sub Form_Load() Dim DBPath As String DBPath = App.Path If Right(DBPath, 1) <> "\" Then DBPath = DBPath & "\" End If DBPath = DBPath & "studentrel00.mdb" Set conStudentrel00 = New ADODB.Connection 'Instantiate the connection object - actually loads object in memory With conStudentrel00 .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Persist Security Info=False;" & _ "Data Source=" & DBPath .Open End With This slide shows the DIM statements in the general area and the beginning of the Form_Load. Note that the Form_Load continues for the next two slides. Establish the connection string for conStudentrel00.

4 Dim SQLCommand As String
Revision of PrFIndetc.vbp Dim SQLCommand As String SQLCommand = "SELECT studentidno, name, majorcode, enrolled FROM student00" Set rsStudent00 = New ADODB.Recordset 'Set...New instantiates an object - in this case a recordset object With rsStudent00 .CursorLocation = adUseClient .CursorType = adOpenDynamic .LockType = adLockOptimistic .Open SQLCommand, conStudentrel00, , , adCmdText .Fields("studentidno").Properties("optimize") = True End With 'CursorLocation is adUseClient which means a client side cursor library is used 'CursorType is type of cursor used in recordset object 'Lock type is the lock placed on records during editing - this locks record by record 'recordset.open source (in this case an SQL statement), active connection '(in this case the connection string), Cursor Type, Lock Type, Options '(in this case adCmdText) Set rsMajor00 = New ADODB.Recordset With rsMajor00 .Open "major00", conStudentrel00, adOpenDynamic, adLockOptimistic, adCmdTable .Fields("majorcode").Properties("optimize") = True Refer to the previous example for explanation of features that were covered there. Note that rsStudent00 contains data through a SQL SELECT while rsMajor00 contains data in the major00 table.

5 Revision of PrFIndetc.vbp
Set rsStucourse00 = New ADODB.Recordset With rsStucourse00 .CursorLocation = adUseClient .Open "stucourse00", conStudentrel00, adOpenDynamic, adLockOptimistic, adCmdTable End With Set rsCourse00 = New ADODB.Recordset With rsCourse00 .Open "course00", conStudentrel00, adOpenDynamic, adLockOptimistic, adCmdTable .Fields("coursecd").Properties("optimize") = True End Sub Both rsStucourse00 and rsCourse00 are using data from a table as opposed to selecting information from the table with SQL.

6 Revision of PrFIndetc.vbp
Private Sub cmdFind_Click() Dim StuFind As String If txtstudentidno.Text = "" Then MsgBox ("Enter Student ID#") txtstudentidno.SetFocus Else txtstudentidno.Locked = True StuFind = "studentidno = '" & txtstudentidno.Text & "'" With rsStudent00 .MoveFirst .Find StuFind If .EOF Then MsgBox "Student Not Found", vbExclamation txtstudentidno.Text = "" Call cmdClear_Click Fill_TextBoxes cmdFind.Enabled = False cmdClear.SetFocus End If End With End Sub Locked so it cannot be changed. Set up the find equation comparing for studentidno equal to txtstudentidno. The find is executed using rsStudent00. This shows the find of a student.

7 Revision of PrFIndetc.vbp
Private Sub Fill_TextBoxes() 'A textbox can not be assigned a null value so if a field on the database can contain null 'you should append an empty string to the database field when you assign it to a text box. With rsStudent00 txtname.Text = .Fields("name") & "" txtenrolled.Text = .Fields("enrolled") & "" txtmajorcode.Text = .Fields("majorcode") & "" End With With rsMajor00 Dim MajFind As String MajFind = "majorcode = '" & txtmajorcode.Text & "'" .MoveFirst .Find MajFind If .EOF Then txtmajorname.Text = "##############" txtadvisor.Text = "#############" Else txtmajorname.Text = .Fields("majorname") & "" txtadvisor.Text = .Fields("advisor") & "" End If Fills text boxes with data from rsStudent00 recordset. Using the majorcode that was put into the text box, I am now going to search for a match in the rsMajor00 recordset. Again remember the very good technique of adding an empty string to fields from a database that is being put into a text box. It is a technique that I do not always follow but it is a good habit to get into! The data is now being taken from rsMajor00 and put into the texts on the form.

8 Revision of PrFIndetc.vbp
With rsStucourse00 Dim StuCrsFilter As String StuCrsFilter = "studentidno = '" & txtstudentidno.Text & "'" .Filter = StuCrsFilter .Sort = ("coursecd") Do Until .EOF With rsCourse00 Dim CrsFind As String Dim CrsName As String Dim CrsCredits As String 'CrsFind = "coursecd = '" & rsStucourse00.Fields("coursecd") & "'" CrsFind = "coursecd = '" & rsStucourse00!coursecd & "'" .MoveFirst .Find CrsFind If .EOF Then CrsName = "###########" CrsCredits = "###" Else CrsName = .Fields("coursename") & "" CrsCredits = .Fields("credits") & "" End If End With CrsName = Left((CrsName & Space(30)), 30) Dim CoursesTaken As String CoursesTaken = .Fields("coursecd") & vbTab & CrsName & vbTab & CrsCredits CoursesTaken = CoursesTaken & vbTab & .Fields("grade") & vbTab & .Fields("semtaken") lbxCrsTaken.AddItem CoursesTaken .MoveNext Loop End Sub Use a filter to extract all records that have a matching studentidno - these courses are then sorted on coursecd. Since there are potentially multiple records for each student a DO loop is used to process each record. For each record in the filter, we will now find the course name and credits in the rsCourse00 recordset. Now I am getting information from the rsStucourse00 recordset and related information from the rsCourse00 recordset. Note that two ways of specifying a particular course are illustrated. One is .Fields("coursecd") and one is !coursecd. For further information on this logic, please try the original presentation. Now the course information is being put together into a string with vbTab between columns. When the string is set it will be written to the list box using AddItem.

9 Revision of PrFIndetc.vbp
This shows the output that resulted from entering 222 as ID # and then clicking on Find.

10 Another revision using the combo box
This shows the output using the combo box. If a course is selected, it will show in the combo box when it is not open.

11 Another revision using the combo box
This shows the combo box instead of the list box. Also note that there is no binding in the properties.

12 CrsName = Left((CrsName & Space(30)), 30)
Another revision using the combo box CrsName = Left((CrsName & Space(30)), 30) Dim CoursesTaken As String CoursesTaken = Left(.Fields("coursecd") & Space(5), 5) & " " & _ CrsName & " " & CrsCredits & " " & _ Left(.Fields("grade") & Space(2), 2) & " " & _ Left(.Fields("semtaken") & Space(5), 5) cboCrsTaken.AddItem CoursesTaken .MoveNext AddItem to the combo box. With the combo box, I needed to use Space to separate columns instead of the vbTab used in the last example. Move to next item in filter.


Download ppt "Find, filter etc with connection to Access code internally"

Similar presentations


Ads by Google