referring to texbox

B

baha17

Hello All,
I have multiple textboxes in my userform. The reason I am just trying
to create a grids inside the userform(that is the only way I think
of:)). Here is my question, if I have too many texboxes with the names
are like ; textbox1,textbox2,textbox3...textbox34,textbox35 etc. my
code is something like below which I placed into keypress event for
userform.But I always get error for textbox name. Can you help me to
get me through
Thank you very much in advance

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 49 Then
GameNum = Sheet1.Range("game").Value 'that is the counter to key
press event
Label1.BackColor = &HFFC0C0
TextBox"& GameNum = "T" ' HERE I GET ERROR

End If
If KeyAscii = 50 Then
Label1.BackColor = &HFF&

End If
Sheet1.Range("game") = Sheet1.Range("game") + 1

End Sub
 
J

Jim Cone

TextBox"& GameNum = "T"

Should be...

Me.Controls("Textbox" & GameNum).Value = "T"
---
Jim Cone
Portland, Oregon USA
http://www.contextures.com/excel-sort-addin.html
(30+ ways to sort)

..
..
..

<[email protected]>
wrote in message
Hello All,
I have multiple textboxes in my userform. The reason I am just trying
to create a grids inside the userform(that is the only way I think
of:)). Here is my question, if I have too many texboxes with the names
are like ; textbox1,textbox2,textbox3...textbox34,textbox35 etc. my
code is something like below which I placed into keypress event for
userform.But I always get error for textbox name. Can you help me to
get me through
Thank you very much in advance

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 49 Then
GameNum = Sheet1.Range("game").Value 'that is the counter to key
press event
Label1.BackColor = &HFFC0C0
TextBox"& GameNum = "T" ' HERE I GET ERROR

End If
If KeyAscii = 50 Then
Label1.BackColor = &HFF&

End If
Sheet1.Range("game") = Sheet1.Range("game") + 1

End Sub
 
B

baha17

Hi Jim,
Do you know why the option for Keypress does not work.I updated my
code as below:
Dim GameNum As Variant

If KeyAscii = 49 Then
GameNum = Sheet1.Range("game").Value
Label10.Caption = GameNum
Label1.BackColor = &HFFC0C0
Controls.Item("TextBox" & GameNum) = "T"
Sheet1.Range("game") = Sheet1.Range("game") + 1
End If

If KeyAscii = 50 Then
GameNum = Sheet1.Range("game").Value
Label10.Caption = GameNum
Label2.BackColor = &HFF&
Controls.Item("TextBox" & GameNum) = "O"
Sheet1.Range("game") = Sheet1.Range("game") + 1
End If

Exit Sub
If KeyAscii = 51 Then
GameNum = Sheet1.Range("game").Value
Label10.Caption = GameNum
Label2.BackColor = &HFF&
Controls.Item("TextBox" & GameNum) = "O"
Sheet1.Range("game") = Sheet1.Range("game") + 1
End If
 
J

Jim Cone

Without knowing what "does not work" means...
I would guess that you need a keypress event for each textbox.
Your code only executes if the keypress occurs over the userform not the textboxes.
--
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware

..
..
..

<[email protected]>
wrote in message
Hi Jim,
Do you know why the option for Keypress does not work.I updated my
code as below:
Dim GameNum As Variant

If KeyAscii = 49 Then
GameNum = Sheet1.Range("game").Value
Label10.Caption = GameNum
Label1.BackColor = &HFFC0C0
Controls.Item("TextBox" & GameNum) = "T"
Sheet1.Range("game") = Sheet1.Range("game") + 1
End If

If KeyAscii = 50 Then
GameNum = Sheet1.Range("game").Value
Label10.Caption = GameNum
Label2.BackColor = &HFF&
Controls.Item("TextBox" & GameNum) = "O"
Sheet1.Range("game") = Sheet1.Range("game") + 1
End If

Exit Sub
If KeyAscii = 51 Then
GameNum = Sheet1.Range("game").Value
Label10.Caption = GameNum
Label2.BackColor = &HFF&
Controls.Item("TextBox" & GameNum) = "O"
Sheet1.Range("game") = Sheet1.Range("game") + 1
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top