Presentation is loading. Please wait.

Presentation is loading. Please wait.

VBA Form Techniques. Open a form from another form Private Sub cmdSupplierForm_Click() DoCmd.OpenForm "frmSupplierDetails" End Sub Code in Standard module.

Similar presentations


Presentation on theme: "VBA Form Techniques. Open a form from another form Private Sub cmdSupplierForm_Click() DoCmd.OpenForm "frmSupplierDetails" End Sub Code in Standard module."— Presentation transcript:

1 VBA Form Techniques

2 Open a form from another form Private Sub cmdSupplierForm_Click() DoCmd.OpenForm "frmSupplierDetails" End Sub Code in Standard module Public Function FormOpen(strName As String) DoCmd.OpenForm strName End Function Code in Cass module

3 OpenForm Method Method of DoCmd object Syntax: DoCmd.OpenForm(FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs) Arguments: –FormName Valid name of form in current database (Required – all other arguments are optional) –View acFormView constant e.g.: acNormal (default) acDesign –FilterName Valid name of query in current database –WhereCondition SQL WHERE clause (without the word WHERE) –DataMode The data entry mode for the form acFormOpenDataMode constant e.g.: acFormAdd acFormEdit acFormPropertySettings (default) –WindowMode The window mode in which the form opens acWindowMode constant e.g.: acWindowNormal (default) –OpenArgs use to set OpenArgs property of form

4 Using named arguments DoCmd.OpenForm(FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs) Want to set value of WhereCondition argument Default values acceptable for all other arguments Method 1: Use commas as placemarkers for omitted arguments DoCmd.OpenForm “frmSupplierDetails”,,, “CoID=“ & txtCoID Method 2: Use named arguments DoCmd.OpenForm “frmSupplierDetails”, WhereCondition := “CoID=“ & txtCoID

5 Code to list forms in database Private Sub Form_Load() Dim objAO As AccessObject Dim objCP As Object Dim strValues As String Set objCP = Application.CurrentProject For Each objAO In objCP.AllForms strValues = strValues & objAO.Name & ";" Next objAO lstForms.RowSourceType = "Value List" lstForms.RowSource = strValues End Sub

6 NotInList Event Occurs when user enters value in Combo box that is not in Combo box list Limit to list property must be set to “Yes” for this event to occur Private Sub cboBoxName_NotInList (NewData As String, Response As Integer) –NewData – the text entered by the user –Response – indicates how the event was handled acDataErrDisplay (default) – displays default error message acDataErrContinue – use to display custom message acDataErrAdded – does not display message. Enables you to add value to list.

7 NotInList Example 1 (adding new value to value list) Private Sub cboEmpTitle_NotInList(NewData As String, Response As Integer) Dim cbo As Control Set cbo = Me.cboEmpTitle If MsgBox(NewData & " is not in list - Do you want to add this value?", vbOKCancel) = vbOK Then Response = acDataErrAdded cbo.RowSource = cbo.RowSource & ";" & NewData Else Response = acDataErrContinue cbo.Undo End If End Sub

8 Private Sub cboOrigin_NotInList(NewData As String, Response As Integer) Dim intNew As Integer, strCountry As String, rst As Recordset intNew = MsgBox("Add country " & NewData & " to list?", vbYesNo) If intNew = vbYes Then Set rst = CurrentDb.OpenRecordset("tblCountryLookup") rst.AddNew rst!Country = NewData rst.Update Response = acDataErrAdded Else MsgBox ("The country you entered isn't in the list. Select a country from the list or enter a value to add") DoCmd.RunCommand acCmdUndo Response = acDataErrContinue End If End Sub NotInList Example 2 (adding new record to lookup table)


Download ppt "VBA Form Techniques. Open a form from another form Private Sub cmdSupplierForm_Click() DoCmd.OpenForm "frmSupplierDetails" End Sub Code in Standard module."

Similar presentations


Ads by Google