Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting textbox from string 1

Status
Not open for further replies.

sisan

Programmer
Jan 9, 2002
39
ID
Anybody please help me ...

Say I have a form and a textbox on it named :
txtSupName (as textBox)

I also have a variable named :
varSubName = "txtSupName" '(as string)

Is it possible to set the textbox's properties by the variable's value ("txtSupName" as string) not by txtSupName as TextBox ?

The clear is:

To set the value of TextBox is:

txtSupName.Text = "Sigit Santosa"

Now if I set its value by the variable:

varSubName.Text = "Sigit Santosa"
(the value of varSubName = "txtSubName")

of course the error will appear.

So, how the variable sets the properties can possible ?
(I think I can do this in FoxPro by '&' (macro substitution) )

Thank you!
 
Give this a try

Private Sub Command1_Click()
Dim varSubName As String
Dim ctrl As Control

varSubName = "txtSupName"

For Each ctrl In Me.Controls
If ctrl.Name = varSubName Then
ctrl.Text = "Sigit Santosa"
Exit For
End If
Next
End Sub

Let me know if this helps If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]

[cheers]
 

Why you would want to do this I do not know but I can answer your question. Yes it can be done but not the way you are describing. Start a demo project and add a textbox to your for and paste the following code in.
[tt]
Option Explicit

Dim T As TextBox

Private Sub Form_Load()
Set T = Text1
T.Text = "test"
End Sub
[/tt]

I hope this helps in your quest for knowledge. Good Luck
 
Thank for all your helps

I want to do this, because I'm tired to type :

txtSupNo = rs("SupNo")
txtSupName = rs("SupName")
txtSupAdd = rs("SupAdd")
...
...
..
..
.
.
(50 fields)

in my form to display the fields's value of a table. By the way that I asked, I want to make a sub and use the textbox controls in the form (as Foada's suggest) and fill it with the value of the table.

I think I've got what I want, and I will combine your two suggestions, such as:

For Each ctrl In Me.Controls
If Left(ctrl.Name) = "txt" Then
ctrl.Text = rs(Mid(ctrl.Name),4)
End If
Next

Or make a sub with vb5progmmr's advised to loop

2thumbsup.gif

Thanks, Foada
Thanks, vb5progmmr.
 
But that way also you will have to first test the Control Name and then assign the Recordset value to the Text Field..

don't you think this will increase the LOC.

The best way to do this could be using the Binding Collection. You can check the MSDN help on how to use this.

Hope this helps..

 
Put your textboxes in a control array and use the rs.fields collection.

for a = 0 to 49
textbox1(a)=rs.fields(a)
next a Let me know if this helps
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
You Should be able to access the Controls collection directly, using the control name as key. Since the control name can be determined from the database column name, you could do something like the following:
Code:
For ix = 0 To 10
   Controls("txt" & RecSet.Fields(ix).Name).Text = RecSet.Fields(ix).Value
Next ix
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi,

The solutions given by John and Centurion will work, if you are sure that there is a one-on-one mapping of Controls on the form and the Database Table Columns. But if not......Just think about it
 
Sorry I'm late to reply (coz weekend!)

I have tried the program I wrote above, and yes it is, I must sure that my textboxes name must be 'txt...', and must have the same name in the field's database.

It worked like what I want and I satisfied enough.

Thanks for you all.

2thumbsup.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top