Land Of Oorang

Or "Things Only I Will Think Are Important"

Blog

OpenArgs

Posted by Oorang on June 5, 2009 at 5:47 AM Comments comments (0)

Migrating... I will be moving this site into the blog section slowly but surely. 


On to the show:

On occasion you need to open a form in Access, and have that form already know something special. (Say a filter.) The generally accepted way istp pass the form the information with an "Open Argument":

DoCmd.OpenForm "Example", OpenArgs:="Table1.[MyField]='1'"


But what if you want to pass more than one argument? Well the bad news is you can pass one, and only one string to the form. So you must cram all of your data into one string and parse the string as needed. If you have ever had to maintain someone else's work you know that if the original Author was in a hurry they probably just tacked this and that on as they went, and left you with a 5 or 6 custom string parsing procedures across 2 or 3 databases. This can be, in a word, painful.

One way you can spare your successors  this agony, is to use a (somewhat)self-documenting design pattern. A structured way to approach this problem is select a universal argument delimiter, and a common way to access the arguments.

Example follows:


Option Explicit

Public Enum eForm1Args
eFilter = 0
eIsSpecial = 1
End Enum

Private m_strArgs() As String
Private m_lngArgsUprBnd As Long

Public Property Get Args(ByVal eForm1Args As eForm1Args) As String
If eForm1Args <= m_lngArgsUprBnd Then
Args = m_strArgs(eForm1Args)
End If
End Property

Public Sub SetArgs(ByVal value As String)
m_strArgs = Split(value, "|")
m_lngArgsUprBnd = UBound(m_strArgs)
End Sub

Private Sub Form_Open(Cancel As Integer)
m_strArgs = Split(Nz(Me.OpenArgs, vbNullString), "|")
'Example use of Args Property:
If LenB(Me.Args(eFilter)) Then Me.Filter = Me.Args(eFilter)
End Sub
Private Sub Command1_Click()
'Example use of Args Property:
If LCase$(Me.Args(eIsSpecial)) = "true" Then
'Do something special
End If
End Sub