Retrieve a Hard Drive Serial Number in a Microsoft Access Form
Question details
The user needs to retrieve a computer's physical hard drive serial number and display it within an unbound text box on a Microsoft Access form.

- Product
- Microsoft Access
- Device & OS
- Windows
- Scenario
- Developing a Microsoft Access database form that requires fetching hardware-specific identifiers for auditing, licensing, or tracking purposes.
- Observed behavior
- The goal is to successfully retrieve the physical media serial number using Windows Management Instrumentation (WMI) and display it dynamically on the form via a custom VBA function.
Ensure you have access to the Visual Basic for Applications (VBA) editor in your database environment, and verify that your Windows user account has permissions to execute WMI queries.
Use WMI and VBA to Fetch the Physical Media Serial Number
Create a custom VBA function that uses Windows Management Instrumentation (WMI) to query the physical drive hardware and retrieve the manufacturer's serial number.
WMI provides a powerful interface for retrieving detailed hardware data in Windows environments. By querying the Win32_PhysicalMedia class, you can extract the manufacturer's hardware serial number for the physical drive. This identifier is permanent, unlike a volume serial number which changes when a drive is reformatted.
Open your Microsoft Access database and press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.
From the top menu, navigate to Insert > Module to create a new standard module (e.g., Module1).
Paste the following function into your new module: Public Function HDSerial() As String On Error GoTo errHandler Dim objWMI As Object, objWin32 As Object, objPM As Object Dim strSN As String Set objWMI = GetObject("WinMgmts:") Set objWin32 = objWMI.InstancesOf("Win32_PhysicalMedia") For Each objPM In objWin32 strSN = strSN & ";" & objPM.SerialNumber Next HDSerial = Trim(Mid(strSN, 2)) Set objPM = Nothing Set objWin32 = Nothing Set objWMI = Nothing Exit Function errHandler: HDSerial = "" End Function
Close the VBA editor and open your target Access form in Design View. Select the unbound text box where you want the serial number to appear. Open the Property Sheet, navigate to the Data tab, and set the Control Source property to: =HDSerial()

Looking for a Lightweight Microsoft Office Alternative?
While Microsoft Access handles highly specific relational database scenarios, for your daily spreadsheet, document, and presentation needs, WPS Office offers a free, lightweight, and incredibly compatible alternative. Seamlessly manage your regular office files without the hefty subscription fees.
- 1. Download WPS Office: Visit the official WPS website and download the free installation package for Windows.
- 2. Install the Software: Run the installer and follow the quick setup wizard to install the lightweight suite.
- 3. Open Your Office Files: Double-click any Word, Excel, or PowerPoint file to instantly open and edit it in WPS Office without formatting loss.

Frequently Asked Questions
Why is the VBA function returning a blank hard drive serial number?
The Win32_PhysicalMedia class relies entirely on the hardware manufacturer accurately reporting the serial number to Windows. If the manufacturer didn't embed a standard serial number, or if there are specific RAID controller drivers intercepting the query, WMI might return a blank string.
What is the difference between a physical serial number and a volume serial number?
The physical serial number is a permanent identifier assigned by the hardware manufacturer to the actual physical drive. A volume serial number is a temporary identifier generated by the operating system when a drive partition is formatted, and it will change if the drive is reformatted.
Can I retrieve the Volume Serial Number instead of the physical one?
Yes. If the physical serial number fails, you can use the FileSystemObject (FSO) library in VBA to retrieve the volume serial number of a specific logical drive (like your C: drive), which is often simpler but less permanent.
Will this WMI script work on a Mac environment?
No. Windows Management Instrumentation (WMI) is a proprietary Microsoft technology built into the Windows operating system. It will not execute correctly on macOS or inside environments that lack Windows API support.




