DoCmd.OpenForm (FormName, View, Filter, WhereCondition, DataMode, WindowMode, OpenArgs)
- FormName: It is the name of a form in the current database. It is a required argument. It must be written between double quotes.
- View: There are a few types of form's view such as acDesign, acNormal, acPreview, acLayout and so on. You can choose one among those types to specify your form when it opens.
- Filter: A query's name is in the current database. You must set it between double quotes.
- WhereCondition: Where the SQL clause without the word WHERE is applied. You must set it between double quotes.
- DataMode: This argument allows you to specify the data entry mode for your form. You can add data, edit data, or you can just read the data.
- WindowMode: You can specify the window mode when the form open. You can open a form as acDialog, acHidden, acIcon or acWindowNormal.
+ acDialog: The open form cannot move.
+ acHidden: The open form is hidden.
+ acIcon: The open form displays as an icon.
+ acWindowNormal: The open form displays as the view form.
- OpenArgs: This argument is used to specify the specific record when a form open. You must set it between double quotes.Note: All arguments in DoCmd.OpenForm syntax are optional except FormName argument.
Ex1: You have a form as the figure below:
The form expresses that the employee that has ID equal to Emp004 that is the one who issued the invoice(Inv002). If you want to know about the detail information of the employee, you need to click View Employee Info button on the form. After you click this button, it displays a form like the following figure:
To make the View Employee Info button perform the action above, you need to apply the VBA Code on View Employee Info button. Before you apply the VBA Code, you should rename it as CmdView; and then type the following VBA Code:
Ex2: You have two forms: FrmViewBook and FrmBookInfo. The FrmViewBook lists all books in the List Box. Each book shows only BookID, and Title. When you want to see the detail of any book, double-click it in the List Box. The FrmBookInfo opens and displays the detail of the book. To do this task, you have to apply the VBA code in FrmViewBook and FrmBookInf as the following:
Private Sub Form_Load()
'Retrieve data from TblBook into List Box
lstBookData.RowSourceType = "Table/Query"
lstBookData.ColumnHeads = True
lstBookData.ColumnCount = 2
lstBookData.RowSource = "Select BookID, Title from TblBook"
Private Sub lstBookData_DblClick(Cancel As Integer)
DoCmd.OpenForm "FrmBookInfo", acNormal, , , , , lstBookData.Column(0, lstBookData.ListIndex + 1)
Private Sub Form_Open(Cancel As Integer)
Dim strBookID As String
' If OpenArgs property contains BookID, find
' corresponding Book record and display it on form. For
' example,if the OpenArgs property contains double-clicked record,
' move to first double-clicked record.
trBookID = Forms!FrmBook.OpenArgs
If Len(strBookID) > 0 Then
DoCmd.FindRecord strBookID, , True, , True, , True
The figure of the two forms:
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: