PDA

View Full Version : Solved: Userform buttons to enter numbers into textbox



Barryj
06-21-2008, 10:42 AM
I have attached a file with a userform, I am trying to get the buttons in frame1 to enter their value into a textbox as a score.

Which ever is the selected textbox when a button is pushed then its value is entered into that textbox.

I have done a search on several sites but not found anything that I can use.

I hope someone can point me in the right direction.

lucas
06-21-2008, 11:04 AM
I think the problem might be that as soon as you click a button then button is active not any of the textboxes.....

Norie
06-21-2008, 11:14 AM
Could the problem be that there's not actually any code in the userform module in the attached workbook?:confused:

Barryj
06-21-2008, 11:23 AM
I realise there is no code for the userform, I was after some help on that to point me in the right direction as I am not sure how to start with the coding.

Thanks for looking

mikerickson
06-21-2008, 11:25 AM
Another problem is that if the .TakeFocusOnClick is False for all the buttons, clicking on one of them passes the focus to the Frame. If the buttons are taken out of the frame. This code should work.
Private Sub UserForm_Initialize()
Dim i As Long
With Me
For i = 1 To 10
.Controls("CommandButton" & i).TakeFocusOnClick = False
Next i
.TextBox1.SetFocus
End With
End Sub

Sub buttonRoutine(ByRef oneButton As Object)
With Me.ActiveControl
If .Name Like "TextBox*" Then
.Text = oneButton.Caption
End If
End With
End Sub

Private Sub CommandButton1_Click()
Call buttonRoutine(Me.CommandButton1)
End Sub
Private Sub CommandButton2_Click()
Call buttonRoutine(Me.CommandButton2)
End Sub
Private Sub CommandButton3_Click()
Call buttonRoutine(Me.CommandButton3)
End Sub
Private Sub CommandButton4_Click()
Call buttonRoutine(Me.CommandButton4)
End Sub
Private Sub CommandButton5_Click()
Call buttonRoutine(Me.CommandButton5)
End Sub
Private Sub CommandButton6_Click()
Call buttonRoutine(Me.CommandButton6)
End Sub
Private Sub CommandButton7_Click()
Call buttonRoutine(Me.CommandButton7)
End Sub
Private Sub CommandButton8_Click()
Call buttonRoutine(Me.CommandButton8)
End Sub
Private Sub CommandButton9_Click()
Call buttonRoutine(Me.CommandButton9)
End Sub
Private Sub CommandButton10_Click()
Call buttonRoutine(Me.CommandButton10)
End Sub

Norie
06-21-2008, 11:39 AM
Barry

Could you explain further what you actually want to do?

Mike

That code doesn't work - for some reason the active control is Frame1.

Barryj
06-21-2008, 11:46 AM
Mike, I removed the frame on your code as you suggested and it is working fine, thankyou very much for your help, and the other guys who showed their intrest.

Norie
06-21-2008, 11:55 AM
Oops.:oops:

Never saw the bit about removing the frame.