Click here to Skip to main content
6,935,947 members and growing! (18,719 online)
Email Password   helpLost your password?
 
Languages » VB.NET » HowTo     Intermediate

MS Access: Pass Variable Values From One Form to Another

By Vishal Monpara

The function helps you to pass multiple variable values from one form to another.
VB, Windows, .NET, Visual-Studio, Dev
Posted:5 Nov 2005
Views:81,625
Bookmarked:18 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.54 Rating: 3.00 out of 5
2 votes, 28.6%
1
1 vote, 14.3%
2

3
2 votes, 28.6%
4
2 votes, 28.6%
5

Introduction

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.

Solution

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
   Next
   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.

Conclusion

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

License

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


Member
For more info visit http://www.vishalon.net
Occupation: Web Developer
Location: United States United States

Other popular VB.NET articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
QuestionForm with 3 reports Pinmemberjbdigit21:41 11 Dec '08  
GeneralPassing Values from one form to Another using c#.net Pinmemberkotrasubba20:07 11 Aug '08  
GeneralRe: Passing Values from one form to Another using c#.net PinmemberKamala.A.K7:09 27 Oct '08  
Generalset global variables Pinmemberuplam12:02 25 Sep '07  
Questionrequire urgent solution Pinmembervishal_18in1:41 5 Jan '07  
Questionpassing other data types Pinmemberbobo_t15:50 27 Oct '06  
Questionretrieving variable values Pinmemberhimmelaufer12:21 20 Sep '06  
AnswerRe: retrieving variable values PinmemberVishal Monpara7:55 21 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 5 Nov 2005
Editor: Rinish Biju
Copyright 2005 by Vishal Monpara
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project