Access - Select a particular Record based on a text string

Posted on 2012-09-16
Last Modified: 2012-09-16
Good evening all,

I have a bit of code that I'm running into some trouble with.

Trying to have a combobox SELECT a record based upon a text string.

The table that I'm looking the value up in is named:

That Table has 2 fields Named (Data Type):
 ShiftTypeID (autonumber) and ShiftType (text)

My Code is:
Dim strUseShiftType As String
    strUseShiftType = "Clerk"

    Me.cboShiftTypeSelected.RowSource = "SELECT ShiftTypeID, ShiftType FROM Shift_Types WHERE ShiftType = " & strUseShiftType
    Me.cboShiftTypeSelected = Me.cboShiftTypeSelected.ItemData(0)

Open in new window

When I execute the code it opens an "Enter Parameter Value" "Clerk" input box. ??????
Question by:wlwebb
    LVL 48

    Accepted Solution

    Try adding some single quotes:

    Me.cboShiftTypeSelected.RowSource = "SELECT ShiftTypeID, ShiftType FROM Shift_Types WHERE ShiftType = '" & strUseShiftType & "'"


    Author Closing Comment

    Thanks gustav!  That did it!  Don't understand why you have to add single vs double quotes.....
    LVL 48

    Expert Comment

    by:Gustav Brock
    That's because it shall be read as a string. If omitted, the value is supposed to be a number.
    Thus, were you looking up an Id, it could have read:

      WHERE ShiftTypeId = " & lngUseShiftTypeId & ""

    The last element (& "") is not needed but often appended anyway to show that the SQL statement ends here.


    Author Comment

    Ok, got it......  Thank you
    LVL 48

    Expert Comment

    by:Gustav Brock
    You are welcome!


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now