How to Open Forms With A Simple Function That Saves You Time and Work


I like to keep things as simple as possible. After all, I am not an Access expert, just a guy who likes to build Databases. I have learned a lot from the forums and on internet sites. Most of the pros on the forums give really great advice. But the forums were not meant to be tutorials where you get detailed step by step instructions. So I created this blog so we can share what we have learned with each other.

Here is one of my favorite Access tips:


How to Open Forms with a simple Function that saves you time and work.


I was designing a database with a lot of forms. To make it user friendly, I had to add numerous buttons on these forms so the users could easily get to the other forms they needed. Using the Access Button Wizard was adding a lot of code to my Database. It was growing faster than the National Debt. Fortunately I discovered a simple solution to avoid adding all that code and the database bloat that goes with it.


Instead of adding code to your forms to open other forms, you can use this simple Function to open your forms. You can add this Function to an existing Module or create a new module (not a Form, Report, or Class Module but a regular module in the Modules group).


You can start by copying the following code to a Module:

Public Function Openmyfrm(myfrmname As String)
    DoCmd.OpenForm myfrmname
End Function


That was easy wasn’t it? But the best is yet to come. How do you get this Function to open your form? With just one little line in the On Click event of a button!


=Openmyfrm(“YOURFORMNAME”)


That’s it! The Function accepts the form name between the quotes as a String because we told it to do that when we added (myfrmname As String) to the Function. It then uses that form name as a variable with the DoCmd.OpenForm code.


You need to add the =Openmyfrm(“YOURFORMNAME”) expression to the On Click Event of a Button. (I am using all caps to indicate where you need to add the name of your form).


In case you need help, here’s how to add the expression to a button: Open your form in design view. Click on the button on the tool bar and place a new button on your form. The Button Wizard fires up. I just click Cancel and I have a new button ready to use. I change the Caption on the button to something like Open Customers Form.


Then open the button’s property sheet. You can do that by right clicking on the button and selecting “Properties”. Next, click on the “Event” tab. Find the white area beside the On Click Event. Add the expression with the name of the Form you want to open between the quotes.


You are finished! (Don’t forget to add some error handling to your Function).


We will have some more tips later about what else you can do with a Custom Function.


To download free databases and code samples visit our Gaining Access Website.

Get the free Code Tracker™ Beta Add-in to record and then view how your procedures are being used.


More Free Downloads:
Pop-up Calendar
ScopeSight for Access Reports Demo: get the exact data you want on your Access Reports.
Free Church Management Software now with new Contributions management.
Code Samples


Get the Access and Outlook Appointment Manager to manage all of your Outlook Calendar Appointments and Access dated information.


Happy computing,
Patrick (Pat) Wood
Gaining Access

About these ads

4 Comments

  1. Posted March 6, 2008 at 6:03 PM | Permalink |

    You can also use DoCmd.OpenForm(“YOURFORMNAME”) directly in the OnClick event.

  2. Posted July 13, 2008 at 1:40 PM | Permalink |

    I cant open your calendar download…sounds like a good tool. Is the link broken?

  3. Patrick Wood
    Posted July 14, 2008 at 3:59 AM | Permalink |

    Hi,

    I am in the process of moving to a new site so the old link does not work any more. You can download the Calendar at this web address:
    http://gainingaccess.net/GainingAccess/FreeDownloads.aspx

    I have grown really fond of this calendar because it works so well, is easy to use, and, unlike activeX calendars, this one works all the time on any computer.

    I hope you enjoy the calendar.

    Patrick (Pat) Wood

  4. Posted December 23, 2010 at 6:01 PM | Permalink |

    I use the following to open forms. Provides the opportunity to pass arguments such as opening arguments or a where clause if you want.

    ‘—————————————————————————————
    ‘ Procedure : subOpenForms
    ‘ Author : Neville Turbit
    ‘ Date : 04/06/09
    ‘ Purpose : This function is used in the Click event of command buttons that opens forms
    ‘—————————————————————————————

    Public Sub subOpenForms(strFormName As String, Optional strLinkCriteria As String, Optional strQuery As String, Optional strWhere As String)

    On Error GoTo Error_subOpenForms

    ‘————————————————————–
    ‘ Open specified form.
    DoCmd.OpenForm strFormName, , strQuery, strWhere, , , strLinkCriteria

    Exit_subOpenForms:
    On Error GoTo 0
    Exit Sub

    Error_subOpenForms:

    MsgBox “An unexpected situation arose in your program.” & vbCrLf & _
    “Please write down the following details:” & vbCrLf & vbCrLf & _
    “Module Name: modGeneric” & vbCrLf & _
    “Type: Module” & vbCrLf & _
    “Calling Procedure: subOpenForms” & vbCrLf & _
    “Error Number: ” & Err.Number & vbCrLf & _
    “Error Description: ” & Err.Description

    Resume Exit_subOpenForms
    Resume

    End Sub

    I also have one for reports.

    ‘—————————————————————————————
    ‘ Procedure : subOpenReports
    ‘ Author : Neville Turbit
    ‘ Date : 04/06/09
    ‘ Purpose : This function is used in the Click event of command buttons that opens reports
    ‘—————————————————————————————

    Public Sub subOpenReports(strReportName As String, Optional strQuery As String, Optional strWhere As String)

    On Error GoTo Error_subOpenReports

    ‘————————————————————–
    ‘ Open specified form.
    DoCmd.OpenReport strReportName, acPreview, strQuery, strWhere

    Exit_subOpenReports:
    On Error GoTo 0
    Exit Sub

    Error_subOpenReports:
    Select Case Err.Number
    Case 2501 ‘ no data
    Resume Next
    Case Else
    MsgBox “An unexpected situation arose in your program.” & vbCrLf & _
    “Please write down the following details:” & vbCrLf & vbCrLf & _
    “Module Name: modGeneric” & vbCrLf & _
    “Type: Module” & vbCrLf & _
    “Calling Procedure: subOpenReports” & vbCrLf & _
    “Error Number: ” & Err.Number & vbCrLf & _
    “Error Description: ” & Err.Description

    Resume Exit_subOpenReports
    End Select

    End Sub

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: