The first two pages contain the "housekeeping" Access Basic subroutines involved with the Selected Houses form.

'------------------------
' (declarations) section
'------------------------
Option Compare Database   'Use database order for string comparisons
Dim wd As Object
Dim db As Database


'----------------
' Close the form
'----------------
Sub cmdClose_Click ()

    On Error GoTo Err_cmdClose_Click
    DoCmd Close

    Exit_cmdClose_Click:
       Exit Sub

    Err_cmdClose_Click:
       MsgBox Error$
       Resume Exit_cmdClose_Click
End Sub



'---------------------------
' Loop back to Close button
'---------------------------
Sub cmdLoop_Enter ()
    DoCmd GoToControl "cmdClose"
End Sub


'--------------------------------------------------------
' Hide the photo so Room Information subform can be seen
'--------------------------------------------------------
Sub cmdHidePhoto_Click ()
    [fraPhoto].Visible = "No"
    [subRoomInformation].Visible = "Yes"
    [lblRoomInformation].Visible = "Yes"
    [cmdShowPhoto].Visible = "Yes"
    DoCmd GoToControl "cmdShowPhoto"
    [cmdHidePhoto].Visible = "No"
End Sub

Sub cmdShowPhoto_Click ()

    [subRoomInformation].Visible = "No"
    [lblRoomInformation].Visible = "No"
    [fraPhoto].Visible = "Yes"
    [cmdHidePhoto].Visible = "Yes"
    DoCmd GoToControl "cmdHidePhoto"
    [cmdShowPhoto].Visible = "No"

End Sub



Sub Form_Open (Cancel As Integer)

    Dim strMessage As String
    Dim strTitle As String
    strMessage = "Sorry - couldn't find any houses matching your criteria."
    strTitle = "Selected Houses"

    If Forms![frmSelectedHouses].Dynaset.RecordCount <> 0 Then
        Forms![frmSelectedHouses]![subRoomInformation].Visible = "No"
        Forms![frmSelectedHouses]![lblRoomInformation].Visible = "No"
        Forms![frmSelectedHouses]![fraPhoto].Visible = "Yes"
        Forms![frmSelectedHouses]![cmdHidePhoto].Visible = "Yes"
        DoCmd GoToControl "cmdHidePhoto"
        Forms![frmSelectedHouses]![cmdShowPhoto].Visible = "No"
        DoCmd GoToControl "cmdClose"
    Else
        MsgBox strMessage, 48, strTitle
        DoCmd CancelEvent
    End If

End Sub


The cmdPrint subroutine implements most of the Access-to-Word dialog. Word is used as an OLE 2 server. The only exposed OLE 2 function in Word 6.0 is "Word.Basic". However, Word.Basic gives us access to all Word Basic commands and functions.


'-----------------------------------------------------------
'Subroutine to send Selected Houses data to a Word document.
'This subroutine uses Word 6a as an OLE 2 server.
'-----------------------------------------------------------
Sub cmdPrint_Click ()

  Dim Send As String ' Temporary variable

  'Create WordBasic object and assign to "wd" object variable
  Set wd = CreateObject("Word.Basic")
  
  ' Moves Word to the top of screen
  wd.AppActivate "Microsoft Word", 1

  'Create new Word document, using HOMEFIND.DOT template
  wd.FileNew "HOMEFIND"

  ' For some reason, "SendToWord" will not accept a text string
  ' directly out of controls on the form. It must be copied to
  ' a string variable first, then the string variable is sent
  ' to the "SendToWord" subroutine.
  ' Because the Buyers information appears on another form (the
  ' Preferences form), we must use a "fully qualified" reference
  ' to the txtBuyers control on that form.
  Send$ = Forms!frmPreferences![txtBuyers]
  SendToWord "_Client_", Send$
  wd.CenterPara ' Center header on page 
  
  ' Format a number (number of bedrooms as string when sent.
  ' The number of bedrooms is numeric data in the table and
  ' is formatted as a number on the Selected Houses form
  SendToWord "_Bedrooms_", Str$([txtBedrooms])
  SendToWord "_MLSNumber_", Str$([txtMLSNumber])
  
  ' Format a number (list price) as currency when sent.
  SendToWord "_ListPrice_", Format$([txtListPrice], "Currency")
  

  ' The following data (Street Address) is "fully qualified".
  Send$ = Forms![frmSelectedHouses].[txtStreetAddress]
  SendToWord "_StreetAddress_", Send$
  

  ' Because the cmdPrint_Click () subroutine exists as
  ' "code behind the form" we can reference the Mail City
  ' data without specifying the form on which it appears.
  Send$ = [txtMailCity]
  SendToWord "_City_", Send$
  
  Send$ = [txtState]
  SendToWord "_State_", Send$
  
  ' You can use the Str$() function when the data is
  ' a number, even if the field's data type is text
  SendToWord "_ZipCode_", Str$([txtZipCode])

  If Len([txtSubdivision]) > 1 Then
    Send$ = [txtSubdivision]
    SendToWord "_Subdivision_", Send$
  Else
    SendToWord "_Subdivision_", "n/a"
  End If

  ' Send data in upper case
  SendToWord "_County_", UCase$(Forms![frmSelectedHouses].[txtCounty])

  Send$ = [txtTaxCity]
  SendToWord "_TaxCity_", Send$

  Send$ = [txtTaxYear]
  SendToWord "_TaxYear_", Send$

  Send$ = [txtRemarks]
  SendToWord "_HouseDescription_", Send$

  SendToWord "_SquareFeet_", Str$([txtSquareFeet])
  SendToWord "_Taxes_", Format$([txtTaxes], "Currency")
  SendToWord "_YearBuilt_", Str$([txtYearBuilt])
  SendToWord "_LotWidth_", Str$([txtLotWidth])
  SendToWord "_LotDepth_", Str$([txtLotDepth])
  SendToWord "_GarageSpaces_", Str$([txtGarageSpaces])

  ' Sending data from an option group control on the form
  wd.StartOfDocument
  wd.EditFind "_Attached_"
  
  If grpGarageAttached Then  ' grpGarageAttached = option group
    wd.Insert "Attached"
  Else
    wd.Insert "Unattached"
  End If


  ' No special treatment before sending this data
  wd.StartOfDocument
  wd.EditFind "_Agency_"
  wd.Insert [txtAgencyName]

  wd.StartOfDocument
  wd.EditFind "_Agent_"
  wd.Insert [txtAgentName]
  wd.Insert "   "
  wd.Insert [txtAgentPhone]

  PrintRoomInformation ' Subroutine to print room information
  
  PrintPhoto           ' Subroutine to print photo of house
  
  wd.StartOfDocument   ' Go back to start of document

