PDA

Click to See Complete Forum and Search --> : Proper coding of an If Then statement


Gimpster
Nov 3rd, 1999, 05:09 AM
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
corneslen@hotmail.com
ICQ# 47799046

MartinLiss
Nov 3rd, 1999, 05:11 AM
Change the End Sub lines that you added to Exit Sub.

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

MartinLiss
Nov 3rd, 1999, 05:23 AM
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

Gimpster
Nov 3rd, 1999, 05:24 AM
Thanks! Now it works fine, Thank you again.

------------------
Thanks,
Ryan
corneslen@hotmail.com
ICQ# 47799046

Gimpster
Nov 3rd, 1999, 05:30 AM
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
corneslen@hotmail.com
ICQ# 47799046

billwagnon
Nov 3rd, 1999, 08:06 AM
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

Gimpster
Nov 4th, 1999, 05:45 AM
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
corneslen@hotmail.com
ICQ# 47799046

MartinLiss
Nov 4th, 1999, 07:30 AM
There are a lot of ways to do it. Here is one:
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

Gimpster
Nov 4th, 1999, 10:26 PM
Ok, Thank you so much for all your help. I think I get the rest of the code figured out now. Thank you.

------------------
Ryan
corneslen@hotmail.com
ICQ# 47799046