Results 1 to 9 of 9

Thread: Proper coding of an If Then statement

  1. #1

    Thread Starter
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    Ok, what I'm trying to do, is create an error handling routine. What I want the program to do, is check to see if any of four text boxes are empty, and if one is, then it displays the appropriate error message. Then once it is done, IF AND ONLY IF it had found any errors, then I want it to end the sub. However, I do not know how to code this. I have included the code that I have tried already, but this gives me an error message on the "End Sub" line in the If Then statement, whether the statement is true or not. Please help me figure out how I can do this. (The dsnerror, uiderror, and pwderror, are other commands run from a module)

    Private Sub cmdContinue_Click()
    dbname = txtdbname.Text
    dsn = txtdsn.Text
    uid = txtuid.Text
    pwd = txtpwd.Text

    If txtdbname.Text = "" Then
    MsgBox ("Please enter a name for the database you wish to connect to."), , "Error: database name not entered"
    dsnerror
    uiderror
    pwderror
    End Sub
    End If

    If txtdsn.Text = "" Then
    MsgBox ("Please enter a data source name for the database you wish to connect to."), , "Error: dsn not entered"
    uiderror
    pwderror
    End Sub
    End If

    If txtuid.Text = "" Then
    MsgBox ("Please enter a valid username for the database you wish to connect to."), , "Error: No username entered"
    pwderror
    End Sub
    End If

    If txtpwd.Text = "" Then
    MsgBox ("Please enter a valid password for the database you wish to connect to."), , "Error: No password entered"
    End Sub
    End If

    On Error GoTo dberror
    X = "ODBC;database=" & dbname & ";DSN=" & dsn & ";UID=" & uid & ";PWD=" & pwd
    Set db = OpenDatabase("", False, False, X)

    dberror
    MsgBox ("Some of the information you entered was invalid, please re-enter the information and make sure that it is correct."), , "Error: Invalid Information Entered"
    End Sub

    Load frmMain
    frmStartup.Hide
    frmMain.Show
    Unload frmStartup
    End Sub


    ------------------
    Thanks,
    Ryan
    [email protected]
    ICQ# 47799046

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Change the End Sub lines that you added to Exit Sub.

    ------------------
    Marty

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Here are a few other things:
    1) It will be annoying to the user of your program to correct one error and then find out he/she has a second, third and fourth error. You should probably check for all invalid conditions and then create one message that will list everything that is wrong. You can also do such things as changing label caption (or other things) forcolors to red when a particular item is in error to help the user locate it.
    2) While it won't hurt anything you don't need to both Load and Show a form. As soon as you Show it, it will automatically be loaded.
    3) You also don't need to both Hide and Unload a form. Either one will remove it from view. Use Hide when you want to be able to quickly redisplay it, and Unload when it won't be needed again.
    4) You need an Exit Sub between Set db = OpenDatabase("", False, False, X)anddberror. Otherwise the code will "fall through" to and execute the error code even when the db call is OK.



    ------------------
    Marty

  4. #4

    Thread Starter
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    Thanks! Now it works fine, Thank you again.

    ------------------
    Thanks,
    Ryan
    [email protected]
    ICQ# 47799046

  5. #5

    Thread Starter
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    I originally wanted to be able to create one single message that would list all applicable errors in the text boxes, but I couldn't figure out how to do that, could you give me some examples?

    ------------------
    Thanks,
    Ryan
    [email protected]
    ICQ# 47799046

  6. #6
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290

    Post

    dim strErrorMsg

    'check for error 1
    if error 1 = true then strerrormsg=strerrormsg & "#1 is bad" & vbcrlf

    and just keep checking for errors and adding them on

    'then display strErrorMsg
    msgbox strErrorMsg

  7. #7

    Thread Starter
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    Ok, so then if I have a strerrormsg that equals "#1 is bad #2 is bad #3 is bad" then how would I convert that to an error message. I don't understand how you can get each message that is accociated with the respective error number to be displayed in a MsgBox when you have multiple errors reported. Please help. I really want to get this to work. Thanks.

    ------------------
    Ryan
    [email protected]
    ICQ# 47799046

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    There are a lot of ways to do it. Here is one:
    Code:
        Dim bErrorFound As Boolean
        Dim sMsg As String
        
        sMsg = "The following data is missing or incorrect" _
             & vbCrLf
             
        If Text1.Text <> "Correct answer for Text1" Then
            bErrorFound = True
            sMsg = sMsg & vbCrLf & "Text1 is invalid"
        End If
        
        If Text2.Text <> "Correct answer for Text2" Then
            bErrorFound = True
            sMsg = sMsg & vbCrLf & "Text2 is invalid"
        End If
        
        If Text3.Text <> "Correct answer for Text3" Then
            bErrorFound = True
            sMsg = sMsg & vbCrLf & "Text3 is invalid"
        End If
        
        If bErrorFound Then
            MsgBox sMsg, vbOKOnly, "Validation Errors"
        End If
    ------------------
    Marty

  9. #9

    Thread Starter
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    Ok, Thank you so much for all your help. I think I get the rest of the code figured out now. Thank you.

    ------------------
    Ryan
    [email protected]
    ICQ# 47799046

Posting Permissions

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



Click Here to Expand Forum to Full Width