I try to many times but failed to get rid of runtime error 13. It shows error in me.filter line. Help will be appreciated
private Sub Form_Open(Cancel As Integer)
Dim arg() As Variant
If Nz(Me.OpenArgs) = 0 Then
Exit Sub
Else
MsgBox (Me.OpenArgs)
If Len(Me.OpenArgs) > 0 Then
arg = Split(Me.OpenArgs, "|")
Me.Filter = "[lab book]=' " & "arg(0)" & "" And "[starting Page]='" & arg(1) & "'"
Me.FilterOn = True
End If
End If
End Sub
CodePudding user response:
Try this - and Page is numeric, I guess:
Private Sub Form_Open(Cancel As Integer)
Dim arg() As Variant
If Not IsNull(Me.OpenArgs) Then
MsgBox (Me.OpenArgs)
arg = Split(Me.OpenArgs, "|")
Me.Filter = "[lab book] = '" & arg(0) & "' And [starting Page] = " & arg(1) & ""
Me.FilterOn = True
End If
End Sub
CodePudding user response:
Private Sub Form_Open(Cancel As Integer)
'an array can be assigned to a variant, but you cannot assign to an array
'Spit returns an array of strings
Dim Arg As Variant 'so make Arg a variant instead of an array of variants
Dim strsql As String 'combining sql strings is very error prone. When debugging print them
If Not IsNull(Me.OpenArgs) Then
'MsgBox (Me.OpenArgs)
Arg = Split(Me.OpenArgs, "|") 'So, now Arg holds the array of strings returned by split
strsql = "[lab book] = '" & Arg(0) & "' And [starting Page] = " & Arg(1)
Debug.Print strsql
'Based on my dummy data: [lab book] = 'science' And [starting Page] = 5
'It is important to wrap your text arguments in apostrophes when you combine them into the sql string or pass them already wrapped
'So check the datatypes in the table or query you are searching
Me.Filter = strsql
Me.FilterOn = True
End If
End Sub