Introduction
Browsing the World Wide Web is nothing new to us. But how about organizing the web addresses of frequently visited websites in a table and browsing the web directly from an Access form?
Not only can you browse Internet sites, but you can also access Intranet websites within your local corporate LAN. To do this, simply create an Access form with a Microsoft Web Browser Control and add a few lines of VBA code.
For example, the following VBA code will automatically open the Gmail website (http://www.gmail.com/ When the form containing the Web Browser Control is opened, as illustrated in the image above:
Simple Website Opening Code
Private Sub Form_Load() Dim strURL As String strURL = "http://www.gmail.com/" Me.WebBrowser0.Navigate strURL End Sub
Designing a Form for a Web ActiveX Control
Don't know how to do it? Try the following:
Open a new Form in Design View.
Click somewhere on the Detail Section of the Form to select that area.
Select the ActiveX Control from the Insert Menu.
Look for the name Microsoft Web Browser in the displayed list of ActiveX Controls and select it.
Click OK to insert a Web Browser Control on the Form.
While the Control is still in the selected state, drag the right-bottom-corner sizing control to make it big enough on the Form so that you can view the Web Pages properly.
Display the Property Sheet of the Browser Control (View ->Properties).
Change the Name Property Value to WebBrowserO to match the name used in the above VBA Code.
Select Code from the View Menu to display the VBA Code Module of the Form.
Copy and Paste the above Code into the Module.
Save and close the Form with the name myWebBrowser.
Demo Run of the Form
Connect your PC to the Internet.
Open myWebBrowser Form in normal View.
Wait for a few seconds to load the Web Page into the Control and to display.
In the previous example, we used the website address directly in the code. However, if you create a table containing all the websites you visit frequently, you can use a combo box on the form to select a website and navigate to it quickly. To achieve this flexibility, you will need to make a few modifications to the code above.
Table with Frequently Visited Web Addresses
Create a Table with a single field with the following details:
Table Name: Websites
Field Name: Web Address Data Type: Text Field Size: 255
Save the Table Structure and open it in Datasheet View.
Add a few records with Web URLs that you visit frequently, and save and close the Table.
Make a copy of the Form myWebBrowser and name the copy as myWebBrowser2.
Open myWebBrowser2 in Design View.
Display the Form Header Section (View -> Form Header/Footer), if it is not already Visible.
Expand the Form Header Section with enough height to create a Combo Box Control.
Create a Combo Box using the Web Sites Table.
While the Combo Box is still in the selected state, display the Property Sheet (View -> Properties).
Change the following Property Values of the Combo Box:
- Name: cboWeb
- Width: 5"
- Default Value: "http://www.gmail.com/" or any other Website Address Value you prefer.
Select the child label attached to the Combo box and change the Caption Property Value to Web Address:
Display the Code Module of the Form (View -> Code).
Copy and paste the following Code into the Module, replacing the existing lines.
Form Module Code
Option Compare Database Option Explicit Dim WebObj As WebBrowser Dim varURL As Variant Private Sub cboWeb_Click() GotoSite End Sub Private Sub cboWebLostFocus() GotoSite End Sub Private Sub Form_Load() Set WebObj = Me.WebBrowser0.Object GotoSite End Sub Private Function GotoSite() varURL = Me!cboWeb If Len(Nz(varURL, "")) = 0 Then Exit Function End If WebObj.Navigate varURL End FunctionSave the Form and open it in Normal View.
The website address that you have inserted into the Default Value Property of the Combo Box will open up in the Browser Control.
Select one of the other website addresses you have added to the Table from the Combo Box.
The Browser Window will open the new Website.
If we open other Web Pages by clicking on the Links from the displayed page, then we can navigate (go Back or Forward) between pages by adding a few more lines of code.
Review of the VBA Code.
But first, let’s examine the code above to understand how it works. We have declared a Web Browser object variable and a Variant variable in the global declaration section of the module.
Created a separate Function GotoSite() to respond to different Events (Actions) on the Form without duplicating the code everywhere.
For example, when we open the Form, the GotoSite() Function opens the Default Value URL, which we set in the Combo Box Property, through the Form_Load() Event Procedure.
When you select a URL from the Combo Box, the cboWeb_Click() Event Procedure calls this Function to open the Web Page in the Browser.
If you type a URL in the Web Address control cboWeb and move the Cursor out of the control, the Lost_Focus() Event Procedure runs the Function to open the URL you typed in the Address Control.
We will create a few Command Buttons on the Header of the Form, as shown to the right of the Combo box Control on the above design, and write Subroutines with some simple Browser Object Commands to navigate between Web Pages, which we may open from the displayed Web Pages.
Web Navigation Controls
Open myWebBrowser2 Form in Design View.
Display the Toolbox (if it is not visible, then select Toolbox from the View Menu.
Create five Command Buttons to the right of the Combo Box.
Display the Property Sheet of the first Command Button and change the Name Property and Caption Property Values to Home.
Similarly, change the Name and Caption Property Values of the other Command Buttons with the values given below.
- Back
- Forward
- Refresh
- Stop
Display the Code Module of the Form (View ->Code), copy and paste the following code into the VBA Module, below the existing Code lines.
Private Sub Home_Click()
On Error Resume Next
WebObj.GoHome
End Sub
Private Sub Back_Click()
On Error Resume Next
WebObj.GoBack
End Sub
Private Sub Forward_Click()
On Error Resume Next
WebObj.GoForward
End Sub
Private Sub Refresh_Click()
On Error Resume Next
WebObj.Refresh
End Sub
Private Sub Stop_Click()
On Error Resume Next
WebObj.Stop
End SubThe On Error Resume Next Statement prevents the Subroutines from running into an Error when there are no Web Pages to navigate to.
To test these Buttons, you must click on a few links on the displayed Web Page to open other Pages from the same website and then use these buttons to navigate between those opened pages.
The Home Button will open the Default Home page you set in Internet Explorer's Tools -> Internet Options... Home Page -> Address Control, not the default value you set in the Combo Box in the Browser control.











Great tip, thanks for sharing that! One thing to consider, when you navigate to different web sites using the Back, Forward and Home buttons, the Combo Box is not synchronized to the page. Would be great to have the combo box aware of the site and update accordingly....
ReplyDeleteH
Hello there, I was just traveling the net and came across your site . Thought Id say thanks and tell you that Ive enjoyed my visit here, hope you have a nice morning !
ReplyDelete