Skip Navigation LinksHome Page > Free Downloads > Free Code Samples

Free Code Samples

To view the code in your browser click on the View Code link.
To download the code click on the Download file link.

  View/DownloadDescription of Code         

  View Code:
    AllCodeToTextFile.htm
  Download file:
    AllCodeToTextFile.zip

This code Module saves all Database Code in a text file. New: The SaveAllCodeToFile Function now saves all code in alphabetical order of the Module names. Modules are grouped first by Forms, then Reports, followed by Standard and Class modules grouped together. This order makes it easier to compare Databases and find Modules and Procedures. Form modules are prefixed with "Form_" and Report modules with "Report_". All Modules are included except Modules without code. Blank lines and Comment lines in Modules with code are included in file and in totals. The SaveAllCodeToFile Function Returns the Grand total number of lines in modules, File name, and Folder path. The Modules have headers added and the code is in the exact same format as that which is in the Database.

Other Procedures included are:
The GetModuleCode Function which returns the code and Name of the specified Module.
The CodeLinesCountSave Function which Saves the total lines of code in a Database in a Text File. Blank lines and Comment only lines are not counted.
The SaveFoundCodeInTextFile Function which saves your specified searched Code in a Text File. Module Names, Line numbers and the line of code is save in the file.
The AllCodeToTextFile Sub saves all code in the Database in a Text file but not in alphabetical order.
The SearchSQLInQueries Function Returns Query Names and SQL of Queries containing specified search string.


  View Code:
    AppointmentToOutlook.htm
  Download file:
    AppointmentToOutlook.zip

Code for the Article: From Access to Outlook--Adding Your Custom Access Data to Your Outlook Calendar

btnAddApptToOutlook_Click is used on a form to add the Form data to an Outlook Calendar. It uses late binding. You can use or make your own form and easily modify the code to send almost any information with a date to the Outlook Calendar. Good for project deadlines, meetings, scheduled maintenance, etc.

Includes isAppThere Function by Rick Dobson which returns a boolean value depending upon whether the specified Application is open.


  View Code:
    ApptDialogForDB.htm
  Download file:
    ApptDialogForDB.zip

This is the code used in the Article "Finishing a Access And Outlook Data Management Tool that is Both Powerful and Flexible" It demonstrates how to loop through a Multiselect ListBox to get selected values. It also adds those selected values to the Outlook Calendar as Outlook Appointments. Also demonstrated is the use of the Execute Method with the dbFailOnError Constant.


  View Code:
    CodeLinesCountSave.htm
  Download file:
    CodeLinesCountSave.zip

This procedure returns the total lines of code in the database and saves a text file in the folder containing the database with details including the names of the modules and the number of lines of code each module contains. It can be helpful in determining how much code was written for a client. You can do this by after adding the code to the database, running the procedure before you start development. Then when you are finished or at appropriate times along the way the procedure can be run and the numbers subtracted to get the total number of lines of code added.

This is not the best or the only way to measure the amount of work done on a project but it can be useful.


  View Code:
    ConnectionStringSQLAzure.htm
  Download file:
    ConnectionStringSQLAzure.zip

This code contains the obfuscatedFunctionName Procedure which returns the Connection String to a SQL Azure Database and is used in the How to Create SQL Azure Database Users With Microsoft Access VBA article. You can also use this procedure for linked tables and pass-through queries.

It is named obfuscatedFunctionName to remind you to change the name of the procedure to mislead hackers. You can use examples like PartColorNotes, strSpindleName, GetProductSize for the name of the Procedures. You can use your imagination to create more Procedure Names as you please.

The Argument strIn is what I call a "Password" Argument. It makes it extremely difficult for a hacker to run your Procedure.

Example use with a Pass-through query: qdf.Connect = obfuscatedFunctionName.

Remember to enter your Connection String info in the My_________ places in the Procedure.


  View Code:
    ConnMasterDBSQLAzure.htm
  Download file:
    ConnMasterDBSQLAzure.zip

This code can be used to create a connection to a SQL Azure master Database. We recommended you use it in place of the Public Function obfuscatedFunctionName in the article How to Use Microsoft Access to Create Logins in a SQL Azure Database.

You will need to fill in your information in the code. We recommend you rename the procedure to obfuscate it. Then you need to replace "obfuscatedFunctionName" in the ExecuteMasterDBSQL Function with your new procedure name.


  View Code:
    CreateSQLAzureDBUsers.htm
  Download file:
    CreateSQLAzureDBUsers.zip

Code to Create SQL Azure Database Users with pass-through queries in VBA. From the article How to Create SQL Azure Database Users With Microsoft Access VBA.

The ConnectionStringSQLAzure code contains the obfuscatedFunctionName Function in the article that enables you to connect to a SQL Azure Database.

