I have a problem with a search form I'm building. My search criteria works without a problem with text searches. But I added a couple numbers searches (cboSearchOrgan ization and cboSearchShopNa me). It seems my use/placement of AND is not correct. It is telling me invalid syntax.
If I delete the shopname string and just leave Organization, it works without a problem. It seems to me like my problem is with AND. What am I doing wrong?
Code:
Private Sub cmdSearch_Click() Dim sqlSearch As String If Not IsNull(Me.cboSearchLastName) Then sqlSearch = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _ & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _ & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _ & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _ & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _ & " WHERE LastName ='" & Me.cboSearchLastName & "'" _ & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _ & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _ & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _ & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _ & " WHERE LastName ='" & Me.cboSearchLastName & "'AND" End If If Not IsNull(Me.cboSearchFirstName) Then sqlSearch = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _ & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _ & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _ & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _ & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _ & " WHERE FirstName ='" & Me.cboSearchFirstName & "'" _ & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _ & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _ & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _ & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _ & " WHERE FirstName ='" & Me.cboSearchFirstName & "'AND" End If If Not IsNull(Me.cboSearchOrganization) Then sqlSearch = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _ & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _ & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _ & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _ & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _ & " WHERE OrganizationFK =" & Me.cboSearchOrganization & "" _ & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _ & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _ & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _ & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _ & " WHERE OrganizationFK =" & Me.cboSearchOrganization & " AND " End If If Not IsNull(Me.cboSearchShopName) Then sqlSearch = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _ & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _ & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _ & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _ & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _ & " WHERE ShopNameFK =" & Me.cboSearchShopName & "" _ & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _ & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _ & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _ & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _ & " WHERE ShopNameFK =" & Me.cboSearchShopName & "" End If Call MsgBox(sqlSearch, vbOKOnly, "Debug") Me.RecordSource = sqlSearch End Sub
Comment