
Microsoft Year 2000 Readiness Disclosure & Resource Center |
 |
 |
 |
Building Year-2000 Compliant Applications with Visual Studio and Windows DNA |
 |
7. Listing 8 – The Excel VBA code for client-side validation
The Business Component: Visual Basic
Our VISUAL BASIC business component is invoked through a call to either its AddByDate method or AddByDay method. Both of these methods add a record to the database. The AddByDate method is used when the user entered the actual due date for the receivable, whereas the AddbyDay method is used if the user just selected a number of days from the Terms. Listing 9 contains the code for both of these functions.
The VISUAL BASIC component validates the data passed to it from its consumer (either the ASP page or the Excel client). Because this component represents a "business component", it validates all parameters, regardless of whether or not they were previously validated. The business component should function as a "black box", meaning that it can be called from any type of client and should not assume that client side validations were performed.
Public Sub AddByDays(ByVal Customer As String, ByVal TransactionDate As Date, ByVal Amount As Single, ByVal Terms As Integer) On Error GoTo Receivable_EH Dim Cmd As adodb.Command Dim dtDueDate As Date Dim DateMath As Y2KSAMPLELib.DateMath Set DateMath = CreateObject("DateMath.DateMath.1") dtDueDate = DateMath.DateAddX(Terms, TransactionDate) Set DateMath = Nothing 'Check some business rules... Call CheckBusinessRules(Customer, TransactionDate, Amount, dtDueDate, "AddByDays") Set Cmd = CreateObject("ADODB.Command") Cmd.ActiveConnection = CONNECT_STRING Cmd.CommandType = adCmdStoredProc Cmd.CommandText = "y2k_addReceivable" Cmd.Parameters("@customer").Value = Customer Cmd.Parameters("@date").Value = TransactionDate Cmd.Parameters("@amount").Value = Amount Cmd.Parameters("@due_date").Value = dtDueDate Cmd.Execute Set Cmd = Nothing If Not ctxContext Is Nothing Then Call ctxContext.SetComplete End If Exit Sub Receivable_EH: Set Cmd = Nothing If Not ctxContext Is Nothing Then Call ctxContext.SetAbort End If Call RaiseError("AddByDays", Err.Number, Err.Description) End Sub Public Sub AddByDate(ByVal Customer As String, ByVal TransactionDate As Date, ByVal Amount As Single, ByVal DueDate As Date) On Error GoTo Receivable_EH Dim Cmd As adodb.Command 'Check some business rules... Call CheckBusinessRules(Customer, TransactionDate, Amount, DueDate, "AddByDate") Set Cmd = CreateObject("ADODB.Command") Cmd.ActiveConnection = CONNECT_STRING Cmd.CommandType = adCmdStoredProc Cmd.CommandText = "y2k_addReceivable" Cmd.Parameters("@customer").Value = Customer Cmd.Parameters("@date").Value = TransactionDate Cmd.Parameters("@amount").Value = Amount Cmd.Parameters("@due_date").Value = DueDate Cmd.Execute Set Cmd = Nothing If Not ctxContext Is Nothing Then Call ctxContext.SetComplete End If Exit Sub Receivable_EH: Set Cmd = Nothing If Not ctxContext Is Nothing Then Call ctxContext.SetAbort End If Call RaiseError("AddByDate", Err.Number, Err.Description) End Sub
Listing 9 - Functions in the VISUAL BASIC component to add records to the database.
Continuing with our philosophy, we are using the Date data type in VISUAL BASIC for the Transaction Date and the Due Date. This is very convenient because in Visual Basic, a run-time error will be raised if we attempt to pass non-date values to either of these parameters.
In addition, VISUAL BASIC has straight forward date math functions such as DateAdd for adding dates and DateDiff for determining the difference between two date. For comparing dates, we can just use the same comparison operators we would use for numeric data ("<", "<=", ">", ">=", etc.). As long as both of the data types we are comparing are date data types, VISUAL BASIC will accurately compare the two values. However, using these operators with values that are not valid dates will trigger a run-time error.
Listing 10 shows the VISUAL BASIC code used in the function CheckBusinessRules to verify that the Due Date is not earlier than the Transaction Date.
'The Due Date cannot be earlier than the transaction date. If DueDate < TransactionDate Then Call RaiseError(ErrorSource:=Source, _ ErrorNumber:=vbObjectError + 1002, _ ErrorDescription:="Due Date cannot be earlier than Transaction Date.") End If
Listing 10 - Using comparison operators to compare two dates in Visual Basic.
Note that the Date data type also stores time information. So, use caution when comparing dates in VISUAL BASIC using the comparison operators, because if your date variables also store times (even unknowingly), the times will be compared as well.
For example, the Debug.Print statement in Listing 11 will very likely always return false. This is because d2 is assigned the value of the Now function, which returns the current date and time, whereas d1 is assigned the value of just the date. When the time is not specified in a Date data type, it defaults to 12:00:00 AM. Unless the Now function was also executed at this exact time, the Debug.Print will return false.
Thus, if you are only interested in working with a date (as opposed to a more precise date/time), make sure to not unknowingly give it time precision as well.
Dim d1 As Date Dim d2 As Date d1 = Date d2 = Now Debug.Print (d1 = d2) 'will return false, except if run at exactly midnight.
Listing 11 - Comparison operators can be deceiving.
If you are working in date/time precision, but need to just compare dates, consider using the DateDiff function. DateDiff can be used to find the number of days that occur between two dates. Using DateDiff, Listing 11 can be re-written as Listing 12. Since DateDiff is used with the "d" parameter, only the day portion of the two dates are compared.
Dim d1 As Date Dim d2 As Date d1 = Date d2 = Now Debug.Print DateDiff("d", d1, d2) 'returns 0.
|
|
|