473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Making error codes/ messages

Hi i was reading one of ya'lls thread and thought I might be able to use the first code to do what I need. However, I need it to get the user to click the save command button (i.e. Command165). Once all Errs are corrected then that same save button will save the record. These are the 2 codes I have but I am missing somthing.I don't know much about coding so if you could brake it down for me pleas.


Expand|Select|Wrap|Line Numbers
  1. Private Sub Command165_Click()
  2. On Error GoTo Err_Command165_Click
  3.  
  4.  
  5. DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  6.  
  7. Exit_Command165_Click:
  8. Exit Sub
  9.  
  10. Err_Command165_Click:
  11. MsgBox Err.Description
  12. Resume Exit_Command165_Click
  13.  
  14. End Sub
  15.  
  16. __________________________________________________ ___
  17. Private Sub Command165_Click()
  18. Dim strError
  19. strError = ""
  20.  
  21. If IsNull(Social_Security_Number.Value) Then strError = strError & vbNewLine & Social_Security_Number
  22. If IsNull(LastName.Value) Then strError = strError & vbNewLine & LastName
  23. If IsNull(FirstName.Value) Then strError = strError & vbNewLine & FirstName
  24. If Len(strError) <> 0 Then
  25. 'Errors encountered
  26. strError = "The following fields are blank:" & vbNewLine & _
  27. strError & vbNewLine _
  28. "Please fill in these fields before submitting"
  29. End If
  30. End Sub
Sep 13 '10 #1
6 1481
Oralloy
988 Expert 512MB
You can use the Err.Raise statement. (Actually, it's a subroutine call, but who's being picky).

You can find full details in the documentation. Here's an example from some of my code:
Expand|Select|Wrap|Line Numbers
  1.     Err.Raise (0 + VBA.Constants.vbObjectError), moduleName, ("Error: No default for field " & fieldName)
Luck!
Sep 13 '10 #2
I don't understand. Like I said I am new to codes. Where do I put this and what does it mean?
Sep 14 '10 #3
Oralloy
988 Expert 512MB
Put it, or something like it wherever you've discovered an error that your code can't deal with.

What it does is raise an error when a problem is detected.

Then, callers can catch that error with an On Error statement.

Does that make sense?
Sep 14 '10 #4
I think so. I will try it. Thank you
Sep 15 '10 #5
Oralloy
988 Expert 512MB
If they don't use On Error, then the message is displayed at the application level. But that's the application programmers' responsibility. Your library is reporting the error in a responsible way.

Luck!
Sep 15 '10 #6
TheSmileyCoder
2,322 Expert Mod 2GB
A couple of things:
1) This is an access question and you should post it there. (Access VBA also belongs in that forum)
2) You should ALWAYS name your buttons, before creating code for them. A good name could be cmd_Save (cmd short for command button) or btn_Save (what I personally use)
3) You should use the code tags arond your code:[code]Code goes here[/code] to increase the readability of it, thats what I did below.
4) You post 2 pieces of code with the same procedure name. That is bound to give errors in your application.
5) When doing string concatanation, you must enclose your string literals in double quotes "my string", whereas references to string VARIABLES should not have quotes around them.

Now, to the question at hand. How to inform the user that he is missing some information. Lets presume we have named the button btn_Save, then it could look something like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_Save_Click()
  2. 'Check that required info is provided and if so, save record
  3. On Error GoTo Err_btn_Save_Click
  4.  
  5. 'Check info
  6.     Dim strError as string
  7.     strError=""
  8.     If IsNull(Social_Security_Number.Value) Then strError = strError & vbNewLine & "Social Security Number"
  9.     If IsNull(FirstName.Value) Then strError = strError & vbNewLine & "FirstName"  
  10.     If IsNull(LastName.Value) Then strError = strError & vbNewLine & LastName
  11.  
  12. 'If errors were found, inform user and exit.
  13.     If strError & ""="" then
  14.         Msgbox "Please fill in these fields before submitting:" & strError,vbOkOnly,"Missing Info"
  15.         GoTo Exit_btn_Save_Click
  16.     End If
  17.  
  18. 'Save the record
  19.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  20.  
  21. Exit_btn_Save_Click:
  22.     Exit Sub
  23.  
  24. Err_btn_Save_Click:
  25.     MsgBox Err.Description
  26.     Resume Exit_Command165_Click
  27.  
  28. End Sub
Sep 16 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Shane Groff | last post by:
I know this is a recurring discussion (I've spent the last 3 days reading through threads on the topic), but I feel compelled to start it up again. After reading through the existing threads, I...
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
7
by: Steve Jorgensen | last post by:
Here is some code to generate code for raising and getting information about custom errors in an application. Executing the GenerateXyzErrDefs procedure generates the code. <tblXyzError>...
2
by: Roy | last post by:
Hey all, I wish to implement a "Try..." block into my web app (which is basically an editable datagrid) to display appropriate error pages to my users. Basically, the two main error's are that the...
3
by: Adam Bukla | last post by:
Hi! I started programming PostgreSQL database with libpq API (client side). It looks very easy, but unexpected I found problem, which I cannot solve myself. If function PQexec fails, I can get...
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
8
by: PW | last post by:
Hi, There is code in Alison Balter's excellant "Mastering Access 2003" to create a list of error codes and descriptions but it only generates error messages 3 through 94. Is there a website...
3
by: Bobby | last post by:
Hi I'm using Access 2003 with SQL server 2000, linked via ODBC. Can anybody tell me how to capture SQL error codes in Access? If this is not possible, is there any way I can simply turn off SQL...
13
by: mike3 | last post by:
Hi. (crossposted because the program is in C++ and some C++-related elements are discussed, hence comp.lang.c++, plus general program design questions are asked, hence comp.programming.) I'm...
5
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.