Find, filter etc with connection to Access code internally

Slides:



Advertisements
Similar presentations
ADO DB in Access VBA © Walter Milner 2005 Slide: 1 ADO VBA Programming in Access.
Advertisements

Internal connection and introduction to shape language Please use speaker notes for additional information.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Persistent data storage: relational databases and ADO.
ActiveX Data Object ISYS 562. ADO An ActiveX control ActiveX is build upon COM, a contract that defines a standard interface by which objects communicate.
Using ADO Programmatically The Connection Object, Command Object, and Recordset Object.
Using Objects and Properties
Programming the RecordSet Object
Using ADO Programmatically The Connection Object and the Command Object.
Visual Basic Database Access BICS546. Microsoft Universal Data Access OLE DB: The OLE database protocol –Allows a program to access information in any.
CIS 451: ASP Recordsets Dr. Ralph D. Westfall May, 2002.
Component 8, Slide 1 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton CP2030 Visual Basic For C++ Programmers.
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
ADO Recordsets. Recordset Objects Similar to Tables and Queries: data Using VBA/VBScript you… –Open a recordset, –Locate a record –Update or add a record.
ActiveX Data Object (ADO) in JavaScript J.L.Wang, Yen-Cheng Chen Dept. of Infomation Management Ming-Chuan University Jan
1 VBScript Session What we learn last session?
Introduction to ADO By David R. Stevenson Consulting Software Engineer ABB Automation.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
InvEasy (Project1) Please use speaker notes for additional information!
Break Processing Please use speaker notes for additional information!
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 11 Accessing Database.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Array - adding to array at run time Please see speaker notes for additional information!
Lecture Note 10: Simple Database Techniques. Introduction –Database System –Access, SQL Server and others. –Microsoft Access - Interacting with this databases.
Visual Basic ADO Programming 56:150 Information System Design.
© 1999, by Que Education and Training, Appendix A, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.
Random Files Please see speaker notes for additional information!
ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer.
Why are Databases Better than Files? Multiple users can all use the same database, and have access to the current, up to the minute values for the data.
Delivery and other DO Examples Please use speaker notes for additional information!
Notes on ADO from other projects Please use speaker notes for additional information!
Two Forms Please use speaker notes for additional information!
Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1.
Lab 8 Data Access Using Microsoft ActiveX Data Object (ADO)
Visual Basic I/O Programs (ProjRead1, ProjRead2, ProjWrite1, ProjPay) Please use speaker notes for additional information!
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
ADO ActiveX Data Object. ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard.
Relational Database in Access Student System As always please use speaker notes!
Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,
Using a Database Access97 Please use speaker notes for additional information!
ASP-13-1 Recordsets Colorado Technical University IT420 Tim Peterson.
1 ADO Activex Data Objects. 2 ADO ADO allows users to access data easily from many existing databases (such as Access or Paradox) From ODBC compliant.
Unbound data fields, find, filter etc. using Data Environment Please use speaker notes for additional information!
ADO and DataList, DataCombo and DataGrid Controls Please use speaker notes for additional information!
 2 Data Object Library approaches ◦ DAO (Data Access Objects)  Original access strategy (up to VB6)  Closely linked to MS Access ◦ ADO (ActiveX Data.
Visual Basic - Break Processing
Processing multiple files
Visual Basic Database Access
Unit 2 Technology Systems
Visual Basic Database Programming.
Unit 9.1 Learning Objectives Data Access in Code
ADO VBA Programming in Access
Data Environment and Grouped Report
ActiveX Data Objects (ADO)
INT213 Updating the Database.
This shows the user interface and the SQL Select for a situation with two criteria in an AND relationship.
3rd prep. – 2nd Term MOE Book Questions.
Please use speaker notes for additional information!
Department Array in Visual Basic
Please use speaker notes for additional information!
Introduction to Views and Reports
Chapter (3) - Looping Questions.
Searching an Array or Table
Please use speaker notes for additional information!
Please use speaker notes for additional information!
Please use speaker notes for additional information!
Working With Databases
Excel Import data from Access to Excel (ADO) using VBA
Please see speaker notes for additional information!
Presentation transcript:

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.

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

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.