Harry Batt
asked on
Recordsource Filter in Microsoft Access
Hello Experts,
What am I doing wrong? I have a form that I want to filter the recordsource based on a control called "Constituency". The code that I am using first gave me a parameter request and after I modified it doesn't do anything. The problem I seem to be having is with strFilter. Is this enough information or do you need more?
Thanks!
What am I doing wrong? I have a form that I want to filter the recordsource based on a control called "Constituency". The code that I am using first gave me a parameter request and after I modified it doesn't do anything. The problem I seem to be having is with strFilter. Is this enough information or do you need more?
Thanks!
Private Sub Constituency_AfterUpdate()
On Error Resume Next
Dim stFilterSQL As String
Dim strSQl As String
Me.RecordSource = strSQl
strFilter = Me.Constituency.Value
If Me.Constituency.Value <> "" Then
Me.lblFormTitle.Caption = strFilter & " Constituency"
strSQl = "SELECT qryContactTrim.Contact" & vbCrLf
strSQl = strSQl & " , tblContacts.*" & vbCrLf
strSQl = strSQl & " , tblDetail.*" & vbCrLf
strSQl = strSQl & " , tblConstituency.Constituency" & vbCrLf
strSQl = strSQl & " FROM (tblContacts " & vbCrLf
strSQl = strSQl & " INNER JOIN qryContactTrim " & vbCrLf
strSQl = strSQl & " ON tblContacts.ContactID = qryContactTrim.ContactID) " & vbCrLf
strSQl = strSQl & " INNER JOIN (tblDetail " & vbCrLf
strSQl = strSQl & " INNER JOIN tblConstituency " & vbCrLf
strSQl = strSQl & " ON tblDetail.fk_ConstituencyID.Value = tblConstituency.ConstituencyID) " & vbCrLf
strSQl = strSQl & " ON tblContacts.ContactID = tblDetail.fk_ContactID" & vbCrLf
strSQl = strSQl & " WHERE (((tblConstituency.Constituency = '" & strFilter & vbCrLf
strSQl = strSQl & " ORDER BY tblContacts.ContactLastName;"
Me.RecordSource = strSQl
Me.Detail.Visible = True
Me.Refresh
Else
Exit Sub
End If
End Sub
It's the where-string:
strSQl & " WHERE tblConstituency.Constituen cy = '" & strFilter & "'" & vbCrLf
/gustav
strSQl & " WHERE tblConstituency.Constituen
/gustav
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
To be more clear, the form that I am using has an unbound recordsource so that when it opens, nothing is displayed. The user can then create the recordsource using an after update event for the unbound cboconstituency control (changed name based on fyed's comment) located in the form's header. I am still not getting what I am doing wrong. I don't get an error message-I just don't get anything. Here is the new code:
Private Sub cboConstituency_AfterUpdate()
On Error Resume Next
Dim stFilterSQL As String
Dim strSQl As String
Me.RecordSource = strSQl
strFilter = "[Constituency] = " & Chr$(34) & Me.cboConstituency & Chr$(34)
If Me.cboConstituency.Value <> "" Then
Me.lblFormTitle.Caption = strFilter & " Constituency"
strSQl = "SELECT qryContactTrim.Contact" & vbCrLf
strSQl = strSQl & " , tblContacts.*" & vbCrLf
strSQl = strSQl & " , tblDetail.*" & vbCrLf
strSQl = strSQl & " , tblConstituency.Constituency" & vbCrLf
strSQl = strSQl & " FROM (tblContacts " & vbCrLf
strSQl = strSQl & " INNER JOIN qryContactTrim " & vbCrLf
strSQl = strSQl & " ON tblContacts.ContactID = qryContactTrim.ContactID) " & vbCrLf
strSQl = strSQl & " INNER JOIN (tblDetail " & vbCrLf
strSQl = strSQl & " INNER JOIN tblConstituency " & vbCrLf
strSQl = strSQl & " ON tblDetail.fk_ConstituencyID.Value = tblConstituency.ConstituencyID) " & vbCrLf
strSQl = strSQl & " ON tblContacts.ContactID = tblDetail.fk_ContactID" & vbCrLf
strSQl = strSQl & " WHERE (((tblConstituency.Constituency = '" & strFilter & "'" & vbCrLf
strSQl = strSQl & " ORDER BY tblContacts.ContactLastName;"
Me.RecordSource = strSQl
Me.Detail.Visible = True
Me.FormFooter.Visible = True
Me.Refresh
Else
Exit Sub
End If
End Sub
ASKER
So, I resisted my temptation to rewrite the SQL and am using an unbound combo box to filter the form. The only modification is that I have set the detail and footer to not visible until the combo has fired so that not everyone with a constituency is listed. Once again fyed's solution makes sense. What happened to the Capricorn1 moniker?
You will see him listed as "Rey Obrero" now. EE recently instituted a method to create an alias in your user profile. Mine now reads: fyed (Dale Fye), Rey chose to drop Capricorn1 (at least for now).
strSQl = strSQl & " , tblContacts.*" & vbCrLf
will look like this: " ,tblContact.*""
probably should code like this:
strSQl = strSQl & " , tblContacts.*"
Now (I forget)... if quotes are required around the asterisk... then this is the code.
strSQl = strSQl & " , tblContacts." & chr(13) & "*" & chr(13) & vbCrLf
I use the CHR(13) instead of "'" because the quotes around an apostrophe are too hard to view.
If an apostrophe I required around the asterisk - change the CHR(13) to "'" (that's quote - apostrophe - quote.
Here's what you do. Put a code break on your line: Me.RecordSource = strSQl
In the immediate window... type print strSQL.
Examine the results. That's what you're trying to run.
PS. You don't put a line feed at the end of each line. You only do that for viewing (eyeball) purposes if creating a "view" or stored procedure in SQL. The actual query doesn't require this.
Scott C