473,651 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to change the RowSource in a SubForm in VBA code

Dear reader,

The syntax for the VBA code to change the RowSource of a Form is:

Forms!FormName. .RowSource = "TableOrQueryNa me"

Bu now the syntax to change the RowSource of a SubForm:

Forms!MasterFor mName..!SubForm Name...RowSourc e =
"TableOrQueryNa me"

Is not working.

Is there a working syntax for this?

Tanks for any help.

Kind regards,

Simon
Aug 19 '07 #1
8 23781
See if this link helps: http://www.mvps.org/access/forms/frm0031.htm

Simon wrote:
>Dear reader,

The syntax for the VBA code to change the RowSource of a Form is:

Forms!FormName. .RowSource = "TableOrQueryNa me"

Bu now the syntax to change the RowSource of a SubForm:

Forms!MasterFor mName..!SubForm Name...RowSourc e =
"TableOrQueryN ame"

Is not working.

Is there a working syntax for this?

Tanks for any help.

Kind regards,

Simon
--
HTH - RuralGuy (RG for short) acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200708/1

Aug 19 '07 #2
forms don't have a RowSource property. you can change the *RecordSource*
property of a subform from code running in the main form, as

Me!MainForm!Sub formControlName .Form.RecordSou rce = "something"

where Me refers to the main form. if the code is running *outside* both the
mainform and subform, replace Me with Forms.

hth
"Simon" <Sv********@Ver satel.nlwrote in message
news:46******** **************@ news.tele2.nl.. .
Dear reader,

The syntax for the VBA code to change the RowSource of a Form is:

Forms!FormName. .RowSource = "TableOrQueryNa me"

Bu now the syntax to change the RowSource of a SubForm:

Forms!MasterFor mName..!SubForm Name...RowSourc e =
"TableOrQueryNa me"

Is not working.

Is there a working syntax for this?

Tanks for any help.

Kind regards,

Simon


Aug 20 '07 #3
Hi Tina,
I think you will find the syntax is either:
Me.SubFormContr olName.Form.Rec ordSource
or
Forms.MainFormN ame.SubFormCont rolName.Form.Re cordSource

tina wrote:
>forms don't have a RowSource property. you can change the *RecordSource*
property of a subform from code running in the main form, as

Me!MainForm!Sub formControlName .Form.RecordSou rce = "something"

where Me refers to the main form. if the code is running *outside* both the
mainform and subform, replace Me with Forms.

hth
>Dear reader,
[quoted text clipped - 16 lines]
>>
Simon
--
HTH - RuralGuy (RG for short) acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200708/1

Aug 20 '07 #4
Simon, just a follow up note.

When you do something like:
Me.[SubFormControlN ame].Form.RecordSou rce = "SELECT ...
Access will reset the LinkMasterField s and LinkChildFields property of the
subform control. If it gets it right, things will be fine. If it gets it
wrong, the subform may show all record (not just the related ones), no
records at all, or the wrong records.

An alternative approach might be to set the Filter of the subform:
With Me.[SubFormControlN ame].Form
.Filter = "[SomeField] = 999"
.FilterOn = True
End With

However, there's a bug in Access (all versions before 2007) such that this
doesn't work correctly either if you filter the main form as well. Details
in this article:
Incorrect filtering of forms and reports
at:
http://allenbrowne.com/bug-02.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"ruralguy via AccessMonster.c om" <u12102@uwewrot e in message
news:76f44264fa 476@uwe...
Hi Tina,
I think you will find the syntax is either:
Me.SubFormContr olName.Form.Rec ordSource
or
Forms.MainFormN ame.SubFormCont rolName.Form.Re cordSource

tina wrote:
>>forms don't have a RowSource property. you can change the *RecordSource*
property of a subform from code running in the main form, as

Me!MainForm!Sub formControlName .Form.RecordSou rce = "something"

