I am using Access 2007 to design a database that will allow users to search an inventory of historical photographs. Currently I have a table with fields such as Date, ID, Description, Project, Division, Unit, Facility, Feature, Subfeature, Region, Area Office, and State. I am using a query by form method that have either text boxes or combo boxes for each of the fields. I want users to be able to enter in the fields that they want to search by and leave others blank. I want the query to disregard the parameters left blank and then display the results that match ALL the parameters entered. For example I want to be able to enter a date range, Project Name, and Region and want the results to match all three criteria not all the records within the date range plus all records associated with the project plus all records from the specified region. I think it may have something to do with the use of AND and OR. If anyone has any words of wisdom it would be greatly appreciated. This is what I have come up with so far:
Code:
SELECT [ID] ,[Project] ,[Division] ,[Unit] ,[Feature] ,[Subfeature] ,[Region] ,[Spec No] ,[State] ,[Date] ,[Description] ,[Photographer] ,[Field1] ,[Latitude] ,[Longitude] ,[Facility] ,[AreaOffice] FROM [Historical Photos] WHERE (([Project]=[Forms]![Search]![txtProject]) OR ([Forms]![Search]![txtProject] Is Null) AND ([Division]=[Forms]![Search]![txtDivision]) OR ([Forms]![Search]![txtDivision] Is Null) AND ([Unit]=[Forms]![Search]![txtUnit]) OR ([Forms]![Search]![txtUnit] Is Null) AND ([ID]=[Forms]![Search]![txtPhotoID]) OR ([Forms]![Search]![txtPhotoID] Is Null) AND ([Feature]=[Forms]![Search]![txtFeature]) OR ([Forms]![Search]![txtFeature] Is Null) AND ([Subfeature]=[Forms]![Search]![txtSubfeature]) OR ([Forms]![Search]![txtSubfeature] Is Null) AND ([Facility]=[Forms]![Search]![txtFacility]) OR ([Forms]![Search]![txtFacility] Is Null) AND ([State]=[Forms]![Search]![cboState]) OR ([Forms]![Search]![cboState] Is Null) AND ([Region]=[Forms]![Search]![cboRegion]) OR ([Forms]![Search]![cboRegion] Is Null) AND ([AreaOffice]=[Forms]![Search]![cboAreaOffice]) OR ([Forms]![Search]![cboAreaOffice] Is Null) AND ([Description] Like "*" & [Forms]![Search]![txtDescription] & "*") OR ([Forms]![Search]![txtDescription] Is Null) AND ([Date] Between [Forms]![Search]![txtStart] AND [Forms]![Search]![txtEnd]) OR ([Forms]![Search]![txtStart] Is Null OR [Forms]![Search]![txtEnd] Is Null));
Comment