Solved Data not updating on a hidden form (1 Viewer)

mafhobb

Registered User.
Local time
Today, 03:34
Joined
Feb 28, 2006
Messages
1,175
I've got a form with property data form (frmPropertyHistory). On this form I have button to add a new property which opens an unbound, modal form (frmAddNewProperty). On this form new I have two fields with data to enter in a table (txtNewOwner and txtNewProperty). This is the code:
Code:
Private Sub cmdAdd_Click()

'Verify that there is data in both fields
    Me.txtNewOwner.SetFocus
    If Me.txtNewOwner.Text = "" Or IsNull(Me.txtNewOwner.Text) Then
        MsgBox "Please add a new owner"
        Exit Sub
    End If
    
    Me.txtNewProperty.SetFocus
     If Me.txtNewProperty.Text = "" Or IsNull(Me.txtNewProperty.Text) Then
        MsgBox "Please add a new property"
        Exit Sub
    End If

'   Insert data into tables so a new property is added
    Dim dbs As Database
    Dim NewOwner As String
    Dim NewProperty As String
    
    Me.txtNewOwner.SetFocus
    NewOwner = Me.txtNewOwner.Text
    Me.txtNewProperty.SetFocus
    NewProperty = Me.txtNewProperty.Text
    
    CurrentDb.Execute "INSERT INTO [tblOwners] (OwnerName)" & _
            " VALUES ('" & NewOwner & "')"
            
    CurrentDb.Execute "INSERT INTO [tblProperties] (PropertyName, OwnerName)" & _
            " VALUES ('" & NewProperty & "', '" & NewOwner & "')"
            
    DoCmd.Close
    
    DoCmd.Close acForm, "frmPropertyHistory", acSaveYes
    DoCmd.OpenForm "frmPropertyHistory"
            
End Sub
This works OK as the data is inserted in the tables, but in order for the original form (frmPropertyHistory) to show the new property, I need to close it and reload it. Why? Is there a more elegant way to refresh/requery this form to show the new data?
Without these two last lines of code, it is interesting to see that the combobox that I use on this form to select properties shows the new property name, but when I select it the new record does load.

Thanks

mafhobb
 

Minty

AWF VIP
Local time
Today, 09:34
Joined
Jul 26, 2013
Messages
9,240
If this form is modal in the source form after you open the this form simply requery your main form.

docmd.open "frmAddNewProperty"
Me.Requery

The reason this will work is that the calling form's code is paused until a modal form is closed and control passes back to the calling form.
 

mafhobb

Registered User.
Local time
Today, 03:34
Joined
Feb 28, 2006
Messages
1,175
That did it.

Many thanks

mafhobb
 

Users who are viewing this thread

Top Bottom