where Me refers to the main form. if the code is running *outside* both
the
mainform and subform, replace Me with Forms.
Aug 20 '07 #5
yeah, i blew that one, all right - must have been asleep when i wrote it!
"ruralguy via AccessMonster.c om" <u12102@uwewrot e in message
news:76f44264fa 476@uwe...
Hi Tina,
I think you will find the syntax is either:
Me.SubFormContr olName.Form.Rec ordSource
or
Forms.MainFormN ame.SubFormCont rolName.Form.Re cordSource

tina wrote:
forms don't have a RowSource property. you can change the *RecordSource*
property of a subform from code running in the main form, as

Me!MainForm!Sub formControlName .Form.RecordSou rce = "something"

where Me refers to the main form. if the code is running *outside* both
the
mainform and subform, replace Me with Forms.

hth
Dear reader,
[quoted text clipped - 16 lines]
>
Simon

--
HTH - RuralGuy (RG for short) acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200708/1

Aug 21 '07 #6
Been there...Done that. ;^)

Take care

tina wrote:
>yeah, i blew that one, all right - must have been asleep when i wrote it!
>Hi Tina,
I think you will find the syntax is either:
[quoted text clipped - 17 lines]
>>
Simon
--
HTH - RuralGuy (RG for short) acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via http://www.accessmonster.com

Aug 21 '07 #7
On Aug 19, 10:46 pm, "Allen Browne" <AllenBro...@Se eSig.Invalid>
wrote:
Simon, just a follow up note.

When you do something like:
Me.[SubFormControlN ame].Form.RecordSou rce = "SELECT ...
Access will reset the LinkMasterField s and LinkChildFields property of the
subform control. If it gets it right, things will be fine. If it gets it
wrong, the subform may show all record (not just the related ones), no
records at all, or the wrong records.
That's an interesting observation.
>From the A97 help for LinkChildFields , LinkMasterField s Properties:
When you create a subform or subreport by dragging a form or report
from the Database window onto another form or report or by using the
Form Wizard, Microsoft Access automatically sets the LinkChildFields
and LinkMasterField s properties under the following conditions:

· Both the main form or report and the child object are based on
tables, and a relationship between those tables has been defined with
the Relationships command. Microsoft Access uses the fields that
relate the two tables as the linking fields.
· The main form or report is based on a table with a primary key, and
the subform or subreport is based on a table or query that contains a
field with the same name and the same or a compatible data type as the
primary key. Microsoft Access uses the primary key from the main
object's underlying table and the identically named field from the
child object's underlying table or query as the linking fields.

Although setting a subform's RecordSource dynamically isn't mentioned
specifically, I imagine that the same principles apply. I've never
had Access get this wrong, but thanks to your warning I'll be on the
lookout for it.

James A. Fortune
CD********@Fort uneJames.com

Aug 22 '07 #8
Yes, that's a good description of what is it supposed to do.

Most times it gets it right if the conditions you met are right. But there
are complicating factors where Access may not be able to figure it out
correctly. For example, where the related table has more than one foreign
key of the right data type to choose from (regardless of whether the names
match or not), and you are using queries as the source.

It makes sense that the same problem situtions could arise when setting the
subform's RecordSource on the fly.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<CD********@For tuneJames.comwr ote in message
news:11******** **************@ i13g2000prf.goo glegroups.com.. .
On Aug 19, 10:46 pm, "Allen Browne" <AllenBro...@Se eSig.Invalid>
wrote:
Simon, just a follow up note.

When you do something like:
Me.[SubFormControlN ame].Form.RecordSou rce = "SELECT ...
Access will reset the LinkMasterField s and LinkChildFields property of the
subform control. If it gets it right, things will be fine. If it gets it
wrong, the subform may show all record (not just the related ones), no
records at all, or the wrong records.
That's an interesting observation.
>From the A97 help for LinkChildFields , LinkMasterField s Properties:
When you create a subform or subreport by dragging a form or report
from the Database window onto another form or report or by using the
Form Wizard, Microsoft Access automatically sets the LinkChildFields
and LinkMasterField s properties under the following conditions:

