Creating a Master / Detail Form in VB

WALI SYSTEMS INC.

Table of Contents

  • Overview
  • Code Details
  • Steps to install the application
  • Contact
  • Disclaimer

1. OVERVIEW

This document will show you how to create a Master/Detail form in Visual Basic, those of you who have worked in Access may already have used Master/Detail form (Sub forms). Visual Basic programmers always felt handicapped by the fact that there are no Sub-Forms in Visual Basic. This document will show you how this can be achieved in Visual Basic, you will also learn how to use RDO. In the next article we will show you how to create a Master/Detail form using the relatively newer technology known as ActiveX Data Objects (ADO). Although you can use an Access database with this article, we have included the SQL script which will generate all the required tables for this articles, you need to have atleast SQL Server 6.5 in order to use the code.

2. CODE DETAILS

The detail form in this program is the grid controls. When the form first loads, the m_Resv member is assigned to CdataObjects. CdataObjects gets initialized, and the ODBC and SQL Server database connections are made. Then the FillControl procedure is called, and it processes the SQL statement. The procedure fills the cboHotel combo box with a list of the hotel names. When the FillControl procedure sets the first item in the list as the selected item, the cboHotel_Click event is performed.

The cboHotel_Click event uses the combo box’s selected items as part of the where criteria of the SQL statement. This event calls the FillGrid procedure twice. Once is to fill the grdRoomInfo grid, and the other is to fill the grdResv grid.

When the OK button is clicked, the m_Resv member is set to nothing, which causes the Class_Terminate event to be processed. This closes all your connections to the database.

3. STEPS TO INSTALL THE APPLICATION

  1. Create a database in the SQL Server, follow the steps below if you don’t know how to create a database in the SQL Server:
    1. Open your SQL Server Enterprise Manager and click on the new device option in the left pane, create a device with the name of “Hotel” and assign 5 MB space to the device.
    2. Click on the new database option in the left pane and select the newly created device “Hotel”, write a name for your database in the “Database name” option, you can name it as “dbHotel”.
    3. Open your SQL query tool and run the following files one by one, these will create the tables and insert data into these tables, these files are provided with the application.
  2. Run the files in the sequence given below:
    1. dbHotel.sql
    2. Filhotel.sql
    3. Ht107a.sql
    4. Ht107b.sql
  3. Once, the database is created, create a SQL Server DSN named as “dsnmdf” and point it towards the database (dbHotel).
  4. Open the application in the Visual Basic and open the file “CdataObj.cls” and look for the class_initialize routine, you will find the following line in the class code:
  5.    'Open the connection
       Set m_con = m_env.OpenConnection dsName:="dsnmdf", _
           Connect:="UID=sa;pwd=")
    

    Change the DSN name to the name of the DSN you created, provide the correct User ID and password of you SQL Server in the connect string.

  6. Save the changes and run the application by pressing F5.

CONTACT

Contact us for any help regarding the application, our email address is: tarhoni@hotmail.com

5. DISCLAIMER

This application is not intended for experienced VB users, it is meant only to demonstrate the VB6 usage to the new VB programmers. This code was provided to help the new programmers catch up with the fast growing technology. This code is a public property and we completely disown this code once it is submitted to any internet site. The users are allowed to copy and paste the code in their own applications.

Download Zipped Project Files (15k)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read