Understanding Error Handling (1 Viewer)

Local time
Today, 10:39
Joined
May 14, 2020
Messages
32
Hi everyone! I've tried searching the forum here and have been a fairly long time lurker but sadly i'm running into a bit of trouble understanding something i imagine is very basic for all of you experts :(. I've been working on a database for sometime now nearly every day and am trying to add in some error handling in my forms. I understand using the On Error go to and how to display error messages ect and escaping the function if theres an error and have most of it in the correct order after reading through a bunch of posts! Now what i was trying to do was the table that hosts error messages similar to the Allen Browns code . I added the module and it liked everything got through the debug just fine but now i'm trying to call upon it when i encounter an error in my form but when i try to compile i get an error on the line saying "Compile error, expected variable or procedure not module"

Code:
OPSYSErrorExit:
Exit Sub


OPSYSError:
MsgBox Err.Number & Err.Description, vbCritical, "OPSYS Error"
Call LogOPSYSError(Err.Number, Err.Description, "Command21_Click()")
Resume OPSYSErrorExit

Thats a snippet of my code (right at the bottom i've got bits above and my first line is On Error GoTo OPSYSError). Why cant i call on this module for the code that creates a table based on my error? am I doing something wrong? Thanks all any other advice on error handling would be fantastic! Have a lovely day!
 

bob fitz

AWF VIP
Local time
Today, 10:39
Joined
May 23, 2011
Messages
4,719
Is "LogOPSYSError" Declared as Public not Private
 
Local time
Today, 10:39
Joined
May 14, 2020
Messages
32
Code:
Public Function LogOPSYSError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _
    StrCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = True) As Boolean

Public function, created as a module. :)
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:39
Joined
Sep 21, 2011
Messages
14,238
Code:
Public Function LogOPSYSError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _
    StrCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = True) As Boolean

Public function, created as a module. :)
I think the error message is telling you it did not expect the name of the module to be LogOPSYSError ? :unsure:
Just call the module something else like modErrors
 

jdraw

Super Moderator
Staff member
Local time
Today, 05:39
Joined
Jan 23, 2006
Messages
15,379
I agree with Gasman sounds like you provided a module name rather than a function name.
Also when posting function code -- please show the entire code.
 
Local time
Today, 10:39
Joined
May 14, 2020
Messages
32
I agree with Gasman sounds like you provided a module name rather than a function name.
Also when posting function code -- please show the entire code.

Oops sorry about that will make sure i do that in future! Ok so i've changed the module name to something else and its compiling just fine now thanks for that everyone!
 

bob fitz

AWF VIP
Local time
Today, 10:39
Joined
May 23, 2011
Messages
4,719
Oops sorry about that will make sure i do that in future! Ok so i've changed the module name to something else and its compiling just fine now thanks for that everyone!
I also thought that might be the problem which is why I asked the question in post #4
Glad you've got it working now :)
 
Local time
Today, 10:39
Joined
May 14, 2020
Messages
32
I also thought that might be the problem which is why I asked the question in post #4
Glad you've got it working now :)
Sorry i wasnt sure how to tag everyone i meant to set replies to all of you who had replied haha. Ended up writing my message and then forgot to add the extra quotes in so people got notified. Thanks :)
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 10:39
Joined
Jul 9, 2003
Messages
16,274
Also when posting function code -- please show the entire code.

I would just like to add, "FULL MARKS" for using code Tags. Much appreciated. Saves me a lot of hassle going through and adding them later!
 
Local time
Today, 10:39
Joined
May 14, 2020
Messages
32
I would just like to add, "FULL MARKS" for using code Tags. Much appreciated. Saves me a lot of hassle going through and adding them later!
Thanks ! Always makes it easier to read haha, tends to be a nightmare otherwise, its also way easier to see comments, i think i tend to over comment my code but as i've been learning as I go its a massive help when i go back to optimise my code later on to see what my thought process was and especially what certain queries are actually doing ect.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:39
Joined
Sep 21, 2011
Messages
14,238
Thanks ! Always makes it easier to read haha, tends to be a nightmare otherwise, its also way easier to see comments, i think i tend to over comment my code but as i've been learning as I go its a massive help when i go back to optimise my code later on to see what my thought process was and especially what certain queries are actually doing ect.
Don't be having a Command21_Click event then. :)
Give your controls meaningful names.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 10:39
Joined
Jul 9, 2003
Messages
16,274
i think i tend to over comment my code