· Both the main form or report and the child object are based on
tables, and a relationship between those tables has been defined with
the Relationships command. Microsoft Access uses the fields that
relate the two tables as the linking fields.
· The main form or report is based on a table with a primary key, and
the subform or subreport is based on a table or query that contains a
field with the same name and the same or a compatible data type as the
primary key. Microsoft Access uses the primary key from the main
object's underlying table and the identically named field from the
child object's underlying table or query as the linking fields.

Although setting a subform's RecordSource dynamically isn't mentioned
specifically, I imagine that the same principles apply. I've never
had Access get this wrong, but thanks to your warning I'll be on the
lookout for it.

James A. Fortune
CD********@Fort uneJames.com

Aug 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2321
by: Saintor | last post by:
The following function works when used directly from a form. It locks controls depending on a value stored in the tag property. Called from a button on a subform, it does not work . TIA. Public Function UpdFormPropsLock(sForm As String, sLockLevel As Integer, bSign As Boolean, Optional iJump As Integer) As Boolean Dim sM1 As String On Error Resume Next
3
4164
by: Sam Gledhill | last post by:
Hi, I'm hoping someone can help me out. I have a database with a form. This form has no recordsource (I'm using it as a complex MsgBox). It has a single textbox of type Date. It also has three unrelated subform that are filtered according to the date entered in the form's text box. By using after update events, I've been able to allow users to change the date in the textbox and alter records from the day throughout three other tables. ...
4
4672
by: Corey | last post by:
I am having some real trouble getting a listbox rowsource to load properly. The listbox is actually on a subform. My main form has several command buttons that change the MASTER_SUB_FORM object source to the specific sub form needed by the end user. When designing the sub form i used the querybuilder to create an SQL statment to define the listbox values. **** RowSource SQL Statement ****
5
1709
by: Guoqi Zheng | last post by:
I think I have this kind of problem very often, now it is really making me crazy. VS.NET always want to change my html code. Ok, that is fine as long as Vs.Net can ask my confirmaton first. For example, I have below in header part of my html page. <base href="http://localhost/more/"> When I click save, VS.NET change it back to <base href="http://localhost/more"> automatically without asking me or anything. It just remove the last "/" by...
11
1619
by: sreekandan | last post by:
Hi everybody, I want to retrieve the details from the database by the date from (MM/DD/YYYY) to (MM/DD/YYYY). For that I have to change the following code as follows rs.Open "select * from Table where empid='" & Text1.Text & "'", conSo kindly anyone reply me.
3
4636
by: Peter Hofmann | last post by:
When I change the source code and save it I have currently always to press explicitely F6 zu "compile" the code (and get new error list). Isn't there an automatic build function which is called whenever I save new code ? BTW: What is the difference between the two menues: Build->Build solution and
1
2108
by: noneedforthis | last post by:
I'm a new user of Access/VB and ran in to this nasty wall. (Access 2003, VB6.3, Windows XP) Runtime Error 2001: You cancelled the previous operation. The highlighted line is the one where the recordsource is set to a previously defined SQL statement. What I don't understand is that the code was working perfectly for a long time and it suddenly appeared. Here's what my program does - search for items based on the owner/item type etc...
10
2765
by: mukeshrasm | last post by:
Hi the code written below is for calculating the number of days remain for a particular event based on the specific local time i.e. time in new york or time in london. <style style="text/css"> .lcdstyle{ /*Example CSS to create LCD countdown look*/ background-color:black; color:lime; font: bold 18px MS Sans Serif;
1
1607
by: neelsfer | last post by:
When i use this code to count the number of records on display in a specific subform in the "txtRecordNo" field that is open now, it seems to "update" all the time as the cursor changes every second and i am unable to add new records on mainform, untill i click in one of the fields of this Subform. Then it settles down. Otherwise it works 100% when i add records in the subform. pls help Private Sub Form_Current() Dim rst As...
0
8352
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.