Reputation: 1108
I have a database originally built in Ms Access 2010. Part of the VBA code allows users to extract data and save to their machine. This employs Ken Getz's code for earlier versions and Application.FileDialog()
for later versions.
One user is running 64 bit Windows 10 with Access 2013 (32 bit) installed.
When trying to run the code on this machine Access crashes without any error messages and restarts.
There are checks in the code for 64 or 32 bit versions and for the version of VB (6 or 7).
Given the lack of error messages I'm not sure how to troubleshoot or fix this.
Here's the sub which calls Ken Getz's code depending on version:
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
Upvotes: 1
Views: 2008
Reputation: 107687
Consider using MS Access' FileDialog property but specify folder picker as dialog type. As far as I know, this should be compliant on any PC (32/64-bit version or Office 2003-2016 version). The somewhat dated link you use concerns the Open/Save Dialog box and not the File/Folder browser.
Once you obtain the folder name, simply concatenate with your Excel file's base name, conditional on export type: surveyName, allData, or effort.
Function exportData_Click()
' Declare Variables
Dim strSaveFileName As String
Dim The_Year As Variant
Dim ctlCurrentControl As Control
Dim surveyName As String, allData As String, effort As String
Dim fileYear As String
Dim fd As Object
Const msoFileDialogFolderPicker = 4
Dim strFolderPath
' 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)
' Folder Pick Dialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Browse for folder to export queries"
.AllowMultiSelect = False
.Filters.Clear
If .Show = -1 Then
strFolderPath = .SelectedItems(1)
Else
'The user pressed Cancel.
MsgBox "No folder Selected", vbExclamation
strFolderPath = Null
Set fd = Nothing
Exit Function
End If
End With
Set fd = Nothing
' Export functions for different survey cases
If surveyName Like "*O*_" Then
strSaveFileName = strFolderPath & "\" & allData & ".xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, allData, strSaveFileName
ElseIf surveyName Like "*DA_" Then
strSaveFileName = strFolderPath & "\" & surveyName & ".xlsx"
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
strSaveFileName = strFolderPath & "\" & allData & ".xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, allData, strSaveFileName
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, effort, strSaveFileName
End If
End Function
Upvotes: 2
Reputation: 5386
Couple of checks.
Check References - are any marked as MISSING?
You might have to reference the Microsoft Office Object library that exists on user computer - is it 64 bit Office with 32 bit MS-Access?
Obvious question - any compile errors?
EDIT - Update suggestions
You're calling aht_apiGetOpenFileName
in your question that requires the tagOpenFilename
structure, but in your code you're showing that you pass multiple parameters
Upvotes: 1