Access-o-Mania

Access-Forum (Deutsch/German) => Access Programmierung => Thema gestartet von: Thomas_Klee am August 17, 2016, 21:24:15

Titel: Explorer für Dateiimport csv nutzen
Beitrag von: Thomas_Klee am August 17, 2016, 21:24:15
Hallo zusammen,

bestimmt könnt ihr mir wiedermal helfen. Ich möchte gerne eine Menge csv Dateien importieren, die ich "einzeln" per
Explorer auswählen/doppelklicken möchte.



'------------------------------------------------------------
' IMPORT_W_LISTE_ALTBESTAND
'
'------------------------------------------------------------
Function IMPORT_W_LISTE_ALTBESTAND()
On Error GoTo IMPORT_W_LISTE_ALTBESTAND_Err

    Beep
    MsgBox "Import der W-LISTE", vbOKOnly, ""
    DoCmd.DeleteObject acTable, "W-IMPORT-NEU"
'--------------------------
' wie bekomme ich den Explorer auf um dann in dem nächsten Schritt ' TransferText '
' nicht immer den gleichen Dateinamen zu haben, sondern den den ich
' im Explorer ausgewählt habe
' wenn es möglich ist sollte der Explorer auch schon in das
' Verzeichnis ' "P:\Filetransfer\BZV\Statistiken\W-LIST-ALT\
' springen und mir nur die
' *.csv Dateien anbieten
'-----------------------------
    DoCmd.TransferText acImportDelim, "W-Liste_Test Importspezifikation", "W-IMPORT-NEU", _
    "P:\Filetransfer\BZV\Statistiken\W-LIST-ALT\W-Liste_Test.csv", False, ""
    DoCmd.OpenQuery "Import: W-LISTE GRUENDE 01", acViewNormal, acEdit


IMPORT_W_LISTE_ALTBESTAND_Exit:
    Exit Function

IMPORT_W_LISTE_ALTBESTAND_Err:
    MsgBox Error$
    Resume IMPORT_W_LISTE_ALTBESTAND_Exit

End Function
Titel: Re: Explorer für Dateiimport csv nutzen
Beitrag von: Beaker s.a. am August 18, 2016, 00:58:47
Hallo Thomas,
Suche mal in die OH mit dem Stichwort "Application.FileDialog"

hth
gruss ekkehard
Titel: Re: Explorer für Dateiimport csv nutzen
Beitrag von: Thomas_Klee am August 19, 2016, 20:04:39
Ein Versuch war es wert, aber ich bekomme beim ausführen die Fehlermeldung:

Benutzerdefinierter Typ nicht definiert  >:(

Option Compare Database

Option Explicit

Private Sub cmdShow_Click()
On Error GoTo SubError
    ' Add "Microsoft Office 14.0 Object Library" in references
    Dim fDialog As Office.FileDialog
    Dim varFile As Variant
   
    txtSelectedName = ""
   
    ' Set up the File Dialog
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
   
    With fDialog
        .Title = "W-LIST CSV Auswählen"
        .AllowMultiSelect = False
        .InitialFileName = "C:\"  'Folder picker needs trailing slash
       
        .Filters.Clear
        .Filters.Add "csv", "*.csv*"
'        .Filters.Add "Excel files", "*.xls"
'        .Filters.Add "Excel files", "*.xlsx"
'        .Filters.Add "Excel macro-enabled", "*.xlsm"

        If .Show = True Then
            If .SelectedItems.Count = 0 Then
                'User clicked open but didn't select a file
                GoTo SubExit
            End If
           
            'An option for MultiSelect = False
            'varFile = .SelectedItems(1)
            'txtSelectedName = varFile
           
            'Needed when MultiSelect = True
            For Each varFile In .SelectedItems
                txtSelectedName = txtSelectedName & varFile & vbCrLf
            Next
        Else
            'user cancelled dialog without choosing!
            'Do you need to react?
        End If
   
    End With
   
SubExit:
On Error Resume Next
    Set fDialog = Nothing
    Exit Sub
   
SubError:
    MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical + vbOKOnly, _
        "An error occurred"
    GoTo SubExit
       
End Sub

Private Sub Form_Load()

End Sub

Private Sub lblFile_BeforeUpdate(Cancel As Integer)

End Sub


Was bedeutet denn das nun wieder
Titel: Re: Explorer für Dateiimport csv nutzen
Beitrag von: Lachtaube am August 19, 2016, 21:06:16
Hallo,

vermutlich fehlt der erforderliche Verweis auf die Office-Bibliothek. Es geht aber auch ohne diesen.Option Compare Database
Option Explicit

Private MyPickedCsvFiles As VBA.Collection

Sub Foo()
   ' Add "Microsoft Office 14.0 Object Library" in references
   ' hier mit Late-Binding (Verweis nicht notwendig)
   Dim fDialog As Object, varFile

   On Error GoTo SubError

   ' Set up the File Dialog
   Set fDialog = FileDialog(3)   'msoFileDialogFilePicker

   With fDialog
      .Title = "W-LIST CSV Auswählen"
      'Mehrfachauswahl mit edrückt gehaltener Umschalt- oder Strg-Taste
      'ermöglichen
      .AllowMultiSelect = True
      .InitialFileName = "C:\Temp"  'Folder picker needs trailing slash

      .Filters.Clear
      .Filters.Add "csv", "*.csv"
      '        .Filters.Add "Excel files", "*.xls"
      '        .Filters.Add "Excel files", "*.xlsx"
      '        .Filters.Add "Excel macro-enabled", "*.xlsm"

      If .Show = True Then
         If .SelectedItems.Count = 0 Then
            'User clicked open but didn't select a file
            GoTo SubExit
         End If

         'An option for MultiSelect = False
         'varFile = .SelectedItems(1)
         'txtSelectedName = varFile

         'Needed when MultiSelect = True
         Set MyPickedCsvFiles = New VBA.Collection
         For Each varFile In .SelectedItems
            'Ergebnis in Collection ablegen
            MyPickedCsvFiles.Add varFile
         Next
      Else
         'user cancelled dialog without choosing!
         'Do you need to react?
      End If

   End With

SubExit:
   On Error Resume Next
   Set fDialog = Nothing
   Exit Sub

SubError:
   MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical + vbOKOnly, _
          "An error occurred"
   GoTo SubExit

End Sub

'... und dann irgendwo so etwas
Sub Bar()
   Dim CsvFile

   For Each CsvFile In MyPickedCsvFiles
      MsgBox CsvFile
      'Call ProcessMyCsvFile(FileItem)
   Next
End Sub