Jump to content

Command if and else

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Dunja

Dunja

    Newbie

  • Members
  • Pip
  • 4 posts
i'm trying to do next:
the program should go to the beginning when there is an error. but it just goes to the next form.....

x = Text1.Text
y = Text2.Text
z = Text3.Text

If Text1.Text = "" Then MsgBox ("Ostavili ste prazno polje x")
If Text2.Text = "" Then MsgBox ("Ostavili ste prazno polje y")
If Text3.Text = "" Then MsgBox ("Ostavili ste prazno polje z")


Form1.Hide
Form2.Show

what shold i type between?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It looks like you need to use the if then elseif structure.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
o0TheNerd0o

o0TheNerd0o

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
Are you asking how to start it all over without showing the next form? If so, construct your if/then statements with one more line of code. The one-lined if/then statements are bad programming style. You should always have one line for "If..." and line(s) for statements, and finally a "End If" line. Like this:

If Text1.Text = "" Then 
    MsgBox "Ostavili ste prazno polje x"
    Exit Sub
End If

Option Explicit
:cool:

#4
Dunja

Dunja

    Newbie

  • Members
  • Pip
  • 4 posts
yes. tnx.. it's just End sub, not Exit sub :p

:)

#5
o0TheNerd0o

o0TheNerd0o

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
You could you "End Sub", but "Exit Sub" does the same thing. I think the theory behind which one to use is: "End Sub" should end the entire sub procedure block. The end to the "Private Sub...". The "Exit Sub" should be used within the sub procedure as an escape method to stop executing code from that line on, until the actual "End Sub" line. Does this make any sense? lol!

But, in most cases, either one will work just fine :D
Option Explicit
:cool:

#6
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Well the actually the Exit Sub is mostly used for error reports, what I mean is when you have a piece of code and if it generates an error you make a 'tag' at the end so if this error happens during execution goto error1, if that error happens goto error2, so Exit Sub is used when you have for example this code:

Private Sub Command1_Click()
On Error GoTo Error

text1.Text = "hi"

Exit Sub

Error:
MsgBox "Error"
End Sub

This code will generate an error IF on form 1 there are no textboxes, but if there was a textbox the Error tag will not be executed (because there is Exit Sub before it) but if there was no Exit Sub, the Error tag will be executed as well, so it will generate an error message even when there were none! And you cannot make code after End Sub as it will give an error as wrong VB Code but you can after Exit Sub So that is one of the main purposes of Exit Sub.