I wouldn't worry too much, over commenting is much better than the opposite! The other thing developers aspire to do, is to make the names of functions and variables as short as possible, quite rightly so. But when you're developing, there's no reason you shouldn't use long meaningful descriptions for your functions and variables. When you've finished, you can tidy up by using find and replace and changing them to something more professional looking.
 

Isaac

Lifelong Learner
Local time
Today, 02:39
Joined
Mar 14, 2017
Messages
8,777
Thanks ! Always makes it easier to read haha, tends to be a nightmare otherwise, its also way easier to see comments, i think i tend to over comment my code but as i've been learning as I go its a massive help when i go back to optimise my code later on to see what my thought process was and especially what certain queries are actually doing ect.
I agree, it's better to err on the side of more. I can't remember ever having a time where I had to take over or read someone else's code, and I thought to myself "there are too many comments here and it is obstructing me".

Also, personally, I'm a big proponent of using extremely precise and descriptive variable and function/sub names, particularly variables. Both in VB and in SQL, too many coders think that variable or alias names are solely an opportunity to "make it really short". Absolutely not! IMHO. They are a chance to 1) be descriptive so you or someone else doesn't have to continually page back up to the Declarations area to remember what the heck they are for, and 2) get Intellisense or SQL DDL information in order to design code correctly and efficiently.

Most of my variable names are not particularly short, but they are very descriptive. It is incredibly frustrating to read cryptic and meaningless variable names in other people's code..it just requires twice as much looking back and forth to see what the variable is for. If you can make them short fine--Do both, but, I always prefer descriptive over short if they are at odds.
 
Local time
Today, 10:39
Joined
May 14, 2020
Messages
32
I agree, it's better to err on the side of more. I can't remember ever having a time where I had to take over or read someone else's code, and I thought to myself "there are too many comments here and it is obstructing me".

Also, personally, I'm a big proponent of using extremely precise and descriptive variable and function/sub names, particularly variables. Both in VB and in SQL, too many coders think that variable or alias names are solely an opportunity to "make it really short". Absolutely not! IMHO. They are a chance to 1) be descriptive so you or someone else doesn't have to continually page back up to the Declarations area to remember what the heck they are for, and 2) get Intellisense or SQL DDL information in order to design code correctly and efficiently.

Most of my variable names are not particularly short, but they are very descriptive. It is incredibly frustrating to read cryptic and meaningless variable names in other people's code..it just requires twice as much looking back and forth to see what the variable is for. If you can make them short fine--Do both, but, I always prefer descriptive over short if they are at odds.
I'm so glad you think this way with variables to! Whenever i look at examples theres always really short variables with no description on whats being stored and its so hard to understand its use. I always have long variable names, but hey it takes an extra second to type them out but i know EXACTLY what they're there for and what the purpose is of that variable.
Don't be having a Command21_Click event then. :)
Give your controls meaningful names.
Funnily enough this was the only command button i had without a decent name haha its on my latest form in the 'testing' stages still i tend to have a copy of a form that i make to test in (this example) just incase i cause any tasty corruptions when testing my code. Once i'm happy with it i copy my VBA accross into the actual form I plan to use (which has the button name SubmitPCBuildOrderBtn) which is the only change to the code being my error handler :). It's a weird process but in the early days on my access career haha I caused some dodgy form corruption using terrible VBA procedures I think so i work this way not to try and keep my forms clean if that makes sense! :).

Great i just spat coffee on my computer! haha this is definitely me on my earlier coding days when I did not comment my code and had to go back in to debug a problem that pops up a few months later.
 

Users who are viewing this thread

Top Bottom