Running a Subform Function from a Main Form

H

HumanJHawkins

I have a mainform where data is manipulated, and a subform where users
can write notes. But when users do certain things on the main form, I
want to automatically enter a note.

To manually add a note, the user types it (to an unbound field named
NewNote) in and clicks an "Add Note" button. The add note button
triggers this function:

Private Sub AddNote_Click()
If (Trim(Me.NewNote) <> "") Then
Me.AllowAdditions = True

DoCmd.GoToRecord , , acNewRec
Me.vchNoteBy = fOSUserName()
Me.dtmNoteDate = Now
Me.txtNote = Trim(Me.NewNote)
Me.NewNote = ""
Me.Requery

Me.AllowAdditions = False
End If

If Me.Dirty Then
DoCmd.RunCommand acCmdSaveRecord
End If
End Sub

That works fine. So, to support auto-entering notes, I added a public
accessor function in the notes subform:

Public Function AddNote(sNote As String)
Me.NewNote = sNote
AddNote_Click
End Function

Then, from the main form, I should be able to do this, right?

....Formulate note into a string called sNote
Me.Sub_Notes.AddNote(sNote)

However, I get an error that the function is not defined. Can anyone
see what is wrong?
 
S

Stuart McCall

HumanJHawkins said:
I have a mainform where data is manipulated, and a subform where users
can write notes. But when users do certain things on the main form, I
want to automatically enter a note.

To manually add a note, the user types it (to an unbound field named
NewNote) in and clicks an "Add Note" button. The add note button
triggers this function:

Private Sub AddNote_Click()
If (Trim(Me.NewNote) <> "") Then
Me.AllowAdditions = True

DoCmd.GoToRecord , , acNewRec
Me.vchNoteBy = fOSUserName()
Me.dtmNoteDate = Now
Me.txtNote = Trim(Me.NewNote)
Me.NewNote = ""
Me.Requery

Me.AllowAdditions = False
End If

If Me.Dirty Then
DoCmd.RunCommand acCmdSaveRecord
End If
End Sub

That works fine. So, to support auto-entering notes, I added a public
accessor function in the notes subform:

Public Function AddNote(sNote As String)
Me.NewNote = sNote
AddNote_Click
End Function

Then, from the main form, I should be able to do this, right?

...Formulate note into a string called sNote
Me.Sub_Notes.AddNote(sNote)

However, I get an error that the function is not defined. Can anyone
see what is wrong?

Me.Sub_Notes.AddNote(sNote)
The above line of code refers to the subform control which is hosting your
subform. To refer to the subform itself, you just need to add the .Form
property, like this:

Me.Sub_Notes.Form.AddNote(sNote)
 
H

HumanJHawkins

The above line of code refers to the subform control which is hosting your
subform. To refer to the subform itself, you just need to add the .Form
property, like this:

Me.Sub_Notes.Form.AddNote(sNote)

The error is different, but it is still note calling the function. If
I breakpoint the calling line and attempt ti "Step Into" the function,
it fails before going anywhere. So I don't think it is finding my
function. Now I get

Run-time error '40036':

Method of 'Form' of object '_SubForm' failed

The interesting thing is that '_SubForm' must be an MS object, as I
have nothing named that.

???
 
H

HumanJHawkins

The error is different, but it is still note calling the function. If
I breakpoint the calling line and attempt ti "Step Into" the function,
it fails before going anywhere. So I don't think it is finding my
function. Now I get

   Run-time error '40036':

   Method of 'Form' of object '_SubForm' failed

The interesting thing is that '_SubForm' must be an MS object, as I
have nothing named that.

???

Figured it out. Apparently there is another "AddNote" function
somewhere. Not sure why the error message wasn't more explicit, but
compiling the project from the Debug menu showed the error.

It turns out it isn't going to work anyway, because I was trying to do
this in a "BeforeUpdate" function. And Access won't let the subform
switch records to make a new note until the main form is saved. But I
learned something. I'm just going to handle this in the SQL.

Thanks!
 

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