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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox array error Conversion from string "Button1" to type 'Integer' is not valid

Status
Not open for further replies.

access101

Programmer
Sep 4, 2010
68
US
I have multiple buttons( button1, button2, etc and instead of typing repeated code i just want the button to refer to the recordid and then based on that update the text value of the button


T=Form.RecordID.Text

Main.Controls("Button" & T).Text = Me.textbox1.Text



any help would be great thanks
 
What is T declared as?
Do you have a form named Form?
Is RecordID a control on your form named Form?
Do you have a form named Main?
"buttons( [red]b[/red]utton1, [red]b[/red]utton2, etc " but you want to change the control named "[red]B[/red]utton1"
Is your Button control placed on Main form itself, or is it placed in some kind of container (group box maybe?)

Have fun.

---- Andy
 
I tried to declare T as Integer then a string but still received the same error.

the form name is Main.vb

RecordID is a textbox on my Main form

Depending on the recordid i would like to go to the corresponding button() and change the text of the Button control. I was able to do this in access VBA using Main("Button" & T).Caption = Me.textbox1.Text but now I can't so I think its a formatting issue?
 

Which line throws the error?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
The error stops at Main.Controls("Button" & T).Text = Me.textbox1.Text
 
VB.NET 2010, simple form,
Text box named TextBox1
2 Buttons named: Button1 and Button2

Code:
Public Class Form1
    Private T As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load
        TextBox1.Text = "2"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
        T = Val(Me.TextBox1.Text)
        Me.Controls("Button" & T).Text = "This is Button " & Me.TextBox1.Text
    End Sub
End Class

Works like a charm... :)

Have fun.

---- Andy
 

How are the buttons added to the form, at design time or via code at run time?





I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
i tried your code now i get an error Object reference not set to an instance of an object
 
Are you talking about my code?
If so, did you try to start a new project, create those 3 controls on the Form, paste the code and run it?

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top