OpenArgs that contain "," in variable

J

jim

Hi, how can I pass OpenArgs(a string) that has a comma in it? In the pop-up
form, that I pass the OpenArgs to, I put the two arguments in an array and so
it comes to the first comma and separates my string, which is the first
element of the array, into OpenArgs(0) and OpenArgs(1). What I need to
happen is the string should all go into OpenArgs(0) and the next element, an
integer, go into OpenArgs(1). Here's my code:

Private Sub Option96_Click()
Dim strOpenArgs As String
strOpenArgs = Me.txtCustomerName & "," & Me.txtBuildingNo
On Error Resume Next 'trap error 2501
DoCmd.OpenForm "sfrmClientBuildContacts", , , "[CustomerName] = """ &
Me!CustomerName & """ And [BuildingNo] = " & Me!BuildingNo, , , strOpenArgs
If Err.Number = 2501 Or Err.Number = 0 Then ' ignore the error
Else ' tell the user about the error
MsgBox Err.Number & " " & Err.Description
End If
End Sub

Do I have to put quotes around -Me.txtCustomerName &- when it's assigned to
strOpenArgs? I tried it several ways and can't get it right.
TIA
 
S

Stefan Hoffmann

hi Jim,

Hi, how can I pass OpenArgs(a string) that has a comma in it?
I would not use a comma, as it may to often occur as content char. I'm
normally using the pipe symbol or Chr(0).
Here's my code:

Public Function SqlQuote(AString As String, _
Optional ADelimiter As String = "'" _
) As String

SqlQuote = ADelimiter & _
Replace(AString, ADelimiter, ADelimiter & ADelimiter) & _
ADelimiter

End Function

Private Sub Option96_Click()

On Local Error GoTo LocalError

Dim strOpenArgs As String
Dim filter As String

filter = "[CustomerName] = " & SqlQuote(Me![CustomerName] & " " & _
"AND [BuildingNo] = " & Me![BuildingNo]

strOpenArgs = Me.txtCustomerName & "|" & Me.txtBuildingNo
DoCmd.OpenForm "sfrmClientBuildContacts", , , Filter, , , strOpenArgs

Exit Sub

LocalError:
If Err.Number <> 2501 Then
MsgBox Err.Number& " "& Err.Description
End If

End Sub

In sfrmClientBuildContacts:

Private Sub Form_Open(Cancel As Integer)

On Local Error GoTo LocalError

Dim args() As String

args = Split(Me.OpenArgs, "|")

MsgBox args(0) & vbCrLf & args(1)

Cancel = UBound(args) <> 1

LocalError:
'Cancel = True
'MsgBox error

End Sub


btw, your code is not really readable... also copy and past your code
after running Debug/Compile.

mfG
--> stefan <--
 
M

Mark Andrews

Why not just use a diff delimiter? I use "|". Of course I usually just
pass IDs in openargs.


'if OpenArgs exist then populate the appropriate fields
'Used by various forms that open this form
'openargs (contactID|employeeID|CampaignID|EventID) use zero to indicate
no value
Dim MyArray() As String
If Not IsNull(Me.OpenArgs) Then
MyArray = Split(Me.OpenArgs, "|")

If (MyArray(0) <> "0") Then
Me.ContactID = CLng(MyArray(0))
End If
If (MyArray(1) <> "0") Then
Me.EmployeeID = CLng(MyArray(1))
End If
If (MyArray(2) <> "0") Then
Me.CampaignID = CLng(MyArray(2))
End If
If (MyArray(3) <> "0") Then
Me.EventID = CLng(MyArray(3))
End If

End If
 

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

Top