我有一个最初在Access 2010中构建的数据库。部分VBA代码允许用户提取数据并保存到他们的计算机上。对于早期版本,它使用Ken Getz's code,对于更高版本使用Application.FileDialog()
。
一位用户正在运行安装了Access 2013(32位)的64位Windows 10。
尝试在此计算机上运行代码时,Access崩溃时没有任何错误消息并重新启动。
代码中有64位或32位版本以及VB版本(6或7)。
鉴于缺少错误消息,我不确定如何排除或解决此问题。
这是根据版本调用Ken Getz代码的子代码:
Dim queryYear As Variant
'Function to export data to location of users choice. Exports TWO queries to same workbook.
'Survey name is automatically detected from the control button used
'(must be changed to BaMN_ for example) as previous export only used one query.
'Year is derived from the combobox value on [Extract Data] form, null value defaults to all years.
Function exportData_Click()
'Checks VBA version. This function will only work on 7+
#If VBA7 Then
'Code is running in the new VBA7 editor
'Declare Variables used by both 32 and 64 bit versions
Dim strSaveFileName As String 'both
Dim The_Year As Variant 'both
Dim ctlCurrentControl As Control 'both
Dim surveyName As String 'both
Dim allData As String 'both
Dim effort As String 'both
Dim fileYear As String 'both
'Get the name of the control button clicked (corresponds to query name to be run)
Set ctlCurrentControl = Screen.ActiveControl
surveyName = ctlCurrentControl.Name
allData = surveyName & "AllData"
effort = surveyName & "Effort_Export"
'Get combobox value and assign relavent values to The_Year
The_Year = Forms![Extract Data]!Extract_Year.value
'Change the year from a variant to what we need in the SQL
If The_Year Like "20*" Then
The_Year = CInt(The_Year)
fileYear = The_Year
Else:
The_Year = "*"
fileYear = "All"
End If
'Set queryYear variable
setYear (The_Year)
'If block to deal with both 32 and 64 bit versions.
#If Win64 Then
'Code is running in 64-bit version of Microsoft Office
MsgBox ("Running 64 bit version")
'Declare 64 bit only variables
Dim f As FileDialog
'Open the Save as Dialog to choose location of query save
Set f = Application.FileDialog(msoFileDialogSaveAs)
f.AllowMultiSelect = False
f.ButtonName = "Save"
f.Title = "Save As"
strSaveFileName = surveyName & fileYear & "_output.xlsx"
f.InitialFileName = strSaveFileName
f.Show
'End of 64 bit code
#Else
'Code is running in 32-bit version of Microsoft Office
MsgBox ("Running 32 bit version")
'Declare
Dim strFilter As String '32
'Open the Save as Dialog to choose location of query save for 32 bit
strFilter = ahtAddFilterItem("Excel Files (*.xlsx)", "*.xlsx")
strSaveFileName = ahtCommonFileOpenSave( _
openFile:=False, _
Filter:=strFilter, _
Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY)
#End If
'Export functions for different survey cases
If surveyName Like "*O*_" Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, allData, strSaveFileName
ElseIf surveyName Like "*DA_" Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, surveyName & "Occ_export", strSaveFileName
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, surveyName & "Trees_export", strSaveFileName
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, surveyName & "RepTree_export", strSaveFileName
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, surveyName & "Habitat_export", strSaveFileName
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, surveyName & "TPole_export", strSaveFileName
Else
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, allData, strSaveFileName
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, effort, strSaveFileName
End If
#Else
'Code is running in VBA version 6 or earlier
MsgBox ("Only available on MS Access 2007 and above")
#End If
End Function
'Function to set queryYear used in data extraction queries
Public Function setYear(The_Year As Variant)
queryYear = The_Year
End Function
'Function to get queryYear used in data extraction queries
Function getYear()
getYear = queryYear
End Function
答案 0 :(得分:2)
考虑使用MS Access' FileDialog属性,但指定文件夹选择器为dialog type。据我所知,这应该适用于任何PC(32/64位版本或Office 2003-2016版本)。您使用的有点过时的链接涉及打开/保存对话框,而不是文件/文件夹浏览器。
获取文件夹名称后,只需连接Excel文件的基本名称,条件为导出类型: surveyName , allData 或努力
querySelector is null
答案 1 :(得分:1)
几次检查。
检查参考 - 是否标记为MISSING?
您可能必须引用用户计算机上存在的Microsoft Office对象库 - 它是64位Office,具有32位MS-Access吗?
明显的问题 - 任何编译错误?
编辑 - 更新建议
您在问题中呼叫aht_apiGetOpenFileName
需要tagOpenFilename
结构,但在您的代码中,您显示您传递了多个参数