Microsoft Access 2010 tutorial:Docmd to open form


VI. DoCmd.OpenForm

DoCmd.OpenForm is a method used to open a form from another form. It has the following syntax:

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:

Access 2010 Docmd.OpenForm

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:

Access 2010 employee's information

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:

Access 2010 Docmd.OpenForm

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:

- FrmViewBook:

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"
End Sub

Private Sub lstBookData_DblClick(Cancel As Integer)
     DoCmd.OpenForm "FrmBookInfo", acNormal, , , , , lstBookData.Column(0,     lstBookData.ListIndex + 1)
End Sub

- FrmBookInfo:

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.GoToControl "BookID"
        DoCmd.FindRecord strBookID, , True, , True, , True
    End If
End Sub

The figure of the two forms:

Access Book Form 


asdada comment


I wanna work in my project, when click a button , open new form and the new form should show that the other form's related values.


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.