Consulting

Results 1 to 8 of 8

Thread: Solved: Userform buttons to enter numbers into textbox

  1. #1
    VBAX Tutor
    Joined
    Jan 2006
    Posts
    248
    Location

    Solved: Userform buttons to enter numbers into textbox

    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.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I think the problem might be that as soon as you click a button then button is active not any of the textboxes.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Could the problem be that there's not actually any code in the userform module in the attached workbook?

  4. #4
    VBAX Tutor
    Joined
    Jan 2006
    Posts
    248
    Location
    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

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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.
    [VBA]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[/VBA]

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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.

  7. #7
    VBAX Tutor
    Joined
    Jan 2006
    Posts
    248
    Location
    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.

  8. #8
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Oops.

    Never saw the bit about removing the frame.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •