I have a form where users select from different listboxes which I then use to build a SQL statement to return the matching records. On the main form I have a subform that is based on the query that was just created.
The problem is that I can't seem to get the returned records to display in the subform. They end up in a pop-up datasheet. What am I doing wrong?
The problem is that I can't seem to get the returned records to display in the subform. They end up in a pop-up datasheet. What am I doing wrong?
Code:
Private Sub cmdRunQuery_Click()
Dim Db As DAO.Database
Dim QD As QueryDef
Dim where As Variant
Set Db = CurrentDb()
' Delete the existing dynamic query; trap the error if the query does not exist.
'On Error Resume Next
'Db.QueryDefs.Delete ("Dynamic_Query")
'On Error GoTo 0
where = Null
where = "WHERE (((tblRouting.UNIQUE_LANE_ID) In (SELECT UNIQUE_LANE_ID FROM tblRouting where"
where = where & " tblRouting.Location_ID= '" + Me!Text35 + "'))"
where = where & " AND tblRouting.Final_Dest= '" + Me!List29 + "'"
where = where & " AND tblRouting.Ship_Day= '" + Me!cboShipDay + "')"
Set QD = Db.CreateQueryDef("Dynamic_Query", _
"Select tblRouting.LOCATION_ID, tblLocation.NAME, tblLocation.CITY, tblLocation.STATE, " & _
" tblLocation.REGION, tblRouting.UNIQUE_LANE_ID, tblRouting.CARRIER_ID, tblRouting.SHIP_DAY, " & _
" tblRouting.DELIVERY_DAY, tblRouting.TIME_AT_LOCATION, tblRouting.STOP_NUM, " & _
" tblRouting.NO_STOPS FROM tblLocation INNER JOIN tblRouting ON tblLocation.LOCATION_ID = " & _
" tblRouting.LOCATION_ID " & (" where " + Mid(where, 6) & ";"))
DoCmd.OpenForm ("subfrmDynamicQuery")
On Error Resume Next
Db.QueryDefs.Delete ("Dynamic_Query")
On Error GoTo 0
End Sub