Need generic function to extract an OPENARGS parameter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Anybody have such a function eg:
parm = OpenargsExtract(2)

where '2' is the second parameter, and if there is only say 1 parameter
passed then OpenargsExtract will not fall over.
 
Hi, Thomas.

In a standard module, try:

'=================================================
' This function extracts the substring value at an ordinal position from
the form's
' OpenArgs Property. The substring values are expected to be delimited by a
' comma. nth is the ordinal value in the OpenArgs string to retrieve the
' substring from. No string will be passed back from this function when
there is
' no OpenArgs Property and when nth exceeds the number of substrings in
' the OpenArgs Property.
'=================================================

Public Function getOpenArg(sOpenArgs As String, nth As Long) As String

On Error GoTo ErrHandler

Dim pos As Long
Dim idx As Long
Dim nStart As Long

nStart = 1

For idx = 1 To nth
pos = InStr(nStart, sOpenArgs, ",", vbDatabaseCompare)

If (idx = nth) Then
If (pos = 0) Then
getOpenArg = Mid$(sOpenArgs, nStart, Len(sOpenArgs) - nStart
+ 1)
Exit For
ElseIf (pos > 0) Then
getOpenArg = Mid$(sOpenArgs, nStart, pos - nStart)
Exit For
End If
ElseIf (pos = 0) Then
Exit For ' Ordinal # exceeds # of OpenArgs.
End If

nStart = pos + 1
pos = 0
Next idx

Exit Function

ErrHandler:

MsgBox "Error in getOpenArg( )." & vbCrLf & vbCrLf & _
"Error #" & Err.nthber & vbCrLf & vbCrLf & Err.Description
Err.Clear
getOpenArg = ""

End Function ' getOpenArg( )

In the form's module, try:

Private Sub Form_Open(Cancel As Integer)

On Error GoTo ErrHandler

Dim sSomeVal As String

If (Len(Me.OpenArgs) > 0) Then
sSomeVal = getOpenArg(Me.OpenArgs, 2)
MsgBox "*" & sSomeVal & "*"
End If

Exit Sub

ErrHandler:

MsgBox "Error in Form_Open( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub


HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Dang. Let me redo that pasting job.

In a standard module, try:

'======================================
' This function extracts the string value at an ordinal position
' from the form's OpenArgs Property. The string values are
' expected to be delimited by a comma. nth is the ordinal
' value in the OpenArgs string to retrieve the substring from.
' No string will be passed back from this function when there
' is no OpenArgs Property and when nth exceeds the number
' of substrings in the OpenArgs Property.
'======================================

Public Function getOpenArg(sOpenArgs As String, nth As Long) As String

On Error GoTo ErrHandler

Dim pos As Long
Dim idx As Long
Dim nStart As Long

nStart = 1

For idx = 1 To nth
pos = InStr(nStart, sOpenArgs, ",", vbDatabaseCompare)

If (idx = nth) Then
If (pos = 0) Then
getOpenArg = Mid$(sOpenArgs, nStart, _
Len(sOpenArgs) - nStart + 1)
Exit For
ElseIf (pos > 0) Then
getOpenArg = Mid$(sOpenArgs, nStart, pos - nStart)
Exit For
End If
ElseIf (pos = 0) Then
Exit For ' Ordinal # exceeds # of OpenArgs.
End If

nStart = pos + 1
pos = 0
Next idx

Exit Function

ErrHandler:

MsgBox "Error in getOpenArg( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
getOpenArg = ""

End Function ' getOpenArg( )

In the form's module, try:

Private Sub Form_Open(Cancel As Integer)

On Error GoTo ErrHandler

Dim sSomeVal As String

If (Len(Me.OpenArgs) > 0) Then
sSomeVal = getOpenArg(Me.OpenArgs, 2)
MsgBox "*" & sSomeVal & "*"
End If

Exit Sub

ErrHandler:

MsgBox "Error in Form_Open( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub


HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
DANG! About a month or so ago, I was trying to pass two arguements through the
OpenArgs property. I ended up separating the two arguements by running OpenArgs
through my name parsing routines. That only worked because I was in complete
control of the values I was passing. This looks like a much better solution!

Thanks for posting this, Gunny!

Regards,
RD
 
Here's code that I have from a Form_Open() event, it should get you
started.

If IsNull(Me.OpenArgs) = False Then
openArgsArray = Split(Me.OpenArgs, ",")
strAccountType = openArgsArray(1)
lngAccountID = openArgsArray(2)
strRecordSource = openArgsArray(3)
End If
 
Why not

function getOpenArg(strOpenArgs, intPosition)

If IsNull(strOpenArgs) = False and isNumeric(intPosition) Then
openArgsArray = Split(Me.OpenArgs, ",")
getOpenArg = openArgsArray(intPosition)
End If

End function
 
You're very welcome. Next time you need an algorithm that sounds like it
might be generic, check the Google Groops archive
(http://groups-beta.google.com/advan...1&as_maxd=11&as_maxm=7&as_maxy=2005&safe=off&)
and/or the Microsoft Online Community knowledge base of previously posted
questions identified as having answers that work
(http://www.microsoft.com/office/com...S&guid=&sloc=en-us&dg=microsoft.public.access)
to see whether someone has already posted a solution that you can reuse, so
you don't spend time reinventing the wheel.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.
 
Hi, David.
Why not

function getOpenArg(strOpenArgs, intPosition)

Don't ask unless you really want to know. ;-)

In Tom's words, it will "fall over." Anyone copying and pasting your code
into his database application will need to add code to fix it before
compiling it. And then he'll have to think about how it would be used in
the application so that he can alter it to get the most reliability, most
flexibility, and the best code reusage out of it.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.
 
I figured that.

'69 Camaro said:
Hi, David.




Don't ask unless you really want to know. ;-)

In Tom's words, it will "fall over." Anyone copying and pasting your code
into his database application will need to add code to fix it before
compiling it. And then he'll have to think about how it would be used in
the application so that he can alter it to get the most reliability, most
flexibility, and the best code reusage out of it.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.
 
Back
Top