SetFocus problem

J

jogamen

Hi i have folowing code and the problem is than when cell is empty the code
should move focus to the textbox1 with error mesege but instead of that it is
running again the code for textbox2_enter. Any idea what is the problem?
Thank you


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
udaje = TextBox1.Text
dlzka = TextBox1.TextLength
If dlzka < 9 Then GoTo line1
kontrola = IsNumeric(udaje)
If kontrola = False Then GoTo line1 Else GoTo line2
line1:
TextBox1.SetFocus
MsgBox " Nebolo zadané správne Äíslo! Tu možeÅ¡ zadaÅ¥ len 9 miestne
Äíslo.", vbCritical, " CHYBA ÄŒÃSLA TOVARU"
Cancel = True
Exit Sub
line2:
End Sub

Private Sub TextBox2_Enter()
If TextBox1.Text = "" Then GoTo line1 Else GoTo line2
line1:
TextBox1.SetFocus
MsgBox "Nebol zadaný Äiarový kód!", vbCritical, " Chyba v Äísle tovaru"
Exit Sub
line2:
Workbooks.Open "\\Alpha\quort_prenos\Ciselnik skladu.xls"
Workbooks("Ciselnik skladu.xls").Activate
Worksheets("Udaje").Select

vyhladat = TextBox1.Text
riadok = 1
Cells(riadok, 1).Select
kontrola = Cells(riadok, 1).Text

Do Until kontrola = vyhladat
riadok = riadok + 1
Cells(riadok, 1).Select
kontrola = Cells(riadok, 1).Text
If Cells(riadok, 1).Text = "" Then GoTo line3
Loop

Label1.Caption = Cells(riadok, 2).Text
UserForm1.Caption = UserForm1.Caption & " - " & Cells(riadok, 2).Text
GoTo lastline
line3:
TextBox1.SetFocus
MsgBox " Takýto tovar sa nenachádza v skladovej evidencii! Skontroluj
si Äíslo tovaru.", vbInformation, " POZOR"
lastline:
Workbooks("Ciselnik skladu.xls").Close SaveChanges:=False
Exit Sub
End Sub
 
B

Bob Bridges

Which sub is running, jogamen? If you're not sure, try putting a MsgBox at
the beginning of each Sub:

MsgBox "Entering TextBox1_Exit" 'or whichever sub name

By the way, I'm not one of those people who say GoTos are ALWAYS bad, but it
semes to me your first Sub would be simpler in this form:

Private Sub TextBox1_Exit(ByVal Cancel _
As MSForms.ReturnBoolean)
If TextBox1.TextLength >= 9 And _
IsNumeric(TextBox1.Text) Then Exit Sub
TextBox1.SetFocus
MsgBox " Nebolo zadané správne Äíslo! Tu " & _
"možeÅ¡ zadaÅ¥ len 9 miestne Äíslo.", vbCritical, _
" CHYBA ÄŒÃSLA TOVARU"
Cancel = True
End Sub
 
J

jogamen

Hi Bob,

the problem is with code in TextBox2_Enter. If the the code dont find the
requested value it should go to line3, msgbox should appear, move focus to
the textbox1 and exit/stop sub.
 
J

jogamen

Hi Bob,

the problem is with code in TextBox2_Enter. If the the code dont find the
requested value it should go to line3, msgbox should appear, move focus to
the textbox1 and exit/stop sub.
 

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

Top