[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Recordsource Filter in Microsoft Access

Posted on 2014-02-23
6
Medium Priority
?
1,224 Views
Last Modified: 2014-02-24
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!
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

Open in new window

0
Comment
Question by:Harry Batt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 20

Expert Comment

by:clarkscott
ID: 39881842
Your strings are wrong.
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
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 39881855
It's the where-string:

strSQl & "       WHERE tblConstituency.Constituency = '" & strFilter & "'" & vbCrLf

/gustav
0
 
LVL 48

Accepted Solution

by:
Dale Fye earned 2000 total points
ID: 39882269
If all you want to do is "filter" the recordsource, then you don't need to rewrite the SQL, you simply need to filter the form.

Private Sub Constituency_AfterUpdate()
On Error Resume Next
Dim stFilterSQL As String

'if [Consitiuency] is numeric, use this
strFilter = "[Constituency] = " & Me.Constituency
'if string, use:
strFilter = "[Constituency] = " & chr$(34) & me.Constituency & chr$(34)

me.filter = strFilter
me.filteron = true

End Sub

Open in new window

BTW, although Access automatically creates controls with Name the same as the field that they are bound to.  It is normally a good idea to rename your controls, so that you know you are referring to a control, not a field name.

In this case:

strFilter = "[Constituency] = " & Me.Constituency

It looks like you are referring to a field in the form.  Normally with this kind of functionality, you would put an unbound textbox or combo in the form header and give it a name like: txt_Filter_Constituency or cbo_Filter_Constituency.  Then, the syntax would read:

strFilter = "[Constituency] = " & chr$(34) & Me.txt_Filter_Constituency & chr$ (34)
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:Harry Batt
ID: 39883820
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

Open in new window

0
 

Author Closing Comment

by:Harry Batt
ID: 39884322
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?
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39884484
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).
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question