0

I'm trying to get this to work? How can I set a textbox to be used in an IN() statement? I would like users to be able to list out countries they'd like to query on

SELECT *
FROM Table
WHERE CountryID IN (forms!frmImport!txtCountries)
2
  • What technology? ASp.NET? Php? Html? Phone App? Commented Jun 9, 2014 at 19:49
  • Oops in Access using either SQL or VBA Commented Jun 9, 2014 at 19:50

1 Answer 1

1

You can't just put the name of the textbox directly into the SQL string.
Instead, you need to use the content of the textbox to build a SQL string like this:

SELECT * FROM Table WHERE CountryID IN (1,2,3)

If your users enter a comma-separated list of CountryIDs into the textbox, you can build the SQL like this:

Dim SQL As String

SQL = "SELECT * FROM Table WHERE CountryID IN (" & Forms!frmImport!txtCountries & ")"

But I wouldn't do it this way because it's simple, but prone to input errors and SQL injection.

A better way would be to use a list box, set the Multi Select property (so that multiple entries can be selected) and fill it with a list of all available countries.

Then, the user can select the ones that he wants, and you can build the SQL string using the selected items from the listbox:

Dim ListItem As Variant
Dim CountryList As String
Dim SQL As String

For Each ListItem In Forms!frmImport!lstCountries.ItemsSelected
    If CountryList > "" Then
        CountryList = CountryList & ","
    End If
    CountryList = CountryList & ListItem
Next

SQL = "SELECT * FROM Table WHERE CountryID IN (" & CountryList & ")"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.