Click here to Skip to main content
13,257,236 members (54,444 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 5 Nov 2005

MS Access: Pass Variable Values From One Form to Another

, 5 Nov 2005
Rate this:
Please Sign up or sign in to vote.
The function helps you to pass multiple variable values from one form to another.


A few days back, I was creating a small prototype for my project using MS Access. I was required to pass values to other forms. I searched everywhere to find out a way to pass values but couldn't succeed. The only hint I got was using Me.OpenArgs argument variable it is possible to pass values across forms.

Example of passing a single variable using the Me.OpenArgs argument

To pass the value "2" from a form frmOne to frmTwo, we use the following code:

DoCmd.OpenForm "frmTwo", acNormal, , , , acWindowNormal, "2"

In the PageLoad event of frmTwo, we can get this value using the following code:

Dim i as Integer
i = CInt(Me.OpenArgs)

The problem with this simple method is that we can pass only one value at a time. If we want to pass more values, we must have some kind of a delimiter and make a string of all values, but it requires the arguments to be passed in a specific order.


I wrote a small function that extracts values from the passed value string without requiring the values to be passed in a particular order.

This function requires that we pass values in a format like "Var1=Val1:Var2=Val2". Here each value passed will have a corresponding variable name and the variable name/value pair will be delimited by ":". The function will split the argument using the delimiter and extract the value of a particular variable. The code for the function is given below:

Public Function GetTagFromArg(ByVal OpenArgs As String, _
                               ByVal Tag As String) As String
    Dim strArgument() As String
    strArgument = Split(OpenArgs, ":")
    Dim i As Integer
    For i = 0 To UBound(strArgument)
     If InStr(strArgument(i), Tag) And _
                 InStr(strArgument(i), "=") > 0 Then
       GetTagFromArg = Mid$(strArgument(i), _
                        InStr(strArgument(i), "=") + 1)
       Exit Function
     End If
   GetTagFromArg = ""
End Function

Now to pass the value "2" from form frmOne to frmTwo we need to write the following code:

DoCmd.OpenForm "frmTwo", acNormal, , , , acWindowNormal, "Count=2"

and in frmTwo we can get the value in the FormLoad event:

Dim i as Integer
i = CInt(GetTagFromArg(Me.OpenArgs, "Count" ))

Using this trick, we can pass any number of values and the order of the values is not important.


This is an extremely simple method that came to my mind at that time. If you have any better solution, please let me know.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Vishal Monpara
Web Developer
United States United States
For more info visit

You may also be interested in...


Comments and Discussions

GeneralAlternate method for passing variables between forms Pin
PaulTKH28-Apr-11 8:24
memberPaulTKH28-Apr-11 8:24 
GeneralThis is pretty good but could be better... Pin
slingshot32211-Mar-11 5:57
memberslingshot32211-Mar-11 5:57 
GeneralMy vote of 5 Pin
bitcom29-Jun-10 2:25
memberbitcom29-Jun-10 2:25 
QuestionForm with 3 reports Pin
jbdigit11-Dec-08 21:41
memberjbdigit11-Dec-08 21:41 
GeneralPassing Values from one form to Another using Pin
kotrasubba11-Aug-08 20:07
memberkotrasubba11-Aug-08 20:07 
GeneralRe: Passing Values from one form to Another using Pin
Kamala.A.K27-Oct-08 7:09
memberKamala.A.K27-Oct-08 7:09 
Generalset global variables Pin
uplam25-Sep-07 12:02
memberuplam25-Sep-07 12:02 
Questionrequire urgent solution Pin
vishal_18in5-Jan-07 1:41
membervishal_18in5-Jan-07 1:41 
Questionpassing other data types Pin
bobo_t27-Oct-06 15:50
memberbobo_t27-Oct-06 15:50 
Questionretrieving variable values Pin
himmelaufer20-Sep-06 12:21
memberhimmelaufer20-Sep-06 12:21 
AnswerRe: retrieving variable values Pin
Vishal Monpara21-Sep-06 7:55
memberVishal Monpara21-Sep-06 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 5 Nov 2005
Article Copyright 2005 by Vishal Monpara
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid