Use Microsoft Access VBA to Find or Add a Customer Ticket
Question details
Find a ticket for the selected customer or open a new ticket record with that customer's ID.

- 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.
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.
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.
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
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
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.

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.

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.




