[VBA]批量替换图块
<DIV>我在做一个块批量替换的程序。原先图中有二、三十种块,现在用其他的图块来替换,如GC120-->10211、GC119-->12111。这种对应关系我已经在ACCESS数据库中已经建立,现在就是在考虑程序运行效率的问题。我想了两种方法:</DIV><DIV>1)一次性将对应关系读入数组中保存,这样就只需访问数据库一次就行。替换时再从数组中找对应关系。</DIV>
<DIV>2)每次都从数据库中去找对应关系。</DIV>
<DIV>请问版主,上面两种方法,哪种应该会运行快点?还有没有其他更好的方法?</DIV> <P>你可以考虑做成INI文件,格式</P>
<P><BR>GC120=10211<BR>GC119=12111<BR>GC121=12112<BR>GC122=12113<BR>GC123=12114</P>
<P>然后用API函数读取.</P>
<P>VB读取INI文件程序:</P>
<P>Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _<BR> (ByVal lpApplicationName As String, _<BR> ByVal lpKeyName As Any, ByVal lpDefault As String, _<BR> ByVal lpReturnedString As String, ByVal nSize As Long, _<BR> ByVal lpFileName As String) As Long</P>
<P>Public Function ReadIni(ByVal file As String, ByVal section As String, _<BR>ByVal key As String, ByVal default As String) As String<BR> Dim x As Long<BR> Dim sSection As String, sEntry As String, sDefault As String<BR> Dim sRetBuf As String, iLenBuf As Integer, sFileName As String<BR> <BR> 'Six arguments<BR> sSection$ = section<BR> sEntry$ = key<BR> sDefault$ = default<BR> sRetBuf$ = String$(256, 0) '256 null character<BR> iLenBuf% = Len(sRetBuf$)<BR> sFileName$ = file<BR> <BR> 'function will return a value of 12 in this case<BR> x = GetPrivateProfileString(sSection$, sEntry$, sDefault$, sRetBuf$, iLenBuf%, sFileName$)<BR> <BR> 'sValue$ will contain the string 'PACKARD BELL'<BR> sValue$ = Left$(sRetBuf$, x)<BR> <BR> 'return value<BR> ReadIni = sValue$<BR> <BR>End Function</P>
<P>Sub Test_ReadIni()<BR> MsgBox ReadIni("d:\test.ini", "TEST", "2", "null")<BR>End Sub</P>
<P>文件d:\test.ini的内容:</P>
<P><BR>1=hello<BR>2=world<BR>3=!</P>
页:
[1]