Unanswered: Referencing table field names as a variable
The basics of the problem are this: I have a form with 1 combo box (searchfield) and one text box (searchfor). The combo box is populated with all of the field names from the table tblPeople. The idea is that the user can select which field name they want to add to a query (qrySearch) and apply a filter to that field using the text box (searchfor). My code, listed below, works beautifully as long as I manually enter the searchfield (in this case “Title”) in both the SELECT and WHERE statement. How can I use a variable to automatically insert the searchfield value for me?
Private Sub Search_Click()
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("qrySearch")
qdf.SQL = "SELECT [Full Name], [Residence],[Title] From tblPeople WHERE Title= Forms!frmSearch!searchfor"
Dim strFieldName as String
strFieldName = Me.searchfield.Value
qdf.SQL = Replace("SELECT [Full Name], [Residence],[@Field] From tblPeople WHERE @Field= Forms!frmSearch!searchfor", "@Field", strFieldName)
Do not forget that if the concerned field is of type Text the value should be enclosed in single quotes.
the aim is store once, not store multiple times
Remember... Optimize 'til you die!
Progaming environment: Access based on my own environment: DAO3.6/A97/A2000/A2003/A2007/A2010 VB based on my own environment: vb6 sp5 ASP based on my own environment: 5.6 VB-NET based on my own environment started 2007 SQL-2005 based on my own environment started 2008 MYLE YOUR PASSWORD IS JUST LIKE YOUR TOOTHBRUSH DON'T SHARE IT.