home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Programmer'…arterly (Limited Edition) / Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso / code / ch25code / dde.bas < prev    next >
Encoding:
BASIC Source File  |  1995-07-27  |  4.7 KB  |  110 lines

  1. Attribute VB_Name = "Module1"
  2. '*************************************************************
  3. ' DDE.BAS: An application independant, reusable module that
  4. '          peforms DDEExecute and DDERequest methods.
  5. '*************************************************************
  6. '*************************************************************
  7. ' Require variable declartion, make text comparisions case-
  8. ' insensitive, and declare API functions.
  9. '*************************************************************
  10. Option Explicit
  11. Option Compare Text
  12. '#If Win16 Then
  13. Private Declare Function GetModuleHandle Lib "Kernel" _
  14.                     (ByVal ModuleName As String) As Integer
  15. '#End If
  16. '*************************************************************
  17. ' Execute one DDE Command.
  18. '*************************************************************
  19. Public Sub DDEExecute(Source As Control, DDEApplication$, _
  20.                         DDECommand$, Optional DDETopic)
  21.     
  22.     On Error GoTo DDEExecute_Error
  23.     '*********************************************************
  24.     ' If the program isn't running, then exit.
  25.     '*********************************************************
  26.     If GetModuleHandle(DDEApplication) = 0 Then
  27.         MsgBox "This feature requires " & _
  28.                 UCase$(DDEApplication) & _
  29.                 ".EXE to be running!", vbCritical
  30.         Exit Sub
  31.     End If
  32.     '*********************************************************
  33.     ' If the optional argument wasn't provided, then assume
  34.     ' System.
  35.     '*********************************************************
  36.     If IsMissing(DDETopic) Then DDETopic = "System"
  37.     '*********************************************************
  38.     ' Manual connect, execute the command, and close the link.
  39.     '*********************************************************
  40.     Source.LinkTopic = DDEApplication & "|" & DDETopic
  41.     Source.LinkMode = 2             'Open the Link
  42.     Source.LinkExecute DDECommand   'Send the Command
  43.     Source.LinkMode 0               'Close the Link
  44.     Exit Sub
  45.  
  46. DDEExecute_Error:
  47.     Resume Next
  48. End Sub
  49. '*************************************************************
  50. ' Execute a DDE Command.
  51. '*************************************************************
  52. Public Sub DDERequest(Source As Control, DDEApplication$, _
  53.                         DDETopic$, DDEItem$)
  54.     
  55.     On Error GoTo DDERequest_Error
  56.     '*********************************************************
  57.     ' If the program isn't running, then exit.
  58.     '*********************************************************
  59.     If GetModuleHandle(DDEApplication) = 0 Then
  60.         MsgBox "This feature requires " & _
  61.                 UCase$(DDEApplication) & _
  62.                 ".EXE to be running!", vbCritical
  63.         Exit Sub
  64.     End If
  65.     '*********************************************************
  66.     ' Manual connect, request the data, and close the link.
  67.     '*********************************************************
  68.     Source.LinkTopic = DDEApplication & "|" & DDETopic
  69.     Source.LinkItem = DDEItem
  70.     Source.LinkMode = 2
  71.     Source.LinkRequest
  72.     Source.LinkMode = 0
  73.     '*********************************************************
  74.     ' WARNING: If you wish to use a topic such as NextRow,
  75.     '          you will have to rewrite this program so that
  76.     '          the link is not broken. NextRow and PrevRow
  77.     '          require a consistant link in order to cycle
  78.     '          through the database.
  79.     '*********************************************************
  80.     Exit Sub
  81.  
  82. DDERequest_Error:
  83.     Resume Next
  84. End Sub
  85. '*************************************************************
  86. ' Locate a string within Source, and replace it with
  87. ' ReplaceStr.
  88. '*************************************************************
  89. Public Function Replace(ByVal Source$, FindStr$, _
  90.                         ByVal ReplaceStr$) As String
  91. Dim res%, retStr$
  92.     '*********************************************************
  93.     ' See if the search string exists.
  94.     '*********************************************************
  95.     res = InStr(Source, FindStr)
  96.     '*********************************************************
  97.     ' While FileStr is in Source, continue to replace it with
  98.     ' ReplaceStr.
  99.     '*********************************************************
  100.     Do While res <> 0
  101.         retStr = retStr & Left$(Source, res - 1) & ReplaceStr
  102.         Source = Mid(Source, res + 1)
  103.         res = InStr(Source, FindStr)
  104.     Loop
  105.     '*********************************************************
  106.     ' Don't forget to return whatever is left over in source.
  107.     '*********************************************************
  108.     Replace = retStr & Source
  109. End Function
  110.