I have a form in my database that I use as a search form. In the original query I used for the form, I used the values the user entered into the text values as the query parameters as in Forms![frmSearch]![BookingID]. After I had the form completed it was decided that a navigation form would be used for the database so now my query wouldn’t work. Rather than modifying the query so that the parameters now referenced my form which is now a sub form, I decided to set up my parameters so I could use the form or the query anywhere and not be restricted to just using my query or form in one place. I decided to just define parameters for my query and then use vba to define the parameters for the query definition and then use the query definition open record set method to define the record set for the form.
Here is my code:
Code:
Private Sub cmdSearch_Click()
Set MyDB = CurrentDb
Set MyQdf = CurrentDb.QueryDefs("qrySearchMain")
MaxLong = 2000000000
Me.Mindate = #1/1/1900#
Me.MaxDate = #1/1/2090#
With MyQdf
'//////////Set booking paramater
If IsNumeric(Me.txtLkBookingID) Then
' MsgBox "IsNumerid"
.Parameters("BookID_Low").Value = Me.txtLkBookingID
.Parameters("BookID_Hi").Value = Me.txtLkBookingID
Else
' MsgBox "NotNumerid"
.Parameters("BookID_Low").Value = 0
.Parameters("BookID_Hi").Value = Me.MaxLong
End If
'//////////Set event paramater
If IsNumeric(Me.txtLkEventIDNew) Then
.Parameters("EventID_low").Value = Me.txtLkEventIDNew
.Parameters("EventID_Hi").Value = Me.txtLkEventIDNew
Else
.Parameters("EventID_low").Value = 0
.Parameters("EventID_Hi").Value = Me.MaxLong
End If
'////////Set booking start date
If Not IsDate(Me.txtLkBookingFrom) Then
.Parameters("BkStartDate").Value = Me.Mindate
Else
.Parameters("BkStartDate").Value = Me.txtLkBookingFrom
End If
'///////Set booking end date paramater
If IsNull(Me.txtLkBookingTo) Then
.Parameters("BkEndDate").Value = Me.MaxDate
Else
.Parameters("BkEndDate").Value = Me.txtLkBookingTo
End If
'//////Set event data start paramater
If IsNull(Me.txtLkEventDateFrom) Then
.Parameters("EventStDate").Value = Me.Mindate
Else
.Parameters("EventStDate").Value = Me.txtLkEventDateFrom
End If
'////////Set event end date paramater
If IsNull(Me.txtLkEventDateTo) Then
.Parameters("EventEndDate").Value = Me.MaxDate
Else
.Parameters("EventEndDate").Value = Me.txtLkEventDateTo
End If
.Parameters("EventTypeLk").Value = Me.cmboLkEvenrType
.Parameters("Company").Value = Me.cmboLkCompanyName
.Parameters("EventPlaceLK").Value = Me.cmboLkEvenrPlace
.Parameters("CityLK").Value = Me.cmboLkCityOfEvent
End With 'Myqdf
Set Me.Recordset = MyQdf.OpenRecordset
End Sub
I got all of that to work. On the form that shows the results of the search, the user also needs to be able to sort the records. There could be as many as a couple of hundred records in the record set and depending on their purpose the may want to sort the records in a variety of was. Since I’m using a record set, when the user tries to sort all of the parameters pop up again.
My question is, Does anyone know how to get around this problem? How can I set the recordset to a query that requires parameters and still allow sorting?
Thanks