If True Then
Err.Number = 40000
Err.Source = "String indicating where error is occurring"
Err.Description = "Error description string"
Err.Raise Err.Number, Err.Source, Err.Description
End If
Obviously, we''''ll replace the ''''If True Then'''' statement with a more practical condition within our method code, such as whether a database record correctly returned at least one record given the value of the NoteID sent by the calling code.
When an error is caused within VB code, an error message is displayed. The first three of the four lines within the If/Then statement sets three VB variables used to describe a custom error message. Normally, VB sets these variables and sends them back automatically so the calling code can be programmed to handle how the error is to be dealt with. Since we''''re going to be getting information from a database, and it''''s the calling code that''''s setting the values used to select which record we''''re retrieving, we''''ll use the same error-messaging system to let our calling ASP file code know when a database record was not successfully returned.
For now we''''ll set the Err.Number, Err.Source, and Err.Description variables to dummy values and send them back by telling VB that we want it to think a real error has occurred. Setting the three Err variables, and then "raising" an error with the fourth line, accomplishes this. One caveat, since VB uses it''''s own error-number values, we''''ll want to use numbers that won''''t interfere with the VB error-numbering system. At this point in time you can safely use at least a few thousand numbers starting with 40000. The above code essentially sends the flow of code to the line of code (ErrorCode:) indicated in the On Error statement that we used at the start of the method, skipping all other method code. So after the three Err variables are set and the error is raised with the Err.Raise statement, the next line of code that is processed is the code following the OnError statement.
Err.Raise Err.Number, Err.Source, Err.Description
We again use the Err.Raise statement, with its three error descriptors and send back the error messages to the calling code. After the error-handling code is completed, we end the function. This area of code below the ErrorCode and End Function statements, will be used to clean up any open database connections, rollback the transaction, etc.
''''///// CODE IF ERROR OCCURS ////////////////////////////////////
ErrorCode:
''''///// SEND BACK RAISED ERROR
Err.Raise Err.Number, Err.Source, Err.Description
End Function
During your debugging and testing phase, you may want to comment out the "On Error GoTo ErrorCode" statement so that it won''''t divert any "real" errors to the "ErrorCode:" line. If you don''''t comment out the On Error statement, VB will always send you to the ErrorCode statement when tracking down the location of a bug in your method, which is not too informative when debugging any real method errors you may initially introduce.
Accessing the Database Tables from within the MethodLet''''s put away the error code for the moment so it won''''t clutter up our next important method code topic -- database access.
First, for any code which instantiates the ADODB object, you''''ll need to tell VB that you want to reference the ADODB object. This is done by going to the Project/References VB menu selection and checking the "Microsoft ActiveX Data Objects 2.0 Library" reference, although you''''ll probably have one with a higher version number.
Now, VB will recognize ADO objects because we referenced it and we can connect to the example NoteDb database. I recommend using ODBC to establish a data-source name for use as a database-connection string.
To establish a Data Source Name (DSN) with ODBC, select the "ODBC Data Sources" icon in the Windows Control Panel. Select the "System DSN" tab on the "ODBC Data Source Administrator" and then click the "Add..." button. The driver you want should be at, or near, the top of the driver list that will be displayed. Highlight the "Microsoft Access Driver (*.mdb)" driver and click the "Finish" button. The "OBDC Microsoft Access Setup" window will appear. In the "Data Source Name" input field, enter the DSN used in this article''''s example code, which is"NotesDSN." Then click the "Select" button and use the directory tree to drill down to the directory that stores this article''''s example Access database file. The file NoteDb.mdb will be displayed in the left window when its directory is selected in the directory tree. Select the example NoteDb.mdb file and click the "OK" button. Click "OK" twice more to exit from the ODBC Data Source Administrator. The "NotesDSN" data source name can now be used on your system via ActiveX Data Objects (ADO) from VB code.
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