SQL Azure Database users can be used in code by Access Databases to connect to SQL Azure Tables, Views, and Stored Procedures.


  View Code:
    DateDialogForm.htm
  Download file:
    DateDialogForm.zip

Code from the Article "Make Selecting Your Dates Easy With A Custom Dialog Form". It includes building a SQL Statement from Form Controls and how to use a Multiselect ListBox to get selected items.


  View Code:
    DoesFileExist.htm
  Download file:
    DoesFileExist.zip

Purpose: Determine if a specific File exists of any file type.

Arguments: strFileNamePath is the full path and file name

Example: DoesFileExist("C:\MyFiles\MyFileName.txt")

Example In a Procedure: If DoesFileExist(strfname) Then . . .


  View Code:
    EmailHTMLReports.htm
  Download file:
    EmailHTMLReports.zip

Normally Reports with more than one page cannot be emailed with the Report as HTML in the body of the Report. Access creates a HTML file for each page. Oliver Stohr developed a method of using code to read the HTML files and concatenate all of the HTML in the files into one string that is put in the body of the Email. You may have to tweak your Report to display the way you want. In my Report, to maintain spacing I added a field with all color properties set to white so it would not be seen but would maintain the spacing I wanted.

I have also included some other Procedures that may be useful.


  View Code:
    EmailSend.htm
  Download file:
    EmailSend.zip

Two different ways to send Emails from Access by the noted Access MVP Arvin Meyer.


  View Code:
    Environ.htm
  Download file:
    Environ.zip

Leverage the information avaiable when using the Environ Function with this VBA Code Module. Get the current user, Computer Name, User Domain, Logon Server name, Program Files folder, number of Processors, CPU Architecture, a number of important folder paths, and more. Included are two procedures that list all the Environ Variables and their values.

This code module is from the free WMISample database which you can download. You can also download or copy and paste the Code in this Module as a Text File.


  View Code:
    ErrorsODBCandVBA.htm
  Download file:
    ErrorsODBCandVBA.zip

This error handling code can be used to handle all VBA and ODBC errors, providing much more precise error details, and it can reduce the lines of code you use to handle errors throughout a database. It can be used to show the line number on which the error occurred. The code uses the DBEngine Errors collection to get precise information about errors dealing with ODBC connection issues and failures with code that accesses databases such as SQL Server. This information can save a great deal of time discovering the cause of ODBC errors. The code also handles VBA Errors and can be used in throughout a database. The code includes an option to save error information in a table which can be very helpful when trying to solve issues in a database distributed to users. Instructions for use are included within the code. An example of how you use this code in your procedures is shown below:

ErrHandle:
    MsgBox GetErrors("MyProcedure of Module MyModule")
    Resume ExitHere


  View Code:
    ExecuteMasterDBSQL.htm
  Download file:
    ExecuteMasterDBSQL.zip

This code executes SQL in a SQL Azure master Database using a pass-through query in VBA. From the article How to Use Microsoft Access to Create Logins in a SQL Azure Database. It also includes the cmdCreateLogin_Click Sub.

You will need to download the ConnMasterDBSQLAzure.zip code file and fill in your information in the code. We recommend you rename the procedure to obfuscate it. Then you will need to replace "obfuscatedFunctionName" in the ExecuteMasterDBSQL Function with your new procedure name.


  View Code:
    ExportAppointmentByDates.htm
  Download file:
    ExportAppointmentByDates.zip

Code used with a Dialog Form to Export Appointments to the Outlook Calendar by date.

Procedure Name: Private Sub btnExportApptByDates_Click(). This code is used in the article titled: Get the Dates You Want With Your Own Microsoft Access Dialog Form.


  View Code:
    FolderDialog.htm
  Download file:
    FolderDialog.zip

Uses the Microsoft Office FileDialog to get a Folder Path String. Requires a reference to Microsoft Office Object Library. Contains a sub to set that reference with code.


  View Code:
    GetFieldDataType.htm
  Download file:
    GetFieldDataType.zip

This Code contains two Functions. GetFieldDataTypeName and GetFieldDataTypeNo.

GetFieldDataTypeName helps when you get DAO Field Type Property values which are returned as Constant Value numbers. You have to look them up in the Object Browser to find out what they are. Using GetFieldDataTypeName you can use the Constant value to get the Constant which is easier to understand..
Example: Debug.Print GetFieldDataTypeName(fld.Properties("Type").Value)) will return one of the Constants like dbText, dbDate, dbLong, etc.

Use GetFieldDataTypeNo to verify if a Field is the right DataType:
If fld.Type = GetFieldDataTypeNo("dbLong") Then
    ' Your code here
End If

