Button to open a report to specific record

Patrick Snyder 5 Reputation points
2026-02-03T19:08:32.4933333+00:00

I asked this question prior but just wanted to start with a new thread.

I created a report but of course it displays all of the records from the data table. I want a button on my main form that prompts the user to enter a ID, like a primary key, but the fiedl that i am trying to used has number and letters combination and then populate the report with that single record from the data table..

I can get it to work with just the primary key using the following:

Private Sub Command19_Click()
    Dim vID As Variant
    Dim strWhere As String

    vID = InputBox("Enter the ID to display on the report:", "Enter Record ID")

    ' If user cancels or leaves it blank - do nothing
    If vID = "" Then Exit Sub
    If Not IsNumeric(vID) Then
        MsgBox "Please enter a valid numeric ID.", vbExclamation
        Exit Sub
    End If

    'strWhere = "[RFQ_num] = " & CLng(vID)
    
    strWhere = "[RFQ_num] = '" & vID & "'"
    
    DoCmd.OpenReport "MCT Report", acViewPreview, , strWhere
    
End Sub

So, what do I need to do to change it from the just a number field to a number/letter combination field. Examples: 765TR-123 or YT567 or 12345, or ABC-1.

Microsoft 365 and Office | Access | Other | Windows
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Duane Hookom 26,625 Reputation points Volunteer Moderator
    2026-02-03T20:49:24.8066667+00:00

    Stating “did not work” isn’t very helpful. We can’t see the results or error message, or whatever. I would add some debugging like

        strWhere = "[RFQ_num] = '" & vID & "'"
        Debug.print strWhere   'Take a look at the value
        DoCmd.OpenReport "MCT Report", acViewPreview, , strWhere
    End Sub
    

    In addition, you could paste this where string into the SQL view of a query to help troubleshoot.

    Also, I would not use an InputBox(). A combo box has many advantages.

    1 person found this answer helpful.

  2. Patrick Snyder 5 Reputation points
    2026-02-03T20:16:34.6533333+00:00

    To anyone that is reading this thread, the answer from the AI did not work on alphanumeric values.

    0 comments No comments

  3. Q&A Assist
    2026-02-03T19:08:44.8133333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To modify your existing code to handle a combination of numbers and letters for the ID, you are already on the right track with your strWhere assignment. The key is to ensure that you treat the ID as a string, which you are doing correctly by enclosing it in single quotes. Here's the relevant part of your code:

    strWhere = "[RFQ_num] = '" & vID & "'"
    

    This line constructs the strWhere string correctly for IDs that contain both letters and numbers. Since you are using single quotes around vID, it will work for values like 765TR-123, YT567, 12345, or ABC-1.

    You do not need to change anything else in your code for it to work with alphanumeric IDs. Just ensure that the RFQ_num field in your database is of a text type (not numeric) to accommodate these values.

    Here is your complete button click event with the necessary checks:

    Private Sub Command19_Click()
        Dim vID As Variant
        Dim strWhere As String
    
        vID = InputBox("Enter the ID to display on the report:", "Enter Record ID")
    
        ' If user cancels or leaves it blank - do nothing
        If vID = "" Then Exit Sub
    
        ' No need to check if numeric since it can be alphanumeric
        ' Just ensure the input is not empty
    
        strWhere = "[RFQ_num] = '" & vID & "'"
        DoCmd.OpenReport "MCT Report", acViewPreview, , strWhere
    End Sub
    

    This should allow your report to open with the specific record based on the ID entered by the user, regardless of whether it is purely numeric or alphanumeric.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.