我正在尝试编写一个代码,该代码首先使用SQL查询获取我的文件名,然后在文件夹及其子文件夹中查找此文件。如果文件存在,我不希望代码执行任何操作,但是如果文件不存在,则我想将文件名添加到列表中,然后最后我要显示所有这些列表项。这是我到目前为止所做的,但是没有用。
Dim oFSO,oFl
Dim Fichier
Dim Generees, Nongenerees
Dim fldr As Object
Dim subfldr As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set MyCnx = CreateObject("ADODB.Connection")
MyCnx.ConnectionString = "Provider=SQLOLEDB.1;Password=OP_USER_2;Persist Security Info=True;User ID=OP_USER_2;Initial Catalog=Fiche_OP;Data Source=JRG4581\MSSQLSERVER32"
adOpenForwardOnly = 0
adLockReadOnly =1
MyCnx.CommandTimeout = 0
MyCnx.Open
Set MyRs = CreateObject("ADODB.Recordset")
Set Generees = CreateObject("Scripting.Dictionary")
Set Nongenerees = CreateObject("Scripting.Dictionary")
'***** Get file name *****
SQL= ""
SQL= SQL & "select A.OPEDesc+'_'+A.OPEValeur+'_'+A.GammeValeur+' .xlsb' as fiche "
SQL= SQL & "from dbo.Fiche_OPE_Test A "
SQL= SQL & "where Statut_OPE = 'Actif' "
'***** check if the file exists ******
MyRs.Open SQL, MyCnx, adOpenForwardOnly, adLockReadOnly
Do Until MyRs.EOF
Fichier = MyRs("fiche")
Set fldr = oFSO.GetFolder("my folder directory")
For Each subfldr In fldr.SubFolders
If oFSO.FileExists(fichier) Then
Generees.Add(fichier)
Else
Nongenerees.Add(fichier)
Exit For
End If
Next subfldr
MyRs.MoveNext
Loop
MsgBox Join(Nongenerees.ToArray)
MyRs.Close
Set MyRs = Nothing
MyCnx.Close
Set MyCnx = Nothing
Set oConn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.recordset")
ConnectionString = "Provider=SQLOLEDB.1;Password=OP_USER_2;Persist Security 'Info=True;User ID=OP_USER_2;Initial Catalog=Fiche_OP;Data Source=JRG4581\MSSQLSERVER32"
oConn.Open ConnectionString
Set rs = Nothing
oConn.Close
Set oConn = Nothing