CIST 304 Week 7: Chapters 15 & 16 VB with other applications and MS Access
Visual Basic Interoperability VB can be used across applications VB interoperability with other MS Office applications (Word, Access, PowerPoint, etc) VB interoperability with third party applications VB can launch applications exchange data between applications modify the file system reach out across the network (including the Internet) Ch 15
Launching Applications Shell Allows you to start an application using VB Ex: Shell calc.exe, vbmaximizedfocus Complete path needed (unless the file s directory location is already in the $PATH variable Note the Windows styles in Table 15.1 (hidden, max, etc) What applications can we launch? Calculator Notepad Control Panel pretty much any Ch 15
Try This: Hands-on 15.1 Complete the Hands-On 15.1 exercise Try changing the cpl files Which cpl file gets us directly to the window that lets us change the user display? Do an Internet search for dll or cpl files that are not listed in Table 15.2 and try using those in the shell command. Ch 15
Moving Between Applications AppActivate Changes the focus (i.e. allows you to move between applications that are already open) Table 15.3 shows MS Office app constants The [wait] argument is optional. If [wait] is set to False, there is no delay. Note: the title is enclosed in quotes and must exactly match the name of the running application Ch 15
Controlling Another Application AppActivate works if the other application is already running SendKeys Sends keystrokes to an open application Syntax: SendKeys string [,wait] string is the set of keystrokes you want to send to the open application [wait] is optional (similar to AppActivate option) Table 15.4 lists some of the keycodes Reserved characters must be enclosed in braces {} Ch 15
Controlling Applications - Automation Automation provides functionality to access the object model of other applications Concept is similar to accessing the object model in Excel Access the properties and methods of the application s objects Automation replaces Dynamic Data Exchange (DDE). Ch 15
Automation / Object Linking and Embedding (OLE) OLE allows you to create compound documents (p420) Compound documents are composed of objects from different applications Word document with Excel table PowerPoint document with Excel table Word document with PowerPoint slide etc Linked vs. Embedding When a linked object is changed the object as it is contained in other objects will change Embedding simply inserts the object without creating a linking (i.e. if the original changes, the embedded object will not) Ch 15
Component Object Model (COM) Provides Automation interfaces Includes all the object properties and methods VB can directly access the object s interfaces using COM Ch 15
Binding Binding is the process by which VB associates the code with the object Late vs. Early binding Late: aka runtime binding VB waits until the procedure is executed before it associates with the object Pros: Can be used with all Automation objects Cons: Programmer must manually define the association Early: aka compile-time binding VB makes the association when the program is compiled Ch 15
References Tools->References allow us to add object references (part of establishing the early binding) Object Browser will reflect the addition of the added reference Ch 15
Creating Automation Objects CreateObject Function GetObject Function Ch 15
Excel, VB, and MS Access (oh, boy ) Concepts from previous section are still applicable MS Access application interoperability and object access is nuanced because you are accessing a database. Special methods for accessing a database: Automation (as before) Data Access Objects (DAO) ActiveX Data Objects (ADO) Ch 16
What is a database? A set of tables with defined relationships Each table is similar to an Excel spreadsheet Rows = records Columns = fields Each record needs a primary key (a unique identifier for that record) Tables can be linked through common fields (relationship) EmpID Name Dept Hired 12345 Jones 11 9/1/2000 67891 Jones 42 3/6/2012 EmpID Salary 12345 $22,000 67891 $31,000 Dept Manager 11 Smith 42 Willis Ch 16
MS Access Object Library Like everything else MS Access uses objects Objects are contained in the MS Access library msacc.olb file Database objects provide ways to connect to the database provide ways to manipulate the database structure provide ways to add, update, modify, delete database entries Need to setup a reference to the Access Library Ch 16
Establishing a connection to the database from Excel Database not secured (no password) you can use Automation (see book) Database secured with a password DAO method Setup a reference Define the connection string Use object methods to connect ADO method Setup a reference Define the connection string Use object methods to connect Ch 16
Creating a new Access database using DAO Functions CreateDatabase: creates the database CreateTableDef: creates a table within the database CreateField: creates a field within the table Methods Append: appends fields and tables to the object Close: closes the database Ch 16
Hands-On 16.5 Code Breakdown: Declaring Objects Dim db as DAO.Database Dim tbl as DAO.TableDef Dim strdb as String Dim strtbl as String DAO TableDef strdb tbl Database sttbl db Ch 16
Hands-On 16.5 Code Breakdown: Building Table strtbl = tblstates Set db = CreateDatabase(strDb, dblanggeneral) Set tbl = db.createtabledef(strtbl) With tbl.fields.append.createfield( StateID, dbtext, 2).Fields.Append.CreateField( StateName, dbtext, 25).Fields.Append.CreateField( StateCapital, dbtext, 25) End With tblstates StateId StateName StateCapital Ch 16
Hands-On 16.5 Code Breakdown: Appending Tables to DB db.tabledefs.append tbl db tbl StateId StateName StateCapital Ch 16
Creating a Database - Try This Use Visual Basic to create a database called UMW Students Create the table structure (definition) below and append it to the database StudentId LastName Major Year Ch 16
In-Class Assignments Hands-On 16.11 Ch 16