Solved

vba subform won't reload on change in recordsource

Posted on 2014-10-24
6
745 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
6 Comments
 
LVL 10

Assisted Solution

by:Anthony Berenguel
Anthony Berenguel earned 167 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 166 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 167 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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 …
Viewers will learn how the fundamental information of how to create a table.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now