PDA

View Full Version : having trouble doing error trapping in VB


singularity2006
11-20-2003, 05:12 AM
Aighty day, so I have a VB6 form with a bunch of entry fields and an output picture box for the entry fields to be printed to. How do I set it such that if they have incomplete entry fields (text boxes), that they get an error message and the program does not continue to the next sub program to print the text fields? I got the error message to work just fine using:

If txtFirstName.Text=" " Then
msgBox "Sorry, you must enter your first name." , , "Input Error!"
Else
Call ProcessData

However, even if there is an error, after it comes up, the program continues running to do the rest of the program to print into the picture box. Any ideas? I was trying to do a do while loop or until loop but I wasn't all too sure how to approach that. Any other ideas?

OC-NightHawk
11-20-2003, 06:00 AM
On error resume next

It tells the sub routine or function that you'll handle the error handleing.

You'll have to use error checking though. For instance for your picture box it would be a good idea to check and make sure there is no error before loading it.

singularity2006
11-20-2003, 08:17 AM
On Error Resume Next? Ehr... hahaha. Not exactly sure how to implement that. We haven't gotten there yet. And actually, I should make a correction, this is a field validation thing, not error trapping. I had no idea they meant two totally different things. In anycase, here is what I have so far:

Private Sub cmdProcessData_Click()
If txtInput1.Text="" Then
MsgBox "You have left a field blank", ,"Input Error!"
Exit Sub
EndIf
End Sub

However, the actual code block is pretty long, about 5x longer than this because there are 5 input fields. It works great actually, so the next issue is compressing it down! It's too large. I originally had a separate sub to handle the blank fields but it got strange because I couldn't figure out how to make it perform the Exit Sub without continuing on the rest of the program to print the display and record the data to file. And I might add, there is also a Sub cmdProcessData2_Click() so that above is in there twice; that's why having a separate sub to handle the field validation would be nice. But yeah, OC, did you ever get the email I sent you? If you did, erase it. I totally changed the program around so it's not anywhere near the same anymore. Lots have changed.

OC-NightHawk
11-20-2003, 06:01 PM
Originally posted by singularity2006
On Error Resume Next? Ehr... hahaha. Not exactly sure how to implement that. We haven't gotten there yet. And actually, I should make a correction, this is a field validation thing, not error trapping. I had no idea they meant two totally different things. In anycase, here is what I have so far:

Private Sub cmdProcessData_Click()
If txtInput1.Text="" Then
MsgBox "You have left a field blank", ,"Input Error!"
Exit Sub
EndIf
End Sub

However, the actual code block is pretty long, about 5x longer than this because there are 5 input fields. It works great actually, so the next issue is compressing it down! It's too large. I originally had a separate sub to handle the blank fields but it got strange because I couldn't figure out how to make it perform the Exit Sub without continuing on the rest of the program to print the display and record the data to file. And I might add, there is also a Sub cmdProcessData2_Click() so that above is in there twice; that's why having a separate sub to handle the field validation would be nice. But yeah, OC, did you ever get the email I sent you? If you did, erase it. I totally changed the program around so it's not anywhere near the same anymore. Lots have changed.

No sorry I don't recall getting the email. I probably deleted by mistake when I was deleting all the spam. :rolleyes: I'm so sick of the make *insert body part* *select bigger or smaller* emails. We need a national don't email list like the phone calls.

if you want a field validation function you need to decide what is illegal and what isn't. Will numeric digits be allowed, spaces? Once you know what you don't want to have in the string then make a fucntion similar to this.


public function funcCheckText(txtField as string) as boolean

funcCheckText = true

'insert your string function here. Left or mid or InStr should do the trick

if err.number<>0 then
funcCheckText=false
end if

end function

singularity2006
11-22-2003, 12:37 AM
yup, I tried that. Here's the problem I'm running into:

Private Sub cmdCheckOut
Call ErrorChecking
Call CheckOut
End Sub

Private Sub ErrorChecking
If txtFirstNameInput.Text = "" Then
MsgBox "Sorry, you must enter your first name." , , "Input Error!"
End Sub
End If
End Sub

My problem is that as cmdCheckOut calls ErrorChecking, and if ErrorChecking finds errors and stops, the remainder of cmdCheckOut still continues to run. The only thing I could think of to correct this was to just include Call CheckOut as an "Else" statement in order to get things to work correctly. However, I have two buttons, one for check in and one for check out. This means that each one must have the field validation routine. I was hoping to have the routine in its own sub as opposed to in both cmdCheckIn and cmdCheckOut.

