Reputation: 3030
StackOverflow. I have an issue I've been grappling with for too long. Currently, I have a form that displays consortium data, with a button that says "Create new consortium." When I click it, it shows a pop up window that allows you to enter the name, as displayed in the image below. Almost everything works fine, except I can't get it to display the new record after I create it. See my code below--you'll get a sense of what I'm trying to do.
By the way, if it matters, the "Consortium name" combo box allows you to select records. I don't see how it could throw my update off, but I thought I should include that information.
Here's the form:
Here's the code:
Interestingly, StackOverflow doesn't seem to indent the code properly after making an edit--I was able to do it successfully with a new test post. Oh well. I'm a noob to this as well:
Private Sub CreateConsortiumButton_Click()
If IsNull(Me.ConsortiumNameText) Or Me.ConsortiumNameText = "" Then ''insert better validation here
MsgBox "Please enter a valid consortium name."
Else
'' Create a new Consortium record with the ConsortiumNameText field on the popup window
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("Consortium")
rst.AddNew
rst("Consortium name").Value = Me.ConsortiumNameText
ConsortiumID = rst("ConsortiumID").Value
rst.Update
'' Now, create a corresponding record in the ResearchContributions table
Set rst = CurrentDb.OpenRecordset("ResearchContributions")
rst.AddNew
rst("ConsortiumID").Value = ConsortiumID
rst.Update
MsgBox "Consortium " & Me.ConsortiumNameText & " successfully created."
'' Change the form's combo box to reflect the new record
Forms!Main!NavigationSubform!ConsortiumNameCombobox = Me.ConsortiumNameText
'' Close popup
DoCmd.Close
'' Attempt to set the record on the main form to the newly created one - NOT WORKING!
DoCmd.OpenForm "Main", acNormal, , "[ConsortiumID] = " & ConsortiumID
End If
End Sub
I think part of the issue may have to do with the fact that I've placed the "Consortium Form" form, which is based around a tab control, into the "Main" form. Perhaps because I'm trying to open the Main form with a certain ConsortiumID, that ConsortiumID doesn't apply to Consortium Form. It appears as though I can't link the master with its child, but this hasn't been necessary in the past. Please forgive me if the concepts sound vague in confused, for it is only because the concepts are vague and confused as of yet in my mind--this is my first Access project. Anyone have any tips?
Upvotes: 0
Views: 3095
Reputation: 3030
Okay, I think I figured it out. I've added the following code:
DoCmd.OpenForm "Main"
DoCmd.Requery
DoCmd.SearchForRecord , , acFirst, "[ConsortiumID] = " & ConsortiumID
First, open the form, then requery in able to register the fact that the new item is in the database, then use SearchForRecord to open it up. Neato. Thanks, everyone.
Upvotes: 4