Link to home
Start Free TrialLog in
Avatar of isurgyn
isurgyn

asked on

vba subform won't reload on change in recordsource

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

SOLUTION
Avatar of Anthony Berenguel
Anthony Berenguel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isurgyn
isurgyn

ASKER

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.
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.
SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isurgyn

ASKER

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