OC-NightHawk
11-22-2003, 10:28 PM
Originally posted by singularity2006
yup, I tried that. Here's the problem I'm running into:

Private Sub cmdCheckOut
Call ErrorChecking
Call CheckOut
End Sub

Private Sub ErrorChecking
If txtFirstNameInput.Text = "" Then
MsgBox "Sorry, you must enter your first name." , , "Input Error!"
End Sub
End If
End Sub

My problem is that as cmdCheckOut calls ErrorChecking, and if ErrorChecking finds errors and stops, the remainder of cmdCheckOut still continues to run. The only thing I could think of to correct this was to just include Call CheckOut as an "Else" statement in order to get things to work correctly. However, I have two buttons, one for check in and one for check out. This means that each one must have the field validation routine. I was hoping to have the routine in its own sub as opposed to in both cmdCheckIn and cmdCheckOut.

Oh ok. Thats a simple fix. Error checking needs to be a function so that the second part of checkout is only run based on the outcome of error checking. Heres the basic concept. Also End subs, exits, and gotos are considered sloppy coding. They lead to having spaghetti code.


Dim result As Long

Private Sub cmdCheckOut
result = ErrorChecking()
if result=False then
Call CheckOut
end if
End Sub

Private function ErrorChecking() as boolean
ErrorChecking=False
If txtFirstNameInput.Text = "" Then
MsgBox "Sorry, you must enter your first name." , , "Input Error!"
ErrorChecking=True
End If
End Sub

Edit: about the email was it actual email or a private message? If its a private message how do I get to it?

singularity2006
11-23-2003, 01:09 AM
ah wow, that's definitely cool! Thanks for the info! I had no idea you could manipulate the boolean test like that. Man, that is awesome! I'm definitely gonna program that in, super cool, much thanks!

Anyhow, I think it was an email. But it was sent through the CGTalk server so I'm not sure how that all works out. Anyhow, to access private messages, just click on the link at the top of the CGTalk menu that says "Profile" and somewhere in there is a thing for messages.

And to continue, there's also another thing I've run into problems with. I have two subs that print out stuff to screen. They print exactly the same thing except for the last few lines that say: "equipment checked out" and "equipment checked in"

So currently cmdCheckOut_Click calls a bunch of stuff, error checking, etc etc etc. then continues to CheckOut(). But if cmdCheckIn_Click is clicked, it goes to CheckIn() instead. However, the only difference is that CheckOut() prints, among other things, "Equipment Checked Out." CheckIn() prints, "Equipment Checked In." Is there a way to combine the two subs that do the printing, CheckIn() and CheckOut() so I don't have to have the redundant code in each yet have the distinction of printing "item checked out" or "item checked in?"

<---edit--->

Actually, scratch that. I find a way of doing it, though it's not as elegant as I want. I'll send you some samples when I have it programmed.

singularity2006
11-24-2003, 08:53 PM
I finished for the most part, what needs to be done in the program. I'm gonna go through a few more times over to clean up code and document the thing. But that won't happen anytime soon. If you could give me some input on what I should change, that'd be good. Here's a zip file for the source code and all (no documentation yet). If anyone could make any recommendations about cleaning it up, that'd be good, thanks.

http://staff.esuhsd.org/~ih-govt/drop_box/TermProject.zip
http://staff.esuhsd.org/~ih-govt/drop_box/roster.exe (here's the compiled executable).

And by the way, I've removed the majority of the original input fields. They are automatically filled upon login. This program will actually be used by my students so by entering their password, which is pre-programmed in, it will auto fill the necessary data. The login password is "begin."

And guys, go easy! :thumbsup: This is only week 8 of my first programming class. I wonder if I'll be ready to take on C in a quarter or two. -.-"

OC-NightHawk
11-28-2003, 05:18 AM
Looks good so far. One suggestion in regards to the passwords. Instead of hardwiring them in why not make a simple text file in notpad and type in a list of passwords with a carriage return seperating them? Then you can have the program read the password list and assign the strings into the password array you have set up. That way down the road you could even have it set up easily to allow them to create their own passwords on the fly. You may also want to look into the binary write format for the password file to make it a little more difficult to crack. If you still have time these little things would really make your project that much better.

singularity2006
11-28-2003, 10:31 PM
yeah, i was considering that. Most definitely. But that issue w/ security of the file itself was what bothered me. Can you explain what a binary write format is and how it works?

