I am attempting to assign a .RowSource to a ListBox using an SQL statement. I want to build the SQL statement "On-the-Fly", based on values in several controls on the form. I would like to restrict this discussion to Native Access, because when these discussions get beyond this, I don't understand, I cannot make them work, or I don't have the necessary resources.
For this question, I have built a simple DataBase, an AddressBook, because that is understandable to everyone, I don't need to spend effort explaining my issue, I can confine my discussion to Access. I have 2 tables.
Table = tAAA is as follows:
Table = tNames is as follows:
In tNames, Pointer is the Key in tAAA to link rows. Type is to determine if that name is displayed, after selection (see below).
To keep it simple here, there are 4 controls on the Form fAAA
txtSearch, a TextBox where User enters partial name to display in lbxNames
chkShowAlt, a CheckBox to specify whether Type=2 names are displayed. Type=1 are always displayed, Type=3 are never displayed.
lbxNames, a Listbox to display names as qualified by txtSearch and chkShowAlt
txtDisplay, a TextBox to display all bound data, but for this example, the field tAAA.Data. This is a simple test program, remember.
3 examples -
1) User enters "Kennedy", lbxNames should contain:
4 names if chkShowAlt=True
Bobby Kennedy
Jack Kennedy
John F. Kennedy
Robert F. Kennedy
2 names if chkShowAlt=Fals e
John F. Kennedy
Robert F. Kennedy
2) User enters "Robt", lbxNames should contain:
2 names if chkShowAlt=True
Bobby Kennedy
Robert F. Kennedy
1 name if chkShowAlt=Fals e
Robert F. Kennedy
3) User enters "Bob", lbxNames should contain:
2 names if chkShowAlt=True
Bobby Kennedy
Robert F. Kennedy
1 name if chkShowAlt=Fals e
Robert F. Kennedy
The SQL Statement "SELECT tNames.Pointer, tNames.Name FROM tNames WHERE (((tNames.Name) Like ""*Bob*"")) ;" returns the row with Key=1 & Name="Bobby Kennedy". I have saved this query as qStep1.
The SQL Statement
returns 2 rows, Key=1 and Key=9 when Type<3, and one row (Key=9) when Type<2.
That is exactly correct, and exactly what I want to do!!! But . . . I want a single SQL statement, if possible. I want to replace the "qStep1" with "SELECT tNames.Pointer . . .". Does this go in parens "()"? How do I refer to "qStep1.Pointer " when qStep1 becomes a SELECT Statement?
OIdBirdman
For this question, I have built a simple DataBase, an AddressBook, because that is understandable to everyone, I don't need to spend effort explaining my issue, I can confine my discussion to Access. I have 2 tables.
Table = tAAA is as follows:
Code:
Key Data 1 Wanted to be President of USA 2 Was President of USA 3 WWII General who became President of USA
Code:
Key Pointer Type Name 1 1 2 Bobby Kennedy 2 3 2 Ike Eisenhower 3 2 2 Jack Kennedy 4 1 3 Robt. Kennedy 5 2 1 John F. Kennedy 6 3 1 Dwight David Eisenhower 7 1 1 Robert F. Kennedy
To keep it simple here, there are 4 controls on the Form fAAA
txtSearch, a TextBox where User enters partial name to display in lbxNames
chkShowAlt, a CheckBox to specify whether Type=2 names are displayed. Type=1 are always displayed, Type=3 are never displayed.
lbxNames, a Listbox to display names as qualified by txtSearch and chkShowAlt
txtDisplay, a TextBox to display all bound data, but for this example, the field tAAA.Data. This is a simple test program, remember.
3 examples -
1) User enters "Kennedy", lbxNames should contain:
4 names if chkShowAlt=True
Bobby Kennedy
Jack Kennedy
John F. Kennedy
Robert F. Kennedy
2 names if chkShowAlt=Fals e
John F. Kennedy
Robert F. Kennedy
2) User enters "Robt", lbxNames should contain:
2 names if chkShowAlt=True
Bobby Kennedy
Robert F. Kennedy
1 name if chkShowAlt=Fals e
Robert F. Kennedy
3) User enters "Bob", lbxNames should contain:
2 names if chkShowAlt=True
Bobby Kennedy
Robert F. Kennedy
1 name if chkShowAlt=Fals e
Robert F. Kennedy
The SQL Statement "SELECT tNames.Pointer, tNames.Name FROM tNames WHERE (((tNames.Name) Like ""*Bob*"")) ;" returns the row with Key=1 & Name="Bobby Kennedy". I have saved this query as qStep1.
The SQL Statement
Code:
"SELECT tNames.Key, tNames.Name FROM tNames INNER JOIN qStep1 ON tNames.Pointer = qStep1.Pointer WHERE (((tNames.Type)<3)) ORDER BY tNames.Name;"
That is exactly correct, and exactly what I want to do!!! But . . . I want a single SQL statement, if possible. I want to replace the "qStep1" with "SELECT tNames.Pointer . . .". Does this go in parens "()"? How do I refer to "qStep1.Pointer " when qStep1 becomes a SELECT Statement?
OIdBirdman
Comment