Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

如何在模块中创建、修改、查找、替换代码

时 间:2005-01-31 00:00:00
作 者:竹笛   ID:8  城市:上海  QQ:2851379730点击这里给张志发消息
摘 要:如何在模块中创建、修改、查找、替换代码 
正 文:

第一步:在一个新mdb文件中,手动建立一模块,命名为:Create Code,复制下面的代码到模块中:
Sub TestOpenDatabase()
    Dim DB As DAO.Database
    Set DB = CurrentDb
    MsgBox "The Database " & DB.Name & " opened successfully!"
    DB.Close
End Sub

第二步:手动建立另一模块,命名为:CodeMaker,复制下面的代码到模块中:
注意:必须引用DAO3.6
Option Explicit
Dim MyModule As Module

Sub MakeCode()

   Dim strIndent As String, strText As String

   ' Create 4 spaces for code indent.
   strIndent = "    "

   ' Build a string variable with the code to be written
   ' to the new module.
   strText = "Sub TestOpenDatabase()" & vbCrLf
   strText = strText & strIndent & "Dim DB As DAO.Database" & vbCrLf
   strText = strText & strIndent & "Set DB = CurrentDB" & vbCrLf
   strText = strText & strIndent & "MsgBox ""The Database "" & " & _
      "DB.Name & "
   strText = strText & strIndent & strIndent & """ opened " & _
      "successfully!""" & vbCrLf
   strText = strText & strIndent & "DB.Close" & vbCrLf
   strText = strText & "End Sub"

   ' Create a new Module.
   Application.RunCommand acCmdNewObjectModule

   ' Set MyModule to be the new Module Object.
    Set MyModule = Application.Modules.Item(Application.Modules.Count - 1)
   ' Insert the code string into the new module.
   MyModule.InsertText strText

   ' Save, close, and rename the new Module as "Created Code."

   DoCmd.Save acModule, MyModule

   DoCmd.Close acModule, MyModule, acSaveYes

   DoCmd.Rename "Created Code", acModule, MyModule

End Sub

第三步:保存模块代码

第四步:运行MakeCode子程序,将创建一个新模块Created Code,其代码与第一步的Create Code模块是一样的.

以上实现的是创建一个已有的模块。

同理,用下面的三个子程序实现搜索、替换、修改模块代码:
Sub SearchCode()
'搜索模块中的代码
  Dim StartLine As Long, StartColumn As Long
  Dim EndLine As Long, EndColumn As Long


  ' Open the Module you want to modify.
  DoCmd.OpenModule "Created Code"

  ' Set the Created Code Modules as the Object.
  Set MyModule = Application.Modules("Created Code")

  ' Search for string "DB.Close".
  If MyModule.Find("DB.Close", StartLine, StartColumn, _
     EndLine, EndColumn) Then

     ' If string is found, insert new line of code using the same
     ' column indent.
     MyModule.InsertLines StartLine + 1, _
        String(StartColumn - 1, " ") & "Set DB = Nothing"
  Else
     MsgBox "Text not found."
  End If

  ' Save and close the module.
  DoCmd.Save acModule, MyModule
  DoCmd.Close acModule, MyModule

End Sub

Sub ReplaceCode()
'替换模块中的代码
Dim StartLine As Long, StartColumn As Long
Dim EndLine As Long, EndColumn As Long
' Open the Module you want to modify.
DoCmd.OpenModule "Created Code"

' Set the Created Code Modules as the Object.
Set MyModule = Application.Modules("Created Code")

' Search for string "Set DB =".
If MyModule.Find("Set DB =", StartLine, StartColumn, EndLine, _
   EndColumn) Then

  ' If string is found, insert new line of code using the same
  ' column indent.
  MyModule.ReplaceLine StartLine, String(StartColumn - 1, " ") & _
     "Set DB = DBEngine.OpenDatabase(""C:\Program Files\" & _
     "Microsoft Office\"" & _" _
     & vbCrLf & "        ""Office\Samples\Inventry.mdb"")"

Else
  MsgBox "Text not found."
End If

' Save and close the module.
DoCmd.Save acModule, MyModule
DoCmd.Close acModule, MyModule, acSaveYes

End Sub

此示例使用Find方法来查找字符串"Inventry.mdb",并将其替换为"Northwind.mdb"。

Sub ModifyCode()

'修改模块中的代码
   Dim StartLine As Long, StartColumn As Long
   Dim EndLine As Long, EndColumn As Long
   Dim strLine As String, strNewLine As String
   Dim intChr As Integer, intBefore As Integer, intAfter As Integer
   Dim strLeft As String, strRight As String
   Dim strSearchText As String, strNewText

   ' The string you are searching for is:
   strSearchText = "Inventry.mdb"

   ' The replacement string is:
   strNewText = "Northwind.mdb"

   ' Open the Module you want to modify.
   DoCmd.OpenModule "Created Code"

   ' Set the Created Code Modules as the Object.
   Set MyModule = Application.Modules("Created Code")

   ' Search for string.
   If MyModule.Find(strSearchText, StartLine, StartColumn, EndLine, _
      EndColumn) Then

    ' Store text of line containing string.
    strLine = MyModule.Lines(StartLine, Abs(EndLine - StartLine) + 1)

    ' Determine length of line.
    intChr = Len(strLine)

    ' Determine number of characters preceding search text.
    intBefore = StartColumn - 1

    ' Determine number of characters following search text.
    intAfter = intChr - CInt(EndColumn - 1)

    ' Store characters to left of search text.
    strLeft = Left$(strLine, intBefore)

    ' Store characters to right of search text.
    strRight = Right$(strLine, intAfter)

    ' Construct string with replacement text.
    strNewLine = strLeft & strNewText & strRight

    ' Replace the original line.
    MyModule.ReplaceLine StartLine, strNewLine

   Else
      MsgBox "Text not found."
   End If

   ' Save and close the module.
   DoCmd.Save acModule, MyModule
   DoCmd.Close acModule, MyModule, acSaveYes

End Sub

 



Access软件网官方交流QQ群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助