Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save data in main form using VB

Status
Not open for further replies.

redaccess

MIS
Aug 2, 2001
110
0
0
US
I've got a button on frm_Main that opens a sub-form without closing frm_Main. If I input a new record into frm_main and then click the button to go to the sub-Form, the sub-form doesn't recognize the new record I input in frm_Main (because it's not saved yet obviously). What VB code will work to automatically save the new record in frm_Main when I click the sub-form button. Here's a few things I've tried that doesn't seem to work.

docmd.refresh
runcommand (accmdsave)
application.runcommand (accmdsave)
me.application.runcommand (accmdsave)

I've tried all of these lines in the command button code before

DoCmd.OpenForm stDocName, , , stLinkCriteria

is executed and none of them seem to work.
Any suggestions?
 

Try putting this at the beginning of the on click event of your sub form button.

me.dirty = false.
Me.requery


Me.dirty = false will force the write of your new or updated record on the main form. The requery will force the underlying recordset to see the updated record.
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
I tried,

me.dirty = false.
Me.requery

at the beginning of the onclick but it didn't work. After trying it I went to the table and looked for the new record but it hadn't been inserted yet. Is there a way to get the new record inserted into the table?
 
redaccess,

Try this code behind your button. It should work just fine.

Private Sub "ButtonName"_Click() 'button name without quotes
stDocName = "Form to open" 'With quotes

DoCmd.RunCommand acCmdSaveRecord 'Forces form to save record
DoCmd.Minimize 'Minimzes the form.
'just an option since you keep it open
DoCmd.OpenForm stDocName, , , stLinkCriteria
'opens form
End Sub

Hope this helps remember that the minimize docmd command can be taken out and the record will be saved. However if you want to keep it in there you will need to put this code on the exit button of the form that was opened.

Private Sub Exit_Click()
On Error GoTo Err_Exit_Click

DoCmd.Close

stDocName = "Whatever..." 'Form name to be opened
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Restore 'restores form to original view etc..
Exit_Exit_Click:
Exit Sub

Err_Exit_Click:
MsgBox Err.Description
Resume Exit_Exit_Click

End Sub



Tofias1
 

You have a rather interesting problem. If your form is bound to a recordset, the following will force either a write of a new record or an update for a currently existing record.

Moving the focus from one form to another form, including a sub form. If the form has been changed in any way and if that form is attached to a viable recordset, setting the dirty property of the form will force an update to the underlying recordset.

Therefore,

1) are you sure your form is bound to a record set or is it an unbound form?

2) make sure if you are using a before update event you are not canceling the update request.

3) Make sure your form is bound to what you think it is bound to.

The only thing that would negate me.dirty = false is if you did a me.undo to cancel your changes, or cancelled the change event from within the forms before update event.
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
I tried using

DoCmd.RunCommand acCmdSaveRecord

but I get an error saying that the RunCommand action was canceled. In the detail of the error message it says "You used a DoCmd object to carry out an action in VB and then clicked Cancel in a Dialog box".

What's causing this error message to pop up? I even get this error when I use the command button wizard for the save record command which uses the following code.

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Any suggestions as to why this error occurs?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top