469,165 Members | 2,180 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,165 developers. It's quick & easy.

How do you change the control source of a text box?

31
I have two forms, one with two command buttons, the other is a testing critera input form. Both cmd bottons open the same form. The first opens it to input test criteria, and is attached to a table. The other cmd button I want to open the same form, but I want it attached to a query to select the test criteria for a specific part which I will use is to test data from another table to pull out the good parts. I'm thinking I need to change the control source for several text boxes to display the proper test criteria from the query. Any ideas on how to do this.
Jan 20 '10 #1

✓ answered by TheSmileyCoder

This code will change the subform's source object (form) and then alter the recordsource.

I have used this code along with a treeview where a user can click on several nodes, representing different things. Based on what tag the node has, different forms will be opened.

The frm_Container is the name of the control holding the subform. This is just example code and you will have to modify it of course.


Expand|Select|Wrap|Line Numbers
  1. Select Case strTag
  2.                 Case "System"
  3.                     'Open the system Documents form
  4.                     If Not Me.frm_Container.SourceObject = "frm_System" Then Me.frm_Container.SourceObject = "frm_System"
  5.                     If Not Me.lbl_FormContainer.Caption = "System" Then Me.lbl_FormContainer.Caption = "System"
  6.                     'Set recordsource
  7.                     Me.frm_Container.Form.RecordSource = "SELECT tbl_EvalNodes.* " & _
  8.                                                     " FROM tbl_EvalNodes " & _
  9.                                                     " WHERE KEY_EvalNode=" & getKeyItem(node.Key)
  10. End Select
To change a single textbox to use another controlsource (I have not tested the implications of this or if it is possible at runtime)
Expand|Select|Wrap|Line Numbers
  1. me.tb_BidderID.ControlSource="[tx_Bidder]"
I think you will need to try it out, to see what happens when changing this at runtime. For instance, if user has allready entered a value in textbox, and you change the controlsource, what then?

7 4233
Delerna
1,134 Expert 1GB
the general syntax for changing control properties on another form is
Expand|Select|Wrap|Line Numbers
  1. Forms!FormName.ControlName.Property=value
  2.  
Jan 21 '10 #2
If I've understood your question correctly, do you want to change the "record source" of a form? Or do you only want to change the "control source" of the text box.
However, I will provide you with both solutions...
To change the "Record Source" of a form.

Here's some code...

Assuming you have a command button on the form called "cmdTestCriteria".
On the cllick event of "cmdTestCriteria".


Private Sub cmdTestCriteria_Click()

Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim frmTheSameForm As TheSameForm

Set dbs = CurrentDb()
Set rs = Me.RecordsetClone or Me.Recordset

DoCmd.OpenForm "frmTheSameForm"
TheSameForm.Recordset = "qryTestCriteria"
End Sub

If you want to change the "ControlSource" of one or several textboxes, then add as many tables and table fields to the query as needed.


Best Regards,

Lamar Dixon, Jr.
President
Sallient Technologies, LLC
MobileMessageMedia(MCubed)
Jan 21 '10 #3
tomric
31
Yes, I want to change the record source. I've tried the code and I keep comming up with a compile error, user-defined type not defined, on the statement Dim frmTestingcriteria as Testingcriteria. The form does exist and that's the name of it, any sugestions as to why this is occuring?
Jan 21 '10 #4
Hello,
The statement "Dim frmTestingcriteria as Testingcriteria" is a euphemism.

The "Compile Error" you are getting is because the form does not exist. Hence the "User-Defined Type". You're not going to open a new form, you're changing the recordset for the EXISTING form.

So here's some code...

Private Sub cmdTestCriteria_Click()

Dim dbs As DAO.Database
Dim rs As DAO.Recordset


Set dbs = CurrentDb()
Set rs = Me.RecordsetClone or Me.Recordset

Me.Recordset = "qryTestCriteria"
End Sub
Jan 22 '10 #5
tomric
31
Will that allow to open a second form and change the record set of that form at the same time?
Jan 22 '10 #6
TheSmileyCoder
2,321 Expert Mod 2GB
This code will change the subform's source object (form) and then alter the recordsource.

I have used this code along with a treeview where a user can click on several nodes, representing different things. Based on what tag the node has, different forms will be opened.

The frm_Container is the name of the control holding the subform. This is just example code and you will have to modify it of course.


Expand|Select|Wrap|Line Numbers
  1. Select Case strTag
  2.                 Case "System"
  3.                     'Open the system Documents form
  4.                     If Not Me.frm_Container.SourceObject = "frm_System" Then Me.frm_Container.SourceObject = "frm_System"
  5.                     If Not Me.lbl_FormContainer.Caption = "System" Then Me.lbl_FormContainer.Caption = "System"
  6.                     'Set recordsource
  7.                     Me.frm_Container.Form.RecordSource = "SELECT tbl_EvalNodes.* " & _
  8.                                                     " FROM tbl_EvalNodes " & _
  9.                                                     " WHERE KEY_EvalNode=" & getKeyItem(node.Key)
  10. End Select
To change a single textbox to use another controlsource (I have not tested the implications of this or if it is possible at runtime)
Expand|Select|Wrap|Line Numbers
  1. me.tb_BidderID.ControlSource="[tx_Bidder]"
I think you will need to try it out, to see what happens when changing this at runtime. For instance, if user has allready entered a value in textbox, and you change the controlsource, what then?
Jan 22 '10 #7
tomric
31
Thank you very much, I got it to work.
Jan 26 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Billy Jacobs | last post: by
1 post views Thread by Mirek Endys | last post: by
reply views Thread by nate | last post: by
6 posts views Thread by AAaron123 | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.