?
Solved

vba subform won't reload on change in recordsource

Posted on 2014-10-24
6
Medium Priority
?
932 Views
Last Modified: 2014-11-07
I have a subform in an Access 2007 db that needs to have its recordsource changed based upon varying search criteria.  The accdb is a front end that connects via ODBC to a MS SQL Server 2008 back end.

I have tried multiple different methods and they all provide the same result.  The subform unloads the old records but fails to show the results of the new recordsource even though the underlying query does bring back the appropriate records.

The only way I can get the form to show the new records is to close the form and reopen it.  For a main form that would not be a problem but not so easy with a subform.

Given that this is supposed to be so simple I am wondering if this is the result of some quirk of ODBC and the requery / refresh method.

I am posting the code for review.  It is a function that is called from another routine.  The same result occurs whether or not the code is executed in the main form, subform etc.
        'Creates a recordsource for frmContactLookupMaster and binds the subform to it.
        Dim strWhere As String
 
        If IsDate(Me.txtContactDate) = True Then
            '--Add a 60 day timestamp window
            strWhere = "AND InquiryTimeStamp >= #" & DateAdd("d", -30, txtContactDate) & "# AND  InquiryTimeStamp < #" & DateAdd("d", 30, txtContactDate) & "# "
        End If
 
       '--Build the filtered SQL statement and bind the recordsource
       Me.frmContactLookupMaster.Form.RecordSource = "SELECT Last, Middle, First, InquiryTimeStamp, " & _
            "ContactID " & _
            "From tblContacts  " & _
            "WHERE Last Like '" & txtLastName & "*' AND First Like '" & txtFirstName & "*' " & strWhere & _
            "ORDER BY Last, First, InquiryTimeStamp DESC"
            

Open in new window

0
Comment
Question by:isurgyn
[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 10

Assisted Solution

by:Anthony Berenguel
Anthony Berenguel earned 668 total points
ID: 40402361
Have you already tried me.refresh or me.requery from the parent form after you change the record source?
0
 

Author Comment

by:isurgyn
ID: 40402435
The subform requery is implicit in the change recordsource method.   So a requery or refresh on the subform makes no difference.  Don't believe I have tried a requery on the main form mainly because I don't want to requery the main form as that will requery all of the subforms on the main form and screw up those recordsets.

That said, I have used the same type method to change the recordsource on another main form with no subforms and it had the identical problem.  The only way to get it to show the new recordsource was to close the form and reopen it.
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 40402489
I want to say i've got around the same problem you're having with the subform by refreshing or requerying the main form. I'll have to dig into some old applications to see if I can find an example. But in the meantime I think it would be something worth trying -- if it's not a problem to also requery the main form.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 85

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 664 total points
ID: 40402641
Do you have the Master and Child linkfields set on the Subform? If you do, and if those links are not valid after resetting the Recordsource, then the subform wouldn't show records.

Note too that SQL Server using the percent sign ( % ) as the wildcard, not the asterisk ( * ). If you're hitting a sql database, you should change that in your SQL statement.
0
 
LVL 31

Accepted Solution

by:
Helen Feddema earned 668 total points
ID: 40404260
I have had similar problems, and found that clearing the record source, then doing whatever you need to do to recreate the record source, then re-assigning the record source to the subform, works well.  Here is some sample code:

      Echo False
      
      Me.RecordSource = ""
      CurrentDb.Execute strSQL
      Me.RecordSource = "qryContactCodes"
      strSearch = "[ContactCode] = " & lngCode _
         & " AND [ContactID] = " & lngContactID
      Me.Recordset.FindFirst strSearch

      Echo True

Open in new window


The point of the search is to get back to the record where you started out (it may change when requerying).
0
 

Author Comment

by:isurgyn
ID: 40421527
Hi,

Sorry for taking so long to get back on this.  Other db crisis to deal with.

I have tried all of the suggested solutions and none solve the problem.  The requery and refresh I have tried on both main and subforms to no avail.  The subform is a lookup form and the criteria are entered into unbound fields on the main form.  Because we are using Like we want to be able to bring back any similar records.  That said, I did try linking master and child fields an no change.

I thought that Helen had it nailed but again, no change in the subform.  The SQL statement works great so the query criteria are not a problem.  

Any other suggestions?  Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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