Learning to built Functions and such

  • Thread starter Thread starter shanesullaway via AccessMonster.com
  • Start date Start date
S

shanesullaway via AccessMonster.com

I'm just starting to built my own functions and subs and would like to ask a
couple of questions, so that I can gain a little more knowledge on how to do
this.

Question #1

If I have a particular Form with a Subform on it and was going to be
referring to it often in my code. How could I make something that would
contain the full name? ie Forms![MainForm]![subForm].Form
Also, once this is done. How would I refer to it, say for instance I was
changing the RecordSource for the subForm.

Question #2
Kind of refers to question #1. How would I built something that would
contain a SQL statement for the RecordSource of the subform and how would I
refer to it.

Hope this makes sense and I appreciate any help you can throw my way.
Shane
 
The example below shows a function that accepts 2 arguments:
- a reference to a form;
- a SQL statement string.

Public Function AssignSource(frm As Form, strSql As String) As Boolean
If frm.Dirty Then frm.Dirty = False 'save first
frm.RecordSource = strSql
AssignSource = True
End Function

Once you have that function in a general module, you can call it from any
form's module (including that of a subform) like this:
Call AssignSource(Me, "SELECT * FROM Table1;")

Or, if you need to call it from outside that form's own module, you can pass
the reference like this:
Call AssignSource(Forms![MainForm]![subForm].Form, "Table1")
 
Hi,
as I understand your question:

to refer to subform's form:
dim frm as form
set frm=Forms![MainForm]![subForm].Form
debug.print frm.recordsource

and:

dim strSQL as string
strSQL="Select * from mytable"

Forms![MainForm]![subForm].Form.RecordSource=strSQL


--
Best regards,
___________
Alex Dybenko (MVP)
http://alexdyb.blogspot.com
http://www.PointLtd.com
 
Thank you for your replies Mr. Browne and Mr. Dybenko,

While I always learn something from you, Mr. Browne and will add your method
to my collection, Mr. Dybenko's reply is more in line of what I would like to
accomplish. One more question to go with what you have written Mr. Dybenko.

You finished with this: Forms![MainForm]![subForm].Form.RecordSource=strSQL

Would: frm.RecordSource = strSQL work too? That is what I'm
really try to accomplish. I will be typing Forms![MainForm]![subForm].Form
over and over, so what makes sense to me, is to get this line into something
much shorter then just refer to the shorter line.

Thanks again to both of you,
Shane

Alex said:
Hi,
as I understand your question:

to refer to subform's form:
dim frm as form
set frm=Forms![MainForm]![subForm].Form
debug.print frm.recordsource

and:

dim strSQL as string
strSQL="Select * from mytable"

Forms![MainForm]![subForm].Form.RecordSource=strSQL
I'm just starting to built my own functions and subs and would like to ask
a
[quoted text clipped - 19 lines]
Hope this makes sense and I appreciate any help you can throw my way.
Shane
 
Mr. Browne and Mr. Dybenko,

Here's what I finished up with and it works. Thanks again for helping me
through a project that I spent several days trying to accomplish. If I need
to tweak it in any way then please feel free to jump in and say so. I am
very familiar with work. Both of you and feel priviledged that you would
take the time to help me out.

Shane

Public Function fCust() As Form

Set fCust = Forms![frmCustomers]![subCusDet].Form

End Function

Function fSQL() As String

fSQL = "SELECT * FROM [Progress Notes] WHERE [Progress Notes].CustomersID
=" _
& Forms![frmCustomers]![CustomersID] & " AND [Progress Notes].
CreatedBy='" & _
Me.cboEnteredBy.Column(1) & "'"

End Function

And I call it in my code using this:
fCust.RecordSource = fSQL
Thank you for your replies Mr. Browne and Mr. Dybenko,

While I always learn something from you, Mr. Browne and will add your method
to my collection, Mr. Dybenko's reply is more in line of what I would like to
accomplish. One more question to go with what you have written Mr. Dybenko.

You finished with this: Forms![MainForm]![subForm].Form.RecordSource=strSQL

Would: frm.RecordSource = strSQL work too? That is what I'm
really try to accomplish. I will be typing Forms![MainForm]![subForm].Form
over and over, so what makes sense to me, is to get this line into something
much shorter then just refer to the shorter line.

Thanks again to both of you,
Shane
Hi,
as I understand your question:
[quoted text clipped - 16 lines]
 
Back
Top