Translate this page
Provided By Microsoft® Translator
Ms Access Guru

VBA Code

Here is VBA code you can use in your projects. A lot of this code works no matter where you are in Microsoft Office ... Excel, Word, PowerPoint, Project, Visio, ... and Access!

(Latest) Clear List Close open Modules Create field Create index Create table Drop down Delete table Drop table Filter a Form (or subform) Find Record First Record Get Computer Name Get Long Integer from String Get Nth Word Get Age Get Distance Highlight Current Record Import Excel Worksheets Last Record Loop and Trim Text Modules close Next Record Previous Record Print Multiple Copies ProperCase, lowercase, UPPERCASE Read Text File Loop through Files and Rename with Date and Time Save String as a File Set Application Title Sequential numbers Sleep

If you have visited before and want to refresh to be sure you see the latest, press Ctrl-F5 on a PC.

VBA

API     

Goto Top  

Get Computer Name

Get the computer name using a Windows API (application programming interface) function. String s4p

Sleep

Pause executing VBA to let something else happen, like wait for a form to close, or something external like Transfer Spreadsheet from Excel, or copy and move files. Better to Sleep than to loop DoEvents. s4p

Application     

Goto Top  

Set Application Title

Set the application title to a specified string. Easily show the path and filename of the database when it is opened using the AutoExec macro to run a function. Boolean s4p

Control     

Goto Top  

Clear List

Clear the selection of a listbox control s4p

Drop down

Drop the list of a combo box control. s4p

DAO     

Goto Top  

Create Table and Make records

Create a table if it doesn't already exist, define a field, make a PrimaryKey index, and put records in. This example builds a table for storing sequential numbers that can be used for printing multiple copies of a report, getting records for every day even if there is no data, finding gaps, ... useful for all kinds of things! s4p

Excel automation     

Goto Top  

Import Excel Worksheets

Open an Excel workbook, loop through its sheets, and write data to a table. This makes it easy to summarize, forecast, slice and dice, make charts, and calculate metrics. s4p

File

Goto Top  

Read Text File

Open a text file and assign its contents to a string that is returned. String s4p

Loop and Rename

Loop through a folder of files and rename them to include date and time -- especially nice for those hundreds of pictures! s4p

Save String as a File

Create or modify a file whose contents is the text you specify. s4p

Form

Goto Top  

Filter a Form
(or Subform)

Filter a form, or a subform, or another form. Build a criteria string that only considers what is filled. Turn the form filter on or off. s4p

Find a Record

Find a Record. Often called on AfterUpdate event of Combo Box. s4p

Go to First

Go to the first record on a form. Byte (not used) s4p

Go to Last

Go to the last record on a form. Byte (not used) s4p

Go to Next

Go to the next record on a form. Byte (not used) s4p

Go to Previous

Go to the previous record on a form. Byte (not used) s4p

Function

Goto Top  

Get Age

Age in years given a date of birth. Optionally, specify date to calculate age as of. Integer s4p

Get Distance

Distance in miles, kilometers, or nautical miles from two points of Latutude and Longitude Double s4p

Get Long Integer From String

Get whole number from a string with other characters. Long s4p

Get Nth Word

Get the Nth word in a string. String s4p

ProperCase lowercase UPPERCASE

Convert a string to ProperCase, lowercase, or UPPERCASE ... in expressions and VBA. Use built-in functions StrConv, UCase, LCase, or a custom function (user-defined function --UDF), to make more adjustments depending on what you need. String s4p

Module

Goto Top  

Close open Modules

Close open standard modules. Optionally keep a module open by passing a module name or determining the active module (default), or close them all. Specify whether to save now, not save, or prompt (default) to save later. This is especially nice for developers so you can then open just what you want to focus on, switch faster, and realistically tile windows. Integer (and String) s4p

SQL

Goto Top  

Drop a table

Delete a table using an SQL statement with DROP TABLE. Optionally, specify another database (default is current database). s4p

Loop and Trim Text

Loop through all the tables and fields. Trim leading and trailing spaces from text by constructing and running SQL for an update query to Trim. Optionally, then also replace ZLS ("") with Null. This helps data compare and sort better. s4p

VBA in Access download

Highlight Current Record

Make it easier to see the current record on your Microsoft Access continuous or datasheet forms by adding a yellow highlight behind the current record. Uses Conditional Formatting, a control to keep track of the primary key value, a control to change color, and some VBA. See how to do it in a sample database you can download, and watch the video tutorial. s4p