Jak wyświetlić okno dialogowe "Otwórz plik" w Access 2007 VBA?
Jak mam pokazać okno dialogowe Otwórz plik (lub wybierz plik) w access 2007 VBA?
Próbowałem użyć aplikacji.GetOpenFileName jak w Excelu, ale ta funkcja nie istnieje w Access.
5 answers
Moje komentarze do odpowiedzi Renauda Bompuisa
W rzeczywistości można użyć late binding, a odwołanie do biblioteki obiektów 11.0 nie jest wymagane.
Poniższy kod będzie działał bez żadnych odniesień:
Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.Show
MsgBox "file choosen = " & f.SelectedItems.Count
Zauważ, że powyższe działa dobrze również w runtime.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-11 16:28:41
W Access 2007 wystarczy użyć Application.FileDialog
.
Oto przykład z Dokumentacji dostępu:
' Requires reference to Microsoft Office 12.0 Object Library. '
Private Sub cmdFileDialog_Click()
Dim fDialog As Office.FileDialog
Dim varFile As Variant
' Clear listbox contents. '
Me.FileList.RowSource = ""
' Set up the File Dialog. '
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Allow user to make multiple selections in dialog box '
.AllowMultiSelect = True
' Set the title of the dialog box. '
.Title = "Please select one or more files"
' Clear out the current filters, and add our own.'
.Filters.Clear
.Filters.Add "Access Databases", "*.MDB"
.Filters.Add "Access Projects", "*.ADP"
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the '
' user picked at least one file. If the .Show method returns '
' False, the user clicked Cancel. '
If .Show = True Then
'Loop through each file selected and add it to our list box. '
For Each varFile In .SelectedItems
Me.FileList.AddItem varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
Jak pokazuje przykład, upewnij się, że masz odniesienie do Biblioteki obiektów Microsoft Access 12.0 (w menu VBE IDE > narzędzia > Referencje).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-08 03:19:55
Dodatek do tego, co Albert już powiedział:
Ten kod (mashup różnych próbek) zapewnia możliwość posiadania okna dialogowego SaveAs
Function getFileName() As String
Dim fDialog As Object
Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
Dim varFile As Variant
With fDialog
.AllowMultiSelect = False
.Title = "Select File Location to Export XLSx :"
.InitialFileName = "jeffatwood.xlsx"
If .Show = True Then
For Each varFile In .SelectedItems
getFileName = varFile
Next
End If
End With
End Function
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-07 01:28:41
Mam podobne rozwiązanie do powyższego i działa na otwieranie, zapisywanie, wybieranie plików. Wklejam go do własnego modułu i używam we wszystkich DBACH dostępu, które tworzę. Jak stwierdza kod, wymaga biblioteki obiektowej Microsoft Office 14.0. Chyba jeszcze jedna opcja:
Public Function Select_File(InitPath, ActionType, FileType)
' Requires reference to Microsoft Office 14.0 Object Library.
Dim fDialog As Office.FileDialog
Dim varFile As Variant
If ActionType = "FilePicker" Then
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
' Set up the File Dialog.
End If
If ActionType = "SaveAs" Then
Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
End If
If ActionType = "Open" Then
Set fDialog = Application.FileDialog(msoFileDialogOpen)
End If
With fDialog
.AllowMultiSelect = False
' Disallow user to make multiple selections in dialog box
.Title = "Please specify the file to save/open..."
' Set the title of the dialog box.
If ActionType <> "SaveAs" Then
.Filters.Clear
' Clear out the current filters, and add our own.
.Filters.Add FileType, "*." & FileType
End If
.InitialFileName = InitPath
' Show the dialog box. If the .Show method returns True, the
' user picked a file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
'Loop through each file selected and add it to our list box.
For Each varFile In .SelectedItems
'return the subroutine value as the file path & name selected
Select_File = varFile
Next
End If
End With
End Function
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 21:58:37
Zgadzam się, że John M ma najlepszą odpowiedź na pytanie OP. Mysl, ze nie jest jasno powiedziane, pozornym celem jest uzyskanie wybranej nazwy pliku, podczas gdy inne odpowiedzi zwracaja albo liczniki, albo listy. Dodam jednak, że msofiledialogfilepicker może być lepszym rozwiązaniem w tym przypadku. ie:
Dim f As object
Set f = Application.FileDialog(msoFileDialogFilePicker)
dim varfile as variant
f.show
with f
.allowmultiselect = false
for each varfile in .selecteditems
msgbox varfile
next varfile
end with
Notatka: wartość varfile pozostanie taka sama, ponieważ multiselect jest false (tylko jeden element jest zaznaczony). Z równym powodzeniem wykorzystałem jego wartość poza pętlą. Chyba lepiej. praktyka, aby to zrobić, jak John M zrobił, jednak. Ponadto selektor folderów może być użyty do uzyskania wybranego folderu. Zawsze preferuję późne wiązanie, ale myślę, że obiekt jest natywny dla domyślnej biblioteki dostępu, więc może nie być tutaj konieczne
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-21 21:42:33