Created
January 4, 2012 19:08
-
-
Save airstrike/1561504 to your computer and use it in GitHub Desktop.
Basic MS Access file picker & Import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Option Compare Database | |
| Public Sub DoImport(fileName As String) | |
| DoCmd.TransferText acImportDelim, "ImportaTrafegoPre", "Import", fileName, True | |
| End Sub | |
| Function getFileName(Optional save As Boolean = False) As String | |
| Dim fDialog As Object | |
| Dim title As String | |
| Dim initial As String | |
| If save Then | |
| Set fDialog = Application.FileDialog(msoFileDialogSaveAs) | |
| title = "Selecione local para salvar arquivo" | |
| initial = "Trafego Pre.xlsx" | |
| Else | |
| Set fDialog = Application.FileDialog(msoFileDialogOpen) | |
| title = "Selecione arquivo .txt para importar" | |
| initial = "" | |
| End If | |
| Dim varFile As Variant | |
| With fDialog | |
| .AllowMultiSelect = False | |
| .title = title | |
| .InitialFileName = initial | |
| If .Show = True Then | |
| For Each varFile In .SelectedItems | |
| getFileName = varFile | |
| Next | |
| End If | |
| End With | |
| End Function | |
| Private Sub btnImport_Click() | |
| 'Importa um arquivo para a base 'Import' | |
| Dim opt As Variant | |
| If Me.txtFindFile.Value <> "" Then | |
| opt = MsgBox("Deseja apagar os dados já existentes " & _ | |
| "na tabela antes de importar o arquivo?", _ | |
| vbYesNoCancel) | |
| If opt = 6 Then | |
| SQL = "DELETE * FROM [TrafegoPre];" | |
| CurrentDb.Execute SQL | |
| MsgBox ("Dados antigos apagados com sucesso." & vbCrLf & vbCrLf & _ | |
| "Prosseguindo com a importação...") | |
| ElseIf opt = 2 Then | |
| MsgBox ("Operação cancelada pelo usuário.") | |
| Exit Sub | |
| End If | |
| 'Dim fname as String | |
| 'fname = getFileName | |
| DoImport (getFileName) | |
| MsgBox ("Dados importados do arquivo com sucesso.") | |
| Else | |
| MsgBox ("Por favor, selecione um arquivo primeiro.") | |
| End If | |
| End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment