Using VB to Modify Data (Add a Record) in a Sub Form

H

HumanJHawkins

HI all,

I have a form with some data that has a subform with some related data.
I have an edit box and a command button on my main form.

I simply want to drop the contents of the edit box into a field on the
sub form when the users click the button. But I can find nothing in the
documentation or online with an example of how to do this.

This happens to be an "Access Data Project" (.adp) connected to SQL.
However, I think even a .mdb based example would be a huge help.

FYI, here is what I attempted, but I get an error on the new record
part:

<BEGIN CODE>
Private Sub Btn_AddRelationship_Click()
Me.EditBox.SetFocus 'Avoids an access error when not the
focus
Me.MySubForm.Form.NewRecord
Me.MySubForm.Form.Recordset("tMyTextField") = Me.EditBox.Text
End Sub
<END CODE>

Thanks in advance for any help!
 
B

Baz

HumanJHawkins said:
HI all,

I have a form with some data that has a subform with some related data.
I have an edit box and a command button on my main form.

I simply want to drop the contents of the edit box into a field on the
sub form when the users click the button. But I can find nothing in the
documentation or online with an example of how to do this.

This happens to be an "Access Data Project" (.adp) connected to SQL.
However, I think even a .mdb based example would be a huge help.

FYI, here is what I attempted, but I get an error on the new record
part:

<BEGIN CODE>
Private Sub Btn_AddRelationship_Click()
Me.EditBox.SetFocus 'Avoids an access error when not the
focus
Me.MySubForm.Form.NewRecord
Me.MySubForm.Form.Recordset("tMyTextField") = Me.EditBox.Text
End Sub
<END CODE>

Thanks in advance for any help!

You are using NewRecord as though it's a method, but it isn't: it's a
read-only property.

Try something like this:

Me!MySubForm.SetFocus
DoCmd.GoToRecord Record:=acNewRec
Me!MySubForm.Form!tMyTextField = Me!EditBox

You don't need to explicitly reference the Text property of Me!EditBox, and
if you avoid doing so then the text box doesn't need the focus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top