MS-Access-2016打开文件

时间:2018-03-01 15:26:10

标签: access-vba ms-access-2007 ms-access-2016

将Access数据库从2007 32位升级到Access 2016 64位。 在那里有一个按钮,用于导入数据的csv文件。 在旧版本中,它会让您选择该文件,但这在2016年不起作用。这是此操作的vba代码,适用于Access 2007 32位版本。

Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Declare PtrSafe Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean

Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

Global Const ahtOFN_READONLY = &H1
Global Const ahtOFN_OVERWRITEPROMPT = &H2
Global Const ahtOFN_HIDEREADONLY = &H4
Global Const ahtOFN_NOCHANGEDIR = &H8
Global Const ahtOFN_SHOWHELP = &H10
' You won't use these.
'Global Const ahtOFN_ENABLEHOOK = &H20
'Global Const ahtOFN_ENABLETEMPLATE = &H40
'Global Const ahtOFN_ENABLETEMPLATEHANDLE = &H80
Global Const ahtOFN_NOVALIDATE = &H100
Global Const ahtOFN_ALLOWMULTISELECT = &H200
Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
Global Const ahtOFN_PATHMUSTEXIST = &H800
Global Const ahtOFN_FILEMUSTEXIST = &H1000
Global Const ahtOFN_CREATEPROMPT = &H2000
Global Const ahtOFN_SHAREAWARE = &H4000
Global Const ahtOFN_NOREADONLYRETURN = &H8000
Global Const ahtOFN_NOTESTFILECREATE = &H10000
Global Const ahtOFN_NONETWORKBUTTON = &H20000
Global Const ahtOFN_NOLONGNAMES = &H40000
' New for Windows 95
Global Const ahtOFN_EXPLORER = &H80000
Global Const ahtOFN_NODEREFERENCELINKS = &H100000
Global Const ahtOFN_LONGNAMES = &H200000


Private Sub cmdImportStudents_Click()

  Dim strDirFile As String
  strDirFile = GetOpenFile(CurrentDb().Name, "Select Student Import File")

  If strDirFile > "" Then
    Dim strA1 As String
    '---
    Open strDirFile For Input As #1
    '---
    If EOF(1) Then
      Close #1
      GoTo ErrorImportStudents
    Else
      '---
      Line Input #1, strA1
      Close #1
      '--- Delete destination table ---
      On Error Resume Next
      DoCmd.DeleteObject acTable, "STUD_REC"
      '--- Import data ---
      On Error GoTo ErrorImportStudents
      DoCmd.TransferText acImportDelim, "Students (comma)", "STUD_REC", strDirFile
    End If
  End If
  MsgBox "Import Complete!", , "Student Import"
ExitImportStudents:
  Exit Function

ErrorImportStudents:
  MsgBox "Error(s) occured during 'Student' import!"
  Resume ExitImportStudents

End Function

Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click

  DoCmd.Close

Exit_cmdExit_Click:
  Exit Sub

Err_cmdExit_Click:
  MsgBox Err.Description
  Resume Exit_cmdExit_Click

End Sub

0 个答案:

没有答案