End Sub

The following subroutine serves as the primary means to transfer data to the Word document. Notice that SendToWord() works only with string data.


'-------------------------------------------------------
' First, locate the "Find" string in the Word document,
' then send the data in "Replace" string to Word
'-------------------------------------------------------
Sub SendToWord (Find As String, Replace As String)

  wd.StartOfDocument
  wd.EditFind Find$
  wd.Insert Replace$

End Sub




This subroutine sends the picture of the house to the Word document. It first has to make sure the photo is visible on the Selected Houses form, then enable it so it can be copied to the Clipboard. Once the photo has been copied to the Clipboard, go to the "HousePicture" bookmark In the Word document and paste the picture there. Then, center the picture and resize it larger by 50%.

' ------------------------------------
' Print the house photo on the report
' ------------------------------------
Sub PrintPhoto ()
  
  ' ---------------------------------------------------------
  ' First, make sure the Room Information subform
  ' ("subRoomInformation") on the Selected Houses form
  ' is hidden, then reveal the House Photo ("fraPhoto")
  ' ---------------------------------------------------------
  [subRoomInformation].Visible = "No"
  [lblRoomInformation].Visible = "No"
  [fraPhoto].Visible = "Yes"
  [cmdHidePhoto].Visible = "Yes"
  DoCmd GoToControl "cmdHidePhoto"
  [cmdShowPhoto].Visible = "No"

  ' ---------------------------------------------------------
  ' Go to the House Photo control and copy to the Clipboard
  ' The House Photo control must be enabled before you
  ' can go to it and copy it to the Clipboard
  ' ---------------------------------------------------------
  [fraPhoto].Enabled = "Yes"
  DoCmd GoToControl "fraPhoto"
  DoCmd DoMenuItem A_FORMBAR, A_EDITMENU, A_COPY ' Copy to clipboard
  [fraPhoto].Enabled = "Yes"

  ' -----------------------------------------------------
  ' Paste Clipboard contents (picture) and center picture.
  ' Uses Bookmark to locate where the picture should go.
  ' -----------------------------------------------------
  wd.EditGoTo "HousePicture"  ' Go to "HousePicture" bookmark
  wd.EditPaste                ' Paste the image from the Clipboard
  wd.CenterPara               ' Center picture on page
  wd.CharLeft 1, 1            ' Highlight the picture
  wd.FormatPicture 0, "0" + Chr$(34), "0" + Chr$(34), "0" + Chr$(34), "0" + Chr$(34), "150%", "150%"
  wd.CharRight 1
  wd.LineDown 1               ' Not InsertPara because we don't
                              ' want the next line to be centered
  DoCmd GoToControl "cmdPrint"  ' Move focus back to "Send to Word" button
  [fraPhoto].Enabled = "No"   ' Disable house photo on formEnd Sub

'----------------------------------------------
' Retrieve and print the room information
'----------------------------------------------
Sub PrintRoomInformation ()

  Dim rsRoomInfo  As Recordset  ' tblRoomInformation
  Dim rsRoomCodes As Recordset  ' tlkpRoomCodes
  Dim Criteria1 As String
  Dim Criteria2 As String

  ' Set search criteria.
  Criteria1 = "MLSNumber=" + [forms]![frmSelectedHouses]![txtMLSNumber] 

  Set db = CurrentDB()
  
  Set rsRoomInfo = db.OpenRecordset("tblRoomInformation", DB_OPEN_DYNASET)
  Set rsRoomCodes = db.OpenRecordset("tlkpRoomCodes", DB_OPEN_DYNASET)

  ' Find first occurrence.
  rsRoomInfo.FindFirst Criteria1
  
  wd.StartOfDocument
  wd.EditFind "_RoomInformation_"

' Loop until no matching records.
  Do Until rsRoomInfo.NoMatch
    Criteria2 = "RoomCode=" + "'" + rsRoomInfo![RoomCode] + "'"

    rsRoomCodes.FindFirst Criteria2
    wd.Insert rsRoomCodes!Description
    wd.Insert Chr$(9)
    wd.Insert Str$(rsRoomInfo![Width])
    wd.Insert Chr$(9)
    wd.Insert Str$(rsRoomInfo![Length])

    If Len(rsRoomInfo![SpecialFeatures]) > 1 Then
      wd.Insert Chr$(9)
      wd.Insert rsRoomInfo![SpecialFeatures]
    End If

    wd.InsertPara
    rsRoomInfo.FindNext Criteria1   ' Find next occurrence
  Loop                              ' End of loop.

End Sub

Source code for HomeFinder "Selected Houses" form



4/23/94	Michael Groh	Page: 8


