Results 1 to 3 of 3

Problem with SetFocus

Threaded View

  1. #1
    Registered User
    Join Date
    11-20-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    29

    Problem with SetFocus

    Hey everyone!

    I am having a little trouble with an addition flash card userform that I am working on. Everything was working exactly as I intended until I tried to implement a keyup event. My intention was to remove the need to press return twice (once for entering answer in text box, and once again to proceed to the next question). Now, with the keyup event, I am able to enter the answer and go to the next question, however the focus is then placed on the "Next" command button. I have tried putting "me.txtSolution.SetFocus" throughout my code to no avail. Any thoughts would be greatly appreciated.

    Thank you for your help! This is what I have so far:

    Private Sub btnNext_Click()
        
        Dim n1, n2 As Integer
        
        If IsNumeric(txtSolution.Value) Then
            lblAddend1.ForeColor = vbBlack
            lblAddend2.ForeColor = vbBlack
        If Int(lblAddend1.Caption) + Int(lblAddend2.Caption) <> Int(txtSolution.Value) Then
            lblAddend1.ForeColor = vbRed
            lblAddend2.ForeColor = vbRed
            txtSolution.Value = ""
        Else
            txtSolution.Value = ""
            
            Dim high, low As Double
            
            low = Worksheets("Data").Range("C4").Value
            high = Worksheets("Data").Range("C5").Value
            low2 = Worksheets("Data").Range("F4").Value
            high2 = Worksheets("Data").Range("F5").Value
            n1 = Int((high - low + 1) * Rnd() + low)
            n2 = Int((high2 - low2 + 1) * Rnd() + low2)
            lblAddend1.Caption = n1
            lblAddend2.Caption = n2
          
        End If
        Else
            txtSolution.Value = ""
        End If
        Me.txtSolution.SetFocus
        
    End Sub
    
    Private Sub txtSolution_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        
        If KeyCode = vbKeyReturn Then
            Call btnNext_Click
        End If
    
    End Sub
    Last edited by exceln00b22; 05-25-2013 at 05:12 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1