Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to get main form to requery

Posted on 2014-12-30
9
Medium Priority
?
137 Views
Last Modified: 2014-12-31
I have this code in an afterupdate event of a combobox on a sub-form.  I want it to requery or refresh or whatever the data on the main form's current record but instead it is bringing the user back to the first record.  What am I doing wrong?

Private Sub cboClass_AfterUpdate()
    Forms!frmMainForm.Requery
End Sub

Open in new window

0
Comment
Question by:SteveL13
[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
  • 3
  • 3
  • 3
9 Comments
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 40524833
A Requery will do that.  You need to store the unique ID field of the current record and after the requery move the pointer back to that record as shown below ...

Dim rst as Recordset
Dim varCurrentID

set rst = me.RecordsetClone
varCurrentId = Me.ID
Forms!frmMainForm.Requery
rst.FindFirst "[ID] = " & varCurrentID
Me.Bookmark = rst.Bookmark


ET
0
 
LVL 85
ID: 40525185
Why would you requery the datasource after selecting a value in a combo? Requery refetches the recordset from the datasource, so I doubt you want to do that.

What's the goal of your combo? Are you using it for record selections? If so, and if the record will be located somewhere in the form's Recordsource, you can use the Bookmark method.
0
 

Author Comment

by:SteveL13
ID: 40525418
The reason I need to requery the datasource of the main form is that after the combobox on the sub-form is updated there is a field on the main form that gets it's value from this code in the oncurrent event of the main form:

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim strMedClasses As String
    
    Set db = CurrentDb
    strSQL = "SELECT DISTINCT tblLOCALTempDrChromoPatientAndMedImport.ChronoID, tblLOCALTempDrChromoPatientAndMedImport.Class FROM tblLOCALTempDrChromoPatientAndMedImport WHERE tblLOCALTempDrChromoPatientAndMedImport.ChronoID = '" & Me.txtChronoID & "'"

    Set rs = db.OpenRecordset(strSQL)
    
    Do While Not rs.EOF
        strMedClasses = strMedClasses & ", " & rs.Fields("Class")
        rs.MoveNext
    Loop
    
    If Len(Trim(strMedClasses)) > 0 Then
        Me.txtMedClasses = Right(strMedClasses, Len(Trim(strMedClasses)) - 2)
    Else
        Me.txtMedClasses = ""
    End If
    
    Me.txtMedClasses2 = Me.txtMedClasses
    
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing

Open in new window


Using the first suggestion I am getting a "syntax error (missing operator) in expression".

Here is the after update code of the combobox on the sub-form:

Dim rst As Recordset
Dim varCurrentID

 Set rst = Me.RecordsetClone
 varCurrentID = Me.ID
 Forms!frmMainForm.Requery
 rst.FindFirst "[ID] = " & varCurrentID
 Me.Bookmark = rst.Bookmark

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 85
ID: 40525438
Why don't you just run that code again when needed, from the Subform? You can add a function to the mainform, and call that function from the subform:

Me.Parent.NameOfYourFunction

If you must use the bookmark method:

Are you running this code on the SUBFORM? If so, then "Me" refers to the object in which the code is running. If you're running this on the subform, but intend to work with the recordset of the mainform, you'd have to do this:

Dim rst As Recordset
Dim varCurrentID

 Set rst = Me.Parent.RecordsetClone
 varCurrentID = Me.ID
 Forms!frmMainForm.Requery
 rst.FindFirst "[ID] = " & varCurrentID
 Me.Parent.Bookmark = rst.Bookmark
0
 

Author Comment

by:SteveL13
ID: 40525454
I like this idea:

"Why don't you just run that code again when needed, from the Subform? You can add a function to the mainform, and call that function from the subform:"

 Me.Parent.NameOfYourFunction

But how do I add the function to the main form?  And then do I just add

Me.Parent.NameOfYourFunction

to the afterupdate event of the combobox?
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 40525477
You add a function to a form just like you do a standard module:

Function MyFunction()
  <code here
End Function

From there, you would call it just as you infer:

Me.Parent.MyFunction
0
 

Author Comment

by:SteveL13
ID: 40525510
Perfect.  Thank you very much.
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 40525555
Yes, as Scott pointed out you will need to add the .Parent because you are requiring the main form from the sub form.  

If you want to put the code on the main form and call it from the sub form then try this ... Put this in the VBA module of your main form.


Public Function ResetCurrentRecordID()
 Dim rst as Recordset
 Dim varCurrentID

 set rst = me.RecordsetClone
 varCurrentId = Me.ID
 Forms!frmMainForm.Requery
 rst.FindFirst "[ID] = " & varCurrentID
 Me.Bookmark = rst.Bookmark

End Function  

In the AfterUpdate Event of the combobox on your sub form enter the following:

Me.Parent.ResetCurrentRecordID


ET
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 40525565
Sorry ... I did not refresh my screen before posting.

ET
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.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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 …

722 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