VBA & Macro Problems

Use Microsoft Access VBA to Find or Add a Customer Ticket

Rana GarciaRana Garcia Nov 20, 2026 869 views

Question details

Find a ticket for the selected customer or open a new ticket record with that customer's ID.

Find or Add a Customer Ticket with Microsoft Access VBA
Product
Microsoft Access
Device & OS
not provided
Scenario
A CustomerList form opens a Tickets form bound to the Tickets table.
Observed behavior
The Tickets form should open an existing matching record for editing, or a new record when no ticket exists for the customer.
Before you start

Confirm that CustomerID is a numeric Long field, that the Tickets form is bound to the Tickets table, and that its CustomerID control is bound to the matching field. Enable trusted VBA content and close the Tickets form before testing; use a customer with and without an existing ticket.

Solution 1Recommended

Search for the customer with DAO and open the Tickets form

Use this approach to open the first matching ticket for editing or start a new ticket for the selected customer.

The example assumes CustomerID is numeric and that the form and control names match those shown. If several tickets match, the filtered form can contain more than one record.

For a new ticket, the code passes the customer ID through OpenArgs. The form assigns it to the new record; the user must still complete any other required fields and save the record.

1
Add the button code

In the CustomerList form's Design View, select the button that opens Tickets. Open its Property Sheet, choose Event > On Click > Event Procedure, and add this code to the click procedure: Dim rst As DAO.Recordset Dim customerId As Long Dim ticketExists As Boolean If IsNull(Me!CustomerID) Then MsgBox "Select a customer first." Exit Sub End If customerId = CLng(Me!CustomerID) Set rst = CurrentDb.OpenRecordset("Tickets", dbOpenDynaset) rst.FindFirst "CustomerID = " & customerId ticketExists = Not rst.NoMatch rst.Close Set rst = Nothing If ticketExists Then DoCmd.OpenForm "Tickets", WhereCondition:="CustomerID = " & customerId Else DoCmd.OpenForm "Tickets", DataMode:=acFormAdd, OpenArgs:=CStr(customerId) End If

2
Set the ID on a new ticket

In the Tickets form's Design View, open Property Sheet > Event > On Current > Event Procedure and add: If Me.NewRecord And Not IsNull(Me.OpenArgs) Then If IsNull(Me!CustomerID) Then Me!CustomerID = CLng(Me.OpenArgs) End If End If

3
Test both paths

Open CustomerList in Form View and click the button for a customer with an existing ticket. Close Tickets, then try a customer without a ticket; complete the required fields in the new record and save it.

Search for the customer with DAO and open the Tickets form
Allow new records: The Tickets form must allow additions and use an updatable record source. If CustomerID is text rather than numeric, the criteria and OpenArgs conversion must be changed.
Free Microsoft Office alternative

Work with customer spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for everyday document and spreadsheet work. Open compatible Office files to move existing spreadsheet workflows over easily. WPS Office does not fix Access VBA or replace an Access database.

Review and organize customer lists in WPS SpreadsheetOpen and edit Microsoft Excel XLSX filesUse a familiar interface for everyday office tasksChoose a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

What happens if a customer has multiple tickets?

The WhereCondition filters the Tickets form to matching CustomerID values. The form opens on a matching record, and users can navigate through other matches using the form's record navigation controls.

Does opening a new ticket save it automatically?

No. The Current event fills in CustomerID on the new form record. Complete any other required fields and save the record using the form's save control or Access's normal record-saving behavior.

Why does Access report that DAO.Recordset is undefined?

In the VBA editor, open Tools > References and check that an appropriate Microsoft Office Access database engine object library is available and selected. Resolve any missing references before compiling again.

Can I use this code with a text CustomerID?

Not unchanged. Remove the Long and CLng conversions and build a quoted text criterion, escaping any apostrophes in the ID. Also assign the text OpenArgs value directly in the Tickets form.