Building the Cruise Group

The Cruise group contains three controls. A button to book a cruise, a gallery that displays the cruise destinations, and a dropDown that lists the months of the year (although you could use it to display a list of cruises that are sailing in a particular month).

You'll fill both the gallery and dropDown controls using callbacks.

You might wonder why you'd use code to fill the list of months instead of a static list of months in the XML. The reason is so that you can fill the list using the MonthName function in VBA. That function returns the localized name of the month depending on the user interface language for Microsoft Office. Using it enables you to build applications that display well in other languages.

Writing the XML

Here's the XML for the Cruise group:

<group id="grpTravelCruise" label="Cruise">

<button id="btnBookCruise" label="Book Cruise1

' size="large"

image="cruise6.bmp"/>

<gallery id="galCruiseDestinations" label:

="Destinations"

size="large" imageMso="PictureReflectionGallery"

columns="3" rows="3" itemHeight=

"80"

itemWidth="80"

getItemCount="onGetItemCount"

getItemLabel="onGetItemLabel"

getItemImage="onGetItemImage"

getItemID="onGetItemID"

onAction="onSelectDestination">

<button id="btnCruiseDestinations" label=

"All

Cruise Destinations"/>

</gallery>

<dropDown id="ddnFindByMonth" label="Find by Month" getItemCount="onGetItemCount" getItemLabel="onGetItemLabel"> </dropDown> </group>

Gallery controls provide a few additional attributes of which you should be aware. The first are the columns and rows attributes. They define the number of columns and rows in the gallery when it is dropped down. Next are the itemHeight and itemWidth attributes. These define the height and width of images in the gallery.

Filling the gallery is similar to filling the dropDown control that you saw earlier. In addition to the getItemCount, getItemID, and getItemLabel callbacks, you also use the getItemImage callback to fill the images from a table.

A button node is included in the gallery. It will display a single button at the bottom of the gallery control.

Writing the Callbacks

To fill both the gallery and the combo box, add the following callbacks. First, the getItemCount callback. Here, you're returning a hard-coded value of 12 for the number of months for the drop-down while the number of destinations for the gallery comes from a query called qryDestinations.

Public Sub OnGetItemCount(ctl As IRibbonControl, ByRef Count)

Dim strSQL As String

Select Case ctl.id

Case "ddnFindByMonth": Count = 12 ' number of months

Case "galCruiseDestinations":

' Open the recordset for cruises. Use a query because

' you store a relative path to the image in the table.

strSQL = "SELECT * FROM qryDestinations" ' WHERE DestinationType =

= 2"

Set rstDestinations = CurrentDb.OpenRecordset(strSQL)

' return the recordcount

rstDestinations.MoveLast

rstDestinations.MoveFirst

Count = rstDestinations.RecordCount

End Select

End Sub

Next, the getItemLabel callback. Note the use of the MonthName function for the drop-down.

Public Sub onGetItemLabel(ctl As IRibbonControl, Index As Integer, ByRef Label) Select Case ctl.id

Case "ddnFindByMonth"

Label = MonthName(Index + 1) Case "galCruiseDestinations"

' return the label from tblDestinations Label = rstDestinations("Destination") End Select End Sub

Then, the getltemlmage callback. You aren't concerned with images for the drop-down so you only return an image for the gallery.

Public Sub onGetItemImage(ctl As IRibbonControl, Index As Integer, ByRef Image) Select Case ctl.id

Case "galCruiseDestinations" ' return the image

If Dir(rstDestinations("ImagePath") <> "" Then

Set Image = LoadPicture(rstDestinations("ImagePath")) End If End Select End Sub

Finally, return an ID for the gallery using the getItemID callback.

Public Sub onGetItemID(ctl As IRibbonControl, Index As Integer, ByRef id) Dim strID As String

Select Case ctl.id

Case "galCruiseDestinations"

' use the ID field in the control id strID = "qryDestinationsID" & rstDestinations("DestinationID")

' get the next destination rstDestinations.MoveNext

' cleanup when you reach the end If rstDestinations.EOF Then rstDestinations.Close Set rstDestinations = Nothing End If End Select End Sub

When the Ribbon loads, it calls these callbacks to fill the gallery and the drop-down. The drop-down contains the list of months; the gallery is displayed in Figure 12-30.

The image files for this project are included in the download code for this chapter.

Galleries are cool! There's an onAction callback when an item is selected in the gallery. In this case, you'll open a form that is filtered to the item that was clicked. The onAction callback is implemented in the following code:

Public Sub onSelectDestination(ctl As

IRibbonControl, selectedId As String,

selectedlndex As Integer)

Dim lngID As Long

' parse the ID number from the ID

string

lngID = CLng(Replace(selectedId, 1

'qryDestinationsID", ""))

' open the destinations form

DoCmd.OpenForm "frmDestinations",

, , "DestinationID = " & lngID

End Sub

Selecting an item in the gallery opens the form shown in Figure 12-31.

Selecting an item in the gallery opens the form shown in Figure 12-31.

0 0

Post a comment