title: "Subscript out of range" error in VBA using array of strings
body: <p>I learned C# and VB.Net in school, but not VBA.  </p>
  
  <p>I am trying to automate a pipe-delimited .csv export in Access. The user selects a ship date from a combobox and clicks "export", and the application needs to do the rest.  The problem is, it is exporting the dates in short date format (m/d/yyyy) no matter how I have them formatted in Access; the client's required format is mmddyyyy.  </p>
  
  <p>I tried appending the date to a text field in another table but Access won't let me do that.  So I placed a textbox on the form and typed the date in the correct format and used that to populate the table to be exported, and that works.  </p>
  
  <p>What I am trying to do now is write a subroutine to populate the textbox in the correct format whenever the user changes the combobox.  I keep getting "Runtime Error 9: Subscript out of range" on the following line: <code>If Len(dateParts(0)) = 2 Then</code></p>
  
  <p>Here is the code I have so far:</p>
  
  <pre><code>Private Sub tbxShipDate_Change()
      Dim strShipDate As String
      Dim strTextDate As String
      Dim dateParts() As String
  
      strShipDate = Me.tbxShipDate.Value
  
      If Len(strShipDate) = 10 Then
          strTextDate = strShipDate
          strTextDate = Replace(strTextDate, "/", "")
      Else
          dateParts = Split(strShipDate, "/")
  
          'check month format
          If Len(dateParts(0)) = 2 Then
              strTextDate = dateParts(0)
          Else
              strTextDate = "0" &amp; dateParts(0)
          End If
  
          'check day format
          If Len(dateParts(1)) = 2 Then
              strTextDate = strTextDate &amp; dateParts(1)
          Else
              strTextDate = strTextDate &amp; "0" &amp; dateParts(1)
          End If
  
          'add year
          strTextDate = strTextDate &amp; dateParts(2)
      End If
  
      Me.tbxTextDate.Value = strTextDate
  
  End Sub
  </code></pre>