如何在模块中创建、修改、查找、替换代码 -张志
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


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

发表时间:2005/1/31 评论(0) 浏览(12824)  评论 | 加入收藏 | 复制
   
摘 要:如何在模块中创建、修改、查找、替换代码 
正 文:

第一步:在一个新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群(群号:198465573)
 
 相关文章
查找和替换access表、查询或窗体中的部分或全部记录  【http://www.microsoft.com/  2012/5/1】
数据内容替换  【hjs  2012/7/24】
ACCESS:Replace 替换文字  【Access软件网  2012/11/15】
Access替换某字段的部分内容  【宏鹏  2013/10/30】
用自己的错误提示替换access的默认提示例子  【杜超-2号  2014/1/11】
【Access小品】以戈舂米---正则表达式替换数据示例  【煮江品茶  2014/4/16】
【Access小品】引经据典--格式化字符串替换示例  【煮江品茶  2015/4/23】
常见问答
技术分类
相关资源
文章搜索
关于作者

张志

文章分类

文章存档

友情链接