‘VBA Code to export an Access table to an Excel file with a date extension
'The paths and names should be changed for your project.
‘Just put the code behind a command button on a form.
Public Sub ExportFile()
DoCmd.SetWarnings False
Dim db As Database
Dim strFileName As String
Dim dFileDate As Date
Set db = CurrentDb()
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Dim rec As Recordset
Set db = CurrentDb
'***************************************************************
Msg = "Do you want to export? Do you wish to continue?"
Style = vbYesNo + vbWarning + vbDefaultButton2
Title = ""
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
MyString = "Yes" ' Perform some action.
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
'***************************************************************
outputFileName = "J:\MyFile_" & Format(Date, "YYYYMMdd") & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "MyReport", outputFileName, True
'***************************************************************
DoCmd.SetWarnings True
MsgBox "The report has been exported"
Exit Sub
ErrorHandler:
MsgBox "There was an Error: " & Err & ": " & Error(Err)
Else ' User chose No.
MyString = "No" ' Perform some action.
End If
End Sub
‘VBA Code to import all excel files from a certain directory and certain range to an Access table.
'The paths and names should be changed for your project.
‘Just put the code behind a command button on a form.
Public Sub Import()
Dim strFile As String
DoCmd.SetWarnings False
Dim db As Database
Dim strFileName As String
Dim dFileDate As Date
Set db = CurrentDb()
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Dim rec As Recordset
'*********************************************
Msg = ""
Style = vbYesNo + vbWarning + vbDefaultButton2
Title = ""
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
MyString = "Yes" ' Perform some action.
'*********************************************
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
DoCmd.SetWarnings False
strPath = "J:\your directory\"
' Tell it to import all Excel files from the file directory
strFile = Dir(strPath & "*.xlsx*")
' Start loop
'*********************************************
Do While strFile <> ""
' Import file
DoCmd.TransferSpreadsheet transfertype:=acImport, tablename:="YourAccessTableName", FileName:=strPath & strFile, HasFieldNames:=False, Range:="A2:AN800"
' Loop to next file in directory
strFile = Dir
Loop
'*********************************************
DoCmd.SetWarnings True
MsgBox "The file(s) have been imported."
Exit Sub
ErrorHandler:
MsgBox "There was an Error: " & Err & ": " & Error(Err)
Else ' User chose No.
MyString = "No" ' Perform some action.
End If
End Sub
‘VBA Code to run a macro.
‘Just put the code behind a command button on a form and change the command button name.
Private Sub Command52_Click()
On Error GoTo Error_Handler
'***************************************************************
DoCmd.RunMacro "MyStoredMacro"
'***************************************************************
Error_Handler_Exit:
On Error Resume Next
Exit Sub
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Command52_Click" & vbCrLf & _
"Error Description: " & Err.Description, vbCritical, _
"An Error has Occured!"
Resume Error_Handler_Exit
‘VBA Code to close all forms upon exiting
(https://www.microsoftaccessexpert.com/Microsoft-Access-Code.aspx)
Public Function CloseAllForms()
Dim lngLoop As Long
For lngLoop = (Forms.Count - 1) To 1 Step -1
DoCmd.Close acForm, Forms(lngLoop).Name
Next lngLoop
End Function
‘VBA Code to convert the name of a month to a number
(https://www.microsoftaccessexpert.com/Microsoft-Access-Code.aspx)
Public Function ChangeToMonth(sMonth As String) As Integer
Select Case sMonth
Case "Jan"
ChangeToMonth = 1
Case "Feb"
ChangeToMonth = 2
Case "Mar"
ChangeToMonth = 3
Case "Apr"
ChangeToMonth = 4
Case "May"
ChangeToMonth = 5
Case "Jun"
ChangeToMonth = 6
Case "Jul"
ChangeToMonth = 7
Case "Aug"
ChangeToMonth = 8
Case "Sep"
ChangeToMonth = 9
Case "Oct"
ChangeToMonth = 10
Case "Nov"
ChangeToMonth = 11
Case "Dec"
ChangeToMonth = 12
Case Else
ChangeToMonth = 0
End Select
End Function