VBA Subform Requery Issue

verdes

Registered User.
Local time
Today, 15:33
Joined
Sep 10, 2012
Messages
49
I'm using access 2007.

I have a mainform and a subform linked properly.
I have a button on my mainform that will insert records to the underlying table of my subform when clicked.

The records are inserted into the table correctly, but they won't display in the subform until I move off the main form.

The underlying table doesn't contain records at the time the button on the mainform is clicked. That's a rule.

I requery the subform at the end of the button click event. It refreshes the subform with data from a previous record.

I don't know how to fix it or what I'm doing wrong.

I insert the records with doCmd.

Any help will be appreciated.

Here's the code from the onclick button event:
Dim myRS As DAO.Recordset
Dim dbs As Database
Dim strSql As String
Dim thefund As Integer
Dim theID As Integer
Dim sql2 As String
Dim calcamt As Currency
Dim thepct As Double
Dim cntrec As Integer
cntrec = 0
theID = Me.contributeID
'theamt = Me.contributeAmount
strSql = "Select * from tithing_Sys_details where titheSystem = " & Me.contributeTithesys & ";"
Set dbs = CurrentDb
Set myRS = dbs.OpenRecordset(strSql)
If Not (myRS.BOF And myRS.EOF) Then
myRS.MoveFirst
Else
MsgBox "Tithing records are missing. Contact Administrator. Allocate contribution manually."
Set myRS = Nothing
Set dbs = Nothing
Exit Sub
End If
Do While Not myRS.EOF
cntrec = cntrec + 1
thefund = myRS!titheFund
thepct = myRS!tithePercent
calcamt = theamt * thepct
'then I need to insert with docmd the allocation record
sql2 = "Insert into allocation_tbl(allocateFund, allocateAmount, allocateContribID, allocatePercent) Values (" & thefund & ", " & calcamt & ", " & theID & ", " & thepct & ");"
DoCmd.SetWarnings False
DoCmd.RunSQL sql2
DoCmd.SetWarnings True
myRS.MoveNext
Loop
MsgBox "tithe records read " & cntrec
Set myRS = Nothing
Set dbs = Nothing
Me.subfrm_Allocation.Requery
 
Sorry. I just fixed my requery syntax to Me.subfrm_Allocation.Form.Requery.

It appears to requery now. I'll do some more testing. Hope it's fixed.

Thanks
 
Fixed! Fixed! Fixed!

I had to add Me.Refresh at the top of the button code. The requery still wasn't working when I was creating a new main record and inserting new subform records.

Please let me know if you see a problem in my code for looping and inserting. Thanks.
 
What is the record source for the sub form, table or query?
How is the main form and sub form linked together, by "Link Master Fields" and "Link Child Fields"?

Okay, I see you solved it.
 
Please let me know if you see a problem in my code for looping and inserting. Thanks.
Can't you use an Append query instead of looping a recordset?
 

Users who are viewing this thread

Back
Top Bottom