ITP 150 – Visual Basic Programming

 

Internet Controls & VB Script Database Project

 

 

This project contains two parts.  The first part involves writing a Visual Basic application that references ‘Microsoft Internet Controls.’  The Microsoft Internet Controls library exposes the functionality of the Internet Explorer Browser.  The VB Application will launch an instance of Internet Explorer and allow various components to be configured through the VB interface.

 

The second part of the project involves writing a web page that connects to a database using VBScript.  The database being used in this project will be the ‘Nwind.mdb’, the same database used in the previous database projects.  In addition to the web page, you will be required to setup a System DSN (from the Control Panels) which establishes the connection to a data source (Nwind.mdb).  The VBScript web page will contain various fields of the Products table and allow the user to navigate through the various records.

 

Part A – Internet Controls

 

 

 

 

 

Dim WithEvents ie As InternetExplorer

 

 

Set ie = New InternetExplorer

 

' set all the UI to invisible

ie.ToolBar = False

ie.StatusBar = False

   

' set starting size and location

ie.Width = 440

ie.Height = 400

ie.Top = 105

ie.Left = 270

       

' make ie visible

ie.Visible = True

 

 

Private Sub optNotVisible_Click()

 

    If optNotVisible.Value = True Then

        ' code to make UI NOT Visible

        ie.ToolBar = False

        ie.StatusBar = False

    End If

 

End Sub

 

Private Sub optVisible_Click()

 

    If optVisible.Value = True Then

        ' code to make UI Visible

        ie.ToolBar = True

        ie.StatusBar = True

    End If

   

End Sub

 

 

ie.Navigate txtURL.Text

 

 

ie.Refresh

 

 

ie.Stop

 

 

ie.Quit

Set ie = Nothing

 

 

Unload Me

 

 

On Error Resume Next

   

' if it's closed, ignore the error...

ie.Quit

   

' clean up ie object variable

Set ie = Nothing

 

 

Private Sub ie_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, _ Flags As Variant, TargetFrameName As Variant, PostData As Variant, _ Headers As Variant, Cancel As Boolean)

 

    lblStatus.Caption = "Navigation begun..."

 

End Sub

 

 

Private Sub ie_NavigateComplete2(ByVal pDisp As Object, URL As Variant)

 

    lblStatus.Caption = "Navigation complete..."

 

End Sub

 

 

Private Sub ie_DownloadBegin()

 

    lblStatus.Caption = "Download begun..."

 

End Sub

 

 

Private Sub ie_DownloadComplete()

 

    lblStatus.Caption = "Download complete..."

 

End Sub

 

 

 

 

Part B – VBScript Database Project

 

 

 

<hmtl>

<head><title>Anthony's VBScript-Database Project Using ADO</title><head>

<body BGCOLOR=ffffcc TEXT="#000000">

 

<h1>VBScript/ActiveX Data Objects (ADO)</h1>

 

<br>

<input type=button name="btnFirst" value="<<" >

<input type=button name="btnPrev" value="<" >

<input type=button name="btnNext" value=">" >

<input type=button name="btnLast" value=">>" >

<br>

<br>

 

The following fields have been generated automatically based on the schema

information found in the recordset.  If the data fields do not appear below this

message you may have to reduce the level of security in your browser.

 

 

<!-- ADO recordset object -->

<object id=rs classid="clsid:00000535-0000-0010-8000-00AA006D2EA4"></object>

 

<script language="VBScript">

            '*******************************************************************

            '  Open the recordset / execute the sql query

            '

            connect = "data source=itp150;user id=admin;password=;"

            query = "select * from products"

            adOpenKeyset = 1

 

            rs.Open query, connect, adOpenKeyset

 

            set flds = rs.Fields

 

            '*******************************************************************

            '  Build the html for viewing the data

            '          

 

            ' determine width for field-name column

            '

            namewid = 0   

            for i = 0 to flds.Count - 1

            ' look for length of each field, compare the length to namewid

              if len(flds(i).Name) > namewid then namewid = len(flds(i).Name)

            next

 

            ' write out html for form-fields

            '

            for i = 0 to flds.Count - 1

              document.write "<pre>"

              s = flds(i).Name

              s = s + space( namewid - len(s) + 2 )

              s = s + "<input type=text name=fld_" + cstr(i) + ">"

              document.write s

              document.write "</pre>"

            next

 

 

            '*******************************************************************

            '  Build the vbscript for copying data to the form

 

            document.writeln "<script language=""vbscript"">"

 

            document.writeln "sub FillForm"

            for i = 0 to flds.Count - 1

              s = "fld_" + cstr(i) + ".value = " + "rs.fields(" + cstr(i) + ").value"

              document.writeln s

            next

            document.writeln "end sub"

 

            ' force 'FillForm' to execute immediately

            document.writeln "FillForm"

            document.writeln "<" + "/" + "script" + ">"

 

</script>

 

<script language="VBScript">

            Sub btnNext_OnClick

              if not rs.EOF then

                rs.MoveNext

                if rs.EOF then

                  rs.MoveLast

                else

                  FillForm

                end if

              end if

            End Sub

 

            Sub btnPrev_OnClick

              if not rs.BOF then

                rs.MovePrevious

                if rs.BOF then

              rs.MoveFirst

                else

                  FillForm

                end if

              end if

            End Sub

 

            Sub btnFirst_OnClick

              rs.MoveFirst

              FillForm

            End Sub

 

            Sub btnLast_OnClick

              rs.MoveLast

              FillForm

            End Sub

</script>

 

<hr size=4>

 

</body>

</html>