Access Cookbook - Ken Getz [315]
GoTo ExitHere
Else
Dim app As Access.Application
Set app = cb.Application
End If
The code then branches based on VerbID. If the VerbID is 1, then the code sets a reference to the Application object's CurrentDb property to gain access to DAO objects. This allows the code to execute a query to obtain the total number of orders for a given CustomerID. This is then retrieved into a DAO Recordset, and passed to the Customers form as an OpenArgs argument:
If VerbID = 1 Then
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = app.CurrentDb
Dim strQry As String
Dim strOrders As String
strQry = "SELECT Count(*) AS NumOrders FROM Orders WHERE CustomerID='" _
& cb.Value & "';"
Set rs = db.OpenRecordset(strQry)
If Not rs.EOF Then
strOrders = "Total number of orders: " & rs!NumOrders
Else
strOrders = "No orders for this customer"
End If
rs.Close
app.DoCmd.OpenForm "Customers", _
WhereCondition:="[CustomerID] = '" & cb.Value & "'", _
OpenArgs:=strOrders
If the second action is chosen, then the code opens rptCustomers report, passing the CustomerID as the WhereCondition argument (without this WhereCondition, the report would open displaying all the customers):
ElseIf VerbID = 2 Then
app.DoCmd.OpenReport "rptCustomers", _
View:=acViewPreview, _
WhereCondition:="[CustomerID] = '" & cb.Value & "'"
End If
The error handling code is mainly useful for debugging. It displays any error information in a MsgBox statement:
ExitHere:
Exit Sub
HandleErr:
MsgBox Err.Number & " " & Err.Description, _
vbCritical, "Error in AccessSmartTag.ISmartTagAction2_InvokeVerb2"
Resume ExitHere
End Sub
Compiling and registering the DLL project
Once you've written the code, build the DLL project by choosing File → Make AccessSmartTag DLL from the menu. This will create the correct registry entries. Launch regedit from the Windows Start → Run menu. To obtain the CLSID for the action handler, navigate to the following node in the Registry:
HKEY_CLASSES_ROOT\AccessSmartTag.stActions\Clsid
Double-click the Clsid node to obtain the value, as shown in Figure 16-12. Copy it to the clipboard and close the regedit window without saving.
Figure 16-12. Obtaining the Clsid from the AccessSmartTag.stActions
You can then edit the registry directly or create a reg file to update the registry entries. Use Notepad to create a new file and name it Reg_AccessSmartTag.reg. The file should contain the following text. However, you will need to replace the value shown in the curly braces with the value that you copied to the Clipboard from the Registry in the previous step:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Actions\
{9F7503BB-4BBA-4A4A-B1A5-A0DF0A0187F5}]
In case you ever need to unregister the smart tag, create a second file named Unreg_AccessSmartTag.reg. The file should contain the following text. Again, replace the value shown here in the curly braces with the value copied to the Clipboard:
Windows Registry Editor Version 5.00
[-HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Actions\
{9F7503BB-4BBA-4A4A-B1A5-A0DF0A0187F5}]
Save both files and double-click Reg_AccessSmartTag.reg. This will create the entries in the registry so that Access can recognize the smart tag actions.
Open the 16-07.MDB sample database and open the Customers form in design view. View the code in the form's Open event, which displays anything passed in the OpenArgs event in the form's Caption property:
Private Sub Form_Open(Cancel As Integer)
' Display any OpenArgs in the Caption
Dim str As String
str = Me.OpenArgs & ""
If Len(str) > 0 Then
Me.Caption = str
End If
End Sub
Close the form and open the Customers table in design view. Assign the smart tag to the CustomerID field, as shown in Figure 16-13.
Figure 16-13. Assigning the smart tag to the CustomerID field in the Customers table
Save the table and view it in datasheet view. When you choose the first smart tag action, the Customers form will open with the total