OC-NightHawk
12-01-2003, 03:52 AM
Originally posted by singularity2006
yeah, i was considering that. Most definitely. But that issue w/ security of the file itself was what bothered me. Can you explain what a binary write format is and how it works?

To use a binary file you type in

Open FileName for Binary as # FileNum

The difference with this is that its random access so you can now read and write to the file.

You'll want to strip out any characters with an ASCII code greater than 127 because it doesn't give a hoot for special characters.

Other than that its just like before.

singularity2006
12-01-2003, 07:23 AM
wh0a, that's pretty cool. So the output file is just a bunch of jibberish if someone tried to read it?

OC-NightHawk
12-01-2003, 04:33 PM
Yeah, the file will not be readable for people. However using straight binary can be cracked somewhat easily if someone knew how to do it. To make it slightly more diffrucult to be cracked you could create a cryptogram object set.

A Sub routine that takes all 126 ASCII characters and swaps the values around before the file is written. Then another sub that uses those values and sets them to where they should be for the program to work right before the file is read. You could also make it even harder to crack by having a set number of sets of keys and adding a number to the start of the file corrosponding to which key to use.

singularity2006
12-01-2003, 05:05 PM
dang, that sounds fun. I was actually tempted to write my own encryption script to mess around with coding and decoding strings of text. Hah, I might just do that right now. I'm @ work @ the moment and don't have my memory key w/ my term project on it.

OC-NightHawk
12-01-2003, 08:29 PM
Work?:surprised I'm on winter break between quarters at college. I have no car, so I have no way to get to a job. So I'm in for a very crappy winter break.

I figure if I'm stuck home for winter I might as well get my Final 2 project under way since I already know what I'm going to do with it. So by the time class starts I can trickle in the work while working on new stuff. That way if I run into a day or two maybe a week where I need to do nothing and just chill I'll still have something to show. :beer: I'm busy cooking up some models for 4 street racing cars, a track, and props. I hope to get the content done before class starts back up and use 8 of the ten weeks to upgrade the graphics engine to use normal maps and shaders. When its done I want something that almost looks prerendered but in real time.I don't know how close I'll get to the goal but I shooting for it. I guess the extra 5 weeks won't hurt.:thumbsup:

draconix
12-04-2003, 03:13 PM
@OC-NightHawk - You attend SCAD?

Also - the link to the project is broken. I'd love to take a look at what you've been working on. Or is there no longer a need?

OC-NightHawk
12-05-2003, 11:37 PM
Originally posted by draconix
@OC-NightHawk - You attend SCAD?

Also - the link to the project is broken. I'd love to take a look at what you've been working on. Or is there no longer a need?

The link is broken? shoot I deleted the file off my macine so I can't offer it, but I'm sure he can get it working. It was a nice basic database application. It shows promise. :cool:

Yeah I go to SCAD. Sometimes I ask myself why I decided to go to an art school when its so hard not to think all logical. Don't go there to learn to program.....because they don't offer anything that goes beyond interfacing with certain renderers or flash and director. But I like the 3D modeling, even if I need work a bit harder at it then the seemingly naturals. Of course I'll have my time in the sun when I whip out my Final 2 project using normal mapping and cg shaders in real time with a crisp V-Synched refresh rate. Then the kicker will be when they ask what engine I used only to hear that its all from scratch. :beer:

singularity2006
12-06-2003, 04:17 PM
What's SCAD stand for?

Anyhow, yeah, I took down the project for major revisions. The code has changed a fair bit since the last post. I accidentally screwed up the whole program in a variable renaming thing. I tried mass renaming many of my variables and the whole thing kinda got trashed. I had to rewrite it from scratch. But in anycase, the error checking and logging functions work great now. I'm gonna finish documenting the thing and post it up for u guys. Thanks again for all your help. :beer:

OC-NightHawk
12-06-2003, 07:39 PM
Originally posted by singularity2006
What's SCAD stand for?

Anyhow, yeah, I took down the project for major revisions. The code has changed a fair bit since the last post. I accidentally screwed up the whole program in a variable renaming thing. I tried mass renaming many of my variables and the whole thing kinda got trashed. I had to rewrite it from scratch. But in anycase, the error checking and logging functions work great now. I'm gonna finish documenting the thing and post it up for u guys. Thanks again for all your help. :beer:

SCAD stands for Savannah College of Art and Design.

As for the help, it was nothing. All you have to do in return is help some one else out if they need it.:beer:

CGTalk Moderation
01-16-2006, 05:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.