我是VBA的新手,我正在尝试自动化一个过程,将特定文件夹中的4个.csv文件导入到SQL数据库中。我有一个代码,其中我有4个命令按钮,在每个按钮中,我选择文件,然后将它们导入到SQL中。然而,我想知道是否有一种方法,我可以只有一个命令按钮,从特定的文件夹中选择所有的.csv文件,然后将它们导入到SQL中。假设文件夹名称为Database,路径为: Y:\Data\Database
我有以下导入按钮代码,用于将本地路径转换为SQL服务器路径,以及所有其他类型的函数,用于将数据导入到SQL服务器。(fName1...fName4是用户当前选择的4个文件路径的TextBox值)
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
任何帮助都将不胜感激。非常感谢。
发布于 2012-08-30 01:12:22
这将帮助您遍历文件夹中的.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
如果你需要更多的改进,比如你只想要文件夹中的某些.csv
文件,可以在While strFile <> ""
后面添加一条语句,去掉任何你不需要的文件。类似于:
If InStr(1, strFile, "myName") > 0 Then
'-> code to upload the file to SQL database
End If
发布于 2014-05-28 20:44:42
我使用
SearchFile = Dir(SearchFolder & "*.csv")
代码行,以便在第一次通过代码时仅成功选择.csv文件,但是当我使用
SearchFile = Dir
在每个循环中,它选择其他文件类型,而我还没有找到一种方法来强制该行代码只选择.csv。所以我的解决方法是在后面添加一个IF语句来检查它
SearchFile = Dir
CheckFile:
If Right(SearchFile, 3) <> "csv" Then
SearchFile = Dir
GoTo CheckFile
End If
这样,如果文件夹中有其他文件类型,它就会跳过它们。我之所以使用这种格式,是因为我的sub中有其他代码,但它可以重写为
CheckFile:
SearchFile = Dir
If Right(SearchFile, 3) <> "csv" Then
GoTo CheckFile
End If
https://stackoverflow.com/questions/12182959
复制相似问题