Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split Function not working

Kennelbloke

Technical User
May 22, 2015
40
AU
I have used this process many times before but I'm can't get this to work
main form:
(button click event) ...
strSexVal = 2
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenForm "frmPetDetails2", acNormal, , , acFormAdd, , Me!PetID & "|" & strSexVal

frmPetDetails2 form:
Private Sub Form_Load()
Public varPrevPet As Variant
Public varWhichSex As Variant
varPrevPet = Split([me.OpenArgs], "|")(0)
varWhichSex = Split([me.OpenArgs], "|")(1)
End Sub

However I keep getting a runtime error 2465 ."can't find the field '|2' referred to in your expression" when trying to Split.
Debug on frmPetDetails2 openargs shows 47418|2. I've tried "|" (pipe), semicolon etc same result.

I have decompiled and even created a new DB just in case of corruption. Got no hair left, this should be simple...
 
Solution
My guess is you're trying to use OpenArgs (the information you passed to the form) too early in the form's setup process. The Form_Load event happens very soon after the form starts, and while OpenArgs is available, it sometimes doesn't behave correctly at that point.

To fix this, move the code that handles OpenArgs to a slightly later stage, like the Form_Open event.

Code:
Private varPrevPet As Variant
Private varWhichSex As Variant

Private Sub Form_Open(Cancel As Integer)
    If Not IsNull(Me.OpenArgs) Then
        Dim args() As String
        args = Split(Me.OpenArgs, "|")
        If UBound(args) >= 1 Then
            varPrevPet = args(0)
            varWhichSex = args(1)
        Else
            MsgBox "Invalid arguments passed to...
You could try use Left() and Mid() in place of Split(). Not ideal but I don’t always need ideal 🙃
 
My guess is you're trying to use OpenArgs (the information you passed to the form) too early in the form's setup process. The Form_Load event happens very soon after the form starts, and while OpenArgs is available, it sometimes doesn't behave correctly at that point.

To fix this, move the code that handles OpenArgs to a slightly later stage, like the Form_Open event.

Code:
Private varPrevPet As Variant
Private varWhichSex As Variant

Private Sub Form_Open(Cancel As Integer)
    If Not IsNull(Me.OpenArgs) Then
        Dim args() As String
        args = Split(Me.OpenArgs, "|")
        If UBound(args) >= 1 Then
            varPrevPet = args(0)
            varWhichSex = args(1)
        Else
            MsgBox "Invalid arguments passed to the form.", vbExclamation, "Error"
        End If
    Else
        MsgBox "No arguments passed to the form.", vbExclamation, "Error"
    End If
End Sub
 
Solution
Thanks Software RT, that was it. It seems strange that code pause on the error and debug returned the correct value, which to me says that the object was there. Wierd...

Oh well on to finishing this up.

Thanks for the responses guys and have a great day.
 

Part and Inventory Search

Sponsor

Back
Top