Results 1 to 3 of 3
  1. #1
    yemisi is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2019
    Posts
    1

    Subscript out of range when opening a form

    I have a form that gives and error "subscript out of range" before it even opens. i used f8 for debugging and I realised that the value of tmpArgs gave the error but I cannot trace the error after this. Please see the code below.

    Private Sub Form_Open(Cancel As Integer)
    Dim tmpArgs As Variant, ffile As String, st1 As String

    tmpArgs = Split(Nz(Me.OpenArgs, ""), "|") 'where the code stops running
    pth1 = ""
    If Not IsNull(tmpArgs) Then
    pth1 = tmpArgs(0)


    st1 = tmpArgs(1)
    End If

    If pth1 > "" Then
    Call ListFiles(pth1, st1 & "*.*", True)
    Else
    Cancel = True
    MsgBox "There isn't any attached file for this task."
    End If
    End Sub

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,410
    Code:
    Private Sub Form_Open(Cancel As Integer) Dim tmpArgs As Variant, ffile As String, st1 As String
    debug.print "Openargs=" & me.openargs
    tmpArgs = Split(Nz(Me.OpenArgs, ""), "|") 'where the code stops running pth1 = "" If Not IsNull(tmpArgs) Then pth1 = tmpArgs(0) st1 = tmpArgs(1) End If If pth1 > "" Then Call ListFiles(pth1, st1 & "*.*", True) Else Cancel = True MsgBox "There isn't any attached file for this task." End If End Sub
    Add the red code to see OpenArgs.
    The code that opens the form must specify the openargs like this:
    Docmd.Openform "frmWhatever",,,,,,"Hello"

    In this case, the form_open event would see "Hello" as the openargs.

    If the form is opened automatically at BOJ via the current DB settings Option, it won't have any openargs, so if you recently added that option, openargs will be null.
    If you open the form via clicking in the Navigation Pane, the openargs will be null.

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,799
    davegri is probably on the right track and it seems you have intended to deal with the possibility that OpenArgs is null because of the Nz function. However, in this situation you need to re-route code execution if that is the case, not convert it to zls. Split returns an array with no elements and no data if the string is zls. Perhaps along the lines of

    Dim tmpArgs As String, ffile As String, st1 As String

    If Not Nz(Me.OpenArgs,"") = "" Then tmpArgs = Split(Nz(Me.OpenArgs, ""), "|")
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subscript out of range
    By wpryan in forum Forms
    Replies: 2
    Last Post: 11-19-2015, 11:18 AM
  2. Subscript out of range - New front end form.
    By Homegrownandy in forum Forms
    Replies: 1
    Last Post: 09-30-2015, 06:26 AM
  3. Sometimes get subscript out of range
    By khughes46 in forum Import/Export Data
    Replies: 8
    Last Post: 06-13-2014, 10:22 AM
  4. subscript out of range
    By philfer in forum Access
    Replies: 3
    Last Post: 08-17-2013, 01:22 PM
  5. Subscript out of range (9)
    By msche398 in forum Programming
    Replies: 1
    Last Post: 07-08-2011, 12:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums