Can I send a variable to a Form when I open it?

  • Thread starter Thread starter AbraAbraCadabra
  • Start date Start date
A

AbraAbraCadabra

If I programatically open a form like this...

docmd.openform "form2", acnormal

Is there a way to send a variable value with it so that it knows
what I want it to do? For example...

docmd.openform "form2", acnormal, X="UseBothInventories"

Thanks!
 
The OpenForm method has an OpenArgs argument:
DoCmd.OpenForm "form2", acNormal, , , , , "UseBothInventories"

....and in the form's Open event:
Dim strMyArgument As String

strMyArgument = Me.OpenArgs

If strMyArgument = "UseBothInventories" Then
'do whatever
Else
'do whatever
End If

Regards,
Graham R Seach
Microsoft Access MVP
Canberra, Australia
 
if you want to pass more than 1 argument this code i found was very
useful

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

Called as so : toDate = GetTagFromArg(Me.OpenArgs, "To")

credit for the code goes to http://www.vishalon.net

Vishal Monpara
 
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

Called as so : toDate = GetTagFromArg(Me.OpenArgs, "To")

Could someone give me an example of how to use the above code?
Is it like this?
GetTagFromArg("blue,red,green,yellow",",")
 
AbraAbraCadabra said:
Could someone give me an example of how to use the above code?
Is it like this?
GetTagFromArg("blue,red,green,yellow",",")

No.

The assumption is that you passed multiple parameters, with each parameter
named, something like:

Customer=123:Supplier=345:Product=3565

You'd then put code in your form's Open event to store those 3 values in
appropriate variables, remembering that the string above will be in the
OpenArgs property of the form, like:

lngCustomer = CLng(GetTagFromArg(Me.OpenArgs, "Customer"))
lngSupplier = CLng(GetTagFromArg(Me.OpenArgs, "Supplier"))
lngProduct = CLng(GetTagFromArg(Me.OpenArgs, "Product"))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top