setfocus question

depcdivr

Active Member
Joined
Jan 21, 2008
Messages
350
Office Version
  1. 365
Platform
  1. Windows
I am not sure what is happening with my code.

I have a very basic userform setup that will allow me to enter a SKU number, hit enter and it will add 1 to the current stock level. It is then supposed to reset the form and allow me to enter another item. I have set the code up so that it deletes the values of the text box(add_sku) and then sets the focus to that text box. it will not set the focus to that text box though. I picks the next textbox(Del_sku) and sets the focus to that instead.

I have a clear button(hidden unless the SKU entered is not found) When I click on that button I clear all text boxes and set the focus to add_sku and it works just fine.

I even tried to call the cancel_click sub routine (which works as described above) but this time it still sets the focus to the del_sku text box. What am I missing in the code that is moving the focus to the next box.

Code:
Sub Add_sku_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)    


If KeyCode = vbKeyReturn Then
         
        found = False
        Set ws2 = Worksheets("Data")
        irow = ws2.Cells(Rows.Count, 1).End(xlUp).Row
        For x = 2 To irow
            If Me.Add_SKU.Value = ws2.Cells(x, 4) Then
                If ws2.Cells(x, 17) = "" Then ws2.Cells(x, 17) = 0
                ws2.Cells(x, 17) = ws2.Cells(x, 17) + 1
                Me.Disp_sku.Value = ws2.Cells(x, 4)
                Me.Disp_name.Value = ws2.Cells(x, 1)
                Me.Disp_cost.Value = ws2.Cells(x, 14)
                Me.Disp_price.Value = ws2.Cells(x, 16)
                Me.Disp_Qty.Value = ws2.Cells(x, 17)
                found = True
            End If
       
        Next x
        
        If found = False Then
            Me.Add_to_inventory.Visible = True
            Me.Clear.Visible = True
            Me.Label3.Visible = True
            Me.Label4.Visible = True
            Me.Label5.Visible = True
            Me.Description.Visible = True
            Me.Cost.Visible = True
            Me.price.Visible = True
            
            Me.Description.SetFocus
            
        End If
        
    
    
        If found = True Then
                Me.Add_SKU.Value = ""
                Me.Add_SKU.SetFocus
        End If
    
    
    End If
    
End Sub


Private Sub Add_to_inventory_Click()


    If Me.Add_SKU.Value = "" Then
        MsgBox "Enter a SKU number"
        Exit Sub
    End If
    If Me.Description.Value = "" Then
        MsgBox "Enter a Product Description"
        Exit Sub
    End If
    If Me.price.Value = "" Then
        MsgBox "Enter a price"
        Exit Sub
    End If
    If Me.Cost.Value = "" Then
        MsgBox "Enter a product cost"
        Exit Sub
    End If
            
    Set ws2 = Worksheets("Data")
    irow = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1


    ws2.Cells(irow, 1) = Me.Description.Value
    ws2.Cells(irow, 4) = Me.Add_SKU.Value
    ws2.Cells(irow, 14) = Me.Cost.Value
    ws2.Cells(irow, 16) = Me.price.Value
    ws2.Cells(irow, 17) = 1
    ws2.Cells(irow, 2) = "Mommy Magic Shop"
    ws2.Cells(irow, 20) = 1
    
    
    Me.Disp_sku.Value = ws2.Cells(irow, 4)
    Me.Disp_name.Value = ws2.Cells(irow, 1)
    Me.Disp_cost.Value = ws2.Cells(irow, 14)
    Me.Disp_price.Value = ws2.Cells(irow, 16)
    Me.Disp_Qty.Value = ws2.Cells(irow, 17)


    Call Clear_Click
    
    
End Sub




Sub Clear_Click()


Me.Add_SKU.Value = ""
Me.Description.Value = ""
Me.Cost.Value = ""
Me.price.Value = ""


Me.Add_SKU.SetFocus


Me.Add_to_inventory.Visible = False
Me.Clear.Visible = False
Me.Label3.Visible = False
Me.Label4.Visible = False
Me.Label5.Visible = False
Me.Description.Visible = False
Me.Cost.Visible = False
Me.price.Visible = False






End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top