If you find some other uses for this code please send me an email and let me know.


  View Code:
    GetFieldDataTypeName.htm
  Download file:
    GetFieldDataTypeName.zip

Converts an Access DataType Numerical Constant to a DataType Name you can recognize.


  View Code:
    GetFileNamePath.htm
  Download file:
    GetFileNamePath.zip

Open Windows File Dialog to add Filename and Path to a textbox.


  View Code:
    GetPWDCharacters.htm
  Download file:
    GetPWDCharacters.zip

Use the GetPWDCharacters Function to get random characters which can be used as Procedure "Password" Arguments. The characters are also safe to use for ODBC Passwords for SQL Azure or SQL Server

Arguments: lngCount is the number of characters you want returned.

Example: strPassword = GetPWDCharacters(15) returns a string of 15 characters.


  View Code:
    OpenApps.htm
  Download file:
    OpenApps.zip

Open common Windows applications using the Shell Function.

The Functions look for the latest version to open it. An error message notifies us if the application is not found. Opens the following and more:

  • Excel
  • Outlook
  • Word
  • MSPaint
  • Infopath
  • Publisher
  • PowerPoint
  • Calculator
  • Notepad
  • Windows Explorer
  • OneNote
  • Internet Explorer


  View Code:
    SafeODBCTablesQueries.htm
  Download file:
    SafeODBCTablesQueries.zip

The Purpose of this code is to make your Linked ODBC Tables, Views, and Pass-through Queries safe where they do not reveal the UID and PWD of the SQL Server, or SQL Azure Database. The User Name and Password are cached in the database using code and are removed from the Connection Properties of Linked Tables, Views, and Pass-Through queries.

This code is based on an article by Ben Clothier, Access MVP entitled Power Tip: Improve the security of database connections. Reading this article will help you understand how this security measure works.

After unzipping and importing the modSafeODBCTablesQueries.bas file into your database before this code will work you must first make all of your Tables, Views, and Pass-through Queries DSN-Less. See Doug Steele's Using DSN-Less Connections article for instructions on how to do this.

Instructions are within the code that will guide you so you can safely use Linked Tables, Views, and Pass-through queries without the User Name or Password being revealed.


  View Code:
    SaveAsText.htm
  Download file:
    SaveAsText.zip

Code to backup and restore Access Database Objects such as Forms, Reports, Queries, Tables, Modules, and Macros. Uses the undocumented SaveAsText and LoadAsText Methods. This is very helpful when used to eliminate corruption in a database.


  View Code:
    SaveSnapshot.htm
  Download file:
    SaveSnapshot.zip

Saves all Reports Open in Preview as Snapshot Files.

The File Path must exist for the Snapshot to be saved. If the file already exists, it will be written over by the new file.

*Requires DoesFileExist Function which you can download from this page.


  View Code:
    SetDSNLessTables.htm
  Download file:
    SetDSNLessTables.zip

Change DSN linked tables to DSN-Less linked tables to SQL Azure or SQL Server.

  View Code:
    SQLAzureRolesAndUsers.htm
  Download file:
    SQLAzureRolesAndUsers.zip

Code to Create SQL Azure Database Roles and add Users to the Roles. Some of the code is for use in a Form. From the article Using Microsoft Access to Manage SQL Azure Database Users and Roles.

The SQLAzureRolesAndUsers code file contains the ExecSQLAzureSQL Function which enables you to use Microsoft Access to run T-SQL Action Queries SQL in s SQL Azure database.


  View Code:
    StringConcatenations.htm
  Download file:
    StringConcatenations.zip

Combine names and addresses strings together for use in Forms, Reports, and Queries.


  View Code:
    TablesDocument.htm
  Download file:
    TablesDocument.zip

Count and list all Tables in a database.
Count and list all table names and each table's recordcount.
Count and list all table names and each table's recordcount in a remote database.

Write documentation for all tables to a text file.


  View Code:
    TextFiles.htm
  Download file:
    TextFiles.zip

Various ways to write text files and also document Command Bars.


  View Code:
    WMI_PAWWMIRegistry.htm
  Download file:
    WMI_PAWWMIRegistry.zip

VBA Code Modules that use WMI(Windows Management Instrumentation) to manage the Windows Registry. You can read, create, modify, and delete Registry Keys and Values. These code modules are from the free WMISample database which you can download.


  View Code:
    WMIProcessorID.htm
  Download file:
    WMIProcessorID.zip

This Function uses WMI to get the local Computer Processor ID. It is featured in the article "Using the Power of VBScripts and WMI in Microsoft Access VBA".

 

I have tried to be sure not to break anyone's copyrights. However, if you think some code posted here is infringing on anyone's copyright, please let us know so we can correct it.

Return to Top