2012-08-29 13 views
1

我是VBA的新手,我試圖自動執行將特定文件夾中的.csv文件導入到SQL數據庫的過程。 我有一個代碼,我有4個命令按鈕,在每個4中,我挑選文件,然後將它們導入到SQL。但是,我想知道是否有一種方法可以讓我只有一個命令按鈕從該特定文件夾中選取所有.csv文件,然後將它們導入到SQL中。假設文件夾名稱是數據庫在以下路徑中:Y:\ Data \ Database如何選擇一個文件夾中的所有.csv文件,然後通過VBA編碼導入它們

我有以下導入按鈕代碼將本地路徑轉換爲SQL Server路徑和所有其他類型的函數將數據導入到SQL Server 。 (fName1 ... fName4是當前正由用戶選擇了4條文件路徑文本框的值)

Private Sub ImportButton_Click() 
Dim fName1 As String 
Dim fName2 As String 
Dim fName3 As String 
Dim fName4 As String 
Dim perc As Single 

Dim index As Integer 
Dim subStr As String 
Dim sqlStr As String 
sqlStr = "E:\Analytics\" 

'Convert the local path to SQL server path 
fName1 = TextBox1.Value 
index = InStr(1, fName1, "\") 
subStr = Left(fName1, index) 
fName1 = Replace(fName1, subStr, sqlStr, , 1) 

fName2 = TextBox2.Value 
index = InStr(1, fName2, "\") 
subStr = Left(fName2, index) 
fName2 = Replace(fName2, subStr, sqlStr, , 1) 

fName3 = TextBox3.Value 
index = InStr(1, fName3, "\") 
subStr = Left(fName3, index) 
fName3 = Replace(fName3, subStr, sqlStr, , 1) 

fName4 = TextBox4.Value 
index = InStr(1, fName4, "\") 
subStr = Left(fName4, index) 
fName4 = Replace(fName4, subStr, sqlStr, , 1) 

'Modify the text captions for test purpose 
TextBox1.Value = fName1 
TextBox2.Value = fName2 
TextBox3.Value = fName3 
TextBox4.Value = fName4 

Dim cnPubs As ADODB.Connection 
Dim cmd As ADODB.Command 

' Create a connection object. 
Set cnPubs = New ADODB.Connection 

' Provide the connection string. 
Dim strConn As String 

'Use the SQL Server OLE DB Provider. 
strConn = "PROVIDER=SQLOLEDB;" 

'Connect to the Pubs database on server hcdcd-actstat01 . 
strConn = strConn & "DATA SOURCE=hcdcd-actstat01;INITIAL CATALOG=Analytics;" 

'Use an integrated login. 
strConn = strConn & " INTEGRATED SECURITY=sspi;" 

'Now open the connection. 
cnPubs.Open strConn 


Set cmd = New ADODB.Command 
cmd.ActiveConnection = cnPubs 
cmd.CommandType = adCmdStoredProc 
cmd.CommandText = "dbo.Proc_CapitalAllocation_Step1" 
cmd.CommandTimeout = 1200 'Seconds 

''cmd.Parameters.Append cmd.CreateParameter_(fName1, fName2, fName3, fName4) 
Call cmd.Execute(Parameters:=Array(fName1, fName2, fName3, fName4), Options:=adCmdStoredProc) 

End Sub 

任何幫助,將不勝感激。 非常感謝。

回答

3

這將幫助您循環瀏覽文件夾中的.csv文件。

Option Explicit 

Sub LoopThroughFiles() 

    Dim strFile As String, strPath As String 

    strPath = "E:\Analytics\" 
    strFile = Dir(strPath & "*.csv") 

    While strFile <> "" 

     '-> code upload the file to SQL Database 

     strFile = Dir 

    Wend 


End Sub 

如果您需要更多的細化,比如你只需要在文件夾中的某些文件.csvWhile strFile <> ""後添加語句來淘汰你不想要的任何文件。喜歡的東西:

If InStr(1, strFile, "myName") > 0 Then 
    '-> code to upload the file to SQL database 
End If 
+0

謝謝!我會在Excel沒有崩潰時嘗試一下...... – Kristina

0

我使用

SearchFile = Dir(SearchFolder & "*.csv") 

線成功地只選擇的.csv文件中第一次通過的代碼,但正如我在文件夾中的所有文件循環使用

SearchFile = Dir 

在每個循環中,它選擇其他文件類型,我還沒有找到一種方法來強制該行代碼只選擇.csv。所以我的修復是添加一個IF語句後立即檢查

 SearchFile = Dir   
CheckFile: 
    If Right(SearchFile, 3) <> "csv" Then 
     SearchFile = Dir 
     GoTo CheckFile 
    End If 

這樣,如果它只是跳過這些文件夾中的其他文件類型。我使用該格式是因爲我的子文件中的其他代碼,但它可以重寫爲

CheckFile: 
    SearchFile = Dir 
    If Right(SearchFile, 3) <> "csv" Then 
     GoTo CheckFile 
    End If 
相關問題