Compile Error that I can't seem to get around in Button Click function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kmarks247
    New Member
    • Jun 2017
    • 8

    Compile Error that I can't seem to get around in Button Click function

    I have the following code that I've built thru some help and searching here on this forum (Thanks a million to all the great help here). I'm getting a Compile error: Invalid outside procedure message box on the "Set objFileDialog =" line.
    Code:
    Dim strPath As String
    Dim strFile As String
    Dim strTable As String
    Dim strSpecification As String
    Dim intImportType As AcTextTransferType
    Dim blnHasFieldNames As Boolean
    Dim objFileDialog As Office.FileDialog
    
    Set objFileDialog = Application.FileDialog(MsoFileDialogType.msoFileDialogFolderPicker)
     
    '**** Modify these values as needed ****
    strTable = "Daily Aeppays Consolidated"
    strSpecification = "DailyAeppayImport"
    blnHasFieldNames = True
    intImportType = acImportDelim
    '***************************************
     
    'Let user select a folder
    With objFileDialog
      .AllowMultiSelect = False     'Meaningless, but set anyway
      .ButtonName = "Folder Picker"
      .Title = "Folder Picker"
         If .Show Then
           strPath = .SelectedItems(1) & "\"
     
           'Loop through the text files
           strFile = Dir(strPath & "*.txt")
             Do While strFile <> ""
               'For Testing purposes only
               DoCmd.TransferText intImportType, , strTable, strPath & strFile, False
             strFile = Dir
             Loop
         End If
    End With
    Any idea where to move it or how to get around this? I need this code to work on the click of the button which it begins to, but then hits the error on this particular line.

    Thanks!
    Last edited by NeoPa; Jun 28 '17, 01:23 AM. Reason: Added mandatory [CODE] tags.
  • baderms
    New Member
    • Jun 2017
    • 7

    #2
    Have you referenced the object library. If not, from the code window select tools and select Microsoft Office xx Object Library, then it will compile.

    Comment

    • baderms
      New Member
      • Jun 2017
      • 7

      #3
      you also need to make sure it's in a procedure such as

      public function whatever
      your code
      exit function

      Comment

      • kmarks247
        New Member
        • Jun 2017
        • 8

        #4
        Just checked the Object Library and everything seems fine there. I put the code into a Private Function On Click() with exit function at the end and still getting same error. Going to keep plugging away at it. Thanks for your suggestions!

        Comment

        • baderms
          New Member
          • Jun 2017
          • 7

          #5
          Put this in the click event of the button
          Code:
              Call GetFile
          put this into a module
          Code:
          Public Function GetFile() As Boolean
              On Error GoTo Proc_Err
              
              Dim strPath             As String
              Dim strFile             As String
              Dim strTable            As String
              Dim strSpecification    As String
              Dim intImportType       As AcTextTransferType
              Dim blnHasFieldNames    As Boolean
              Dim objFileDialog       As Office.FileDialog
              
              Set objFileDialog = Application.FileDialog(MsoFileDialogType.msoFileDialogFolderPicker)
              
              '**** Modify these values as needed ****
              strTable = "Daily Aeppays Consolidated"
              strSpecification = "DailyAeppayImport"
              blnHasFieldNames = True
              intImportType = acImportDelim
              '***************************************
          
              'Let user select a folder
              With objFileDialog
                  .AllowMultiSelect = False 'Meaningless, but set anyway
                  .ButtonName = "Folder Picker"
                  .Title = "Folder Picker"
                  If .Show Then
                      strPath = .SelectedItems(1) & "\"
                      'Loop through the text files
                      strFile = Dir(strPath & "*.txt")
                      Do While strFile <> ""
                      'For Testing purposes only
                      DoCmd.TransferText intImportType, , strTable, strPath & strFile, False
                      strFile = Dir
                      Loop
                  End If
              End With
              
          Proc_Exit:
              Exit Function
              
          Proc_Err:
              MsgBox "Error Nbr. " & Err.Number & vbCrLf & _
                     "Line Nbr. " & Erl & vbCrLf & vbCrLf & _
                     Err.Description, vbExclamation, "Mod: Common.GetFile"
              Resume Proc_Exit
              
          End Function

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32645

            #6
            Can I assume you've already looked at Select a File or Folder using the FileDialog Object?

            PS. Welcome to the club Baderms. Alweays good to see new members answering questions :-)

            Comment

            Working...