views:

122

answers:

1

I'm trying to have some SQL execute when I open a report. This works fine when I try to match on a column that's an integer with an integer, but when I try to match on a "text" column, it keeps popping up a dialog asking for what you want to filter on.

Here's a somple query:

select person_phone_numbers.person_id from person_phone_numbers where phone_number = '444-444-4444'

This is actually a sub-query I'm trying to use, but this is where the problem is. If I change it to this it works fine:

select person_phone_numbers.person_id from person_phone_numbers where phone_id = 2

I put this in the OnOpen event and I'm assigning it to Me.RecordSource if that makes a difference. My goal here is to have a form accept query parameter(s) and have it open a report with the results.

EDIT: Full code snippet:

Private Sub Report_Open(Cancel As Integer)
    Me.RecordSource = "SELECT person.original_name, person.normalized_name, phone_number.full_number, phone_number.type, person.person_id " _
                & "FROM phone_number INNER JOIN (person INNER JOIN person_phone_numbers ON person.person_id = person_phone_numbers.person_id) " _
                & "ON phone_number.full_number = person_phone_numbers.full_number where person.person_id IN " _
                & "(select person_phone_numbers.person_id from person_phone_numbers where phone_number = '444-444-4444')"
End Sub

Any thoughts on why it wants to ask for a parameter vs. just running the query the way I have it?

A: 

is the phone # field actually storing the hash marks?

from what you have supplied for code, i do not see anything that would cause a parameter prompt - please post the entire query

Badfish
No. It just stores the number with dashes. E.g. 444-444-4444.I edited the original post to have the full code snippet that I'm trying to run. My goal is to replace where it says '444-444-4444' with a reference to a form control in a report or perhaps a variable later on. This is just some test code that I'm using to make sure I know exactly how to do this.
blockcipher
I would recommend against hardwiring any form control references into SQL. Instead, in the report's OnOpen event, dynamically construct your SQL string with literal values and then assign it as the report's Recordsource. Granted, that's getting ahead of ourselves, since we haven't actually solved your existing problem yet, but that's because we don't yet have enough information (see my comment on your original question).
David-W-Fenton
Well, that's, I think, what I'm shooting for. I'm hoping that once I get this resolved, I can take the '444-444-4444' and instead have a reference to a form element.
blockcipher
I'm not sure you're understanding what I said. My suggestion is that the resulting SQL would have the test value hardwired into it, just like the '444-44-4444'. But the WHERE clause would be written at runtime using the value pulled from the form control, and assigned to the form's Recordsource in the report's OnOpen event.
David-W-Fenton