我正在尝试使用Sub get_data()
Dim strSQL As String
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim DBPath As String
Dim sconnect As String
DBPath = "\\abc\Quality Report.xlsx"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
cnn.Open sconnect
strSQL = "SELECT * FROM [Error_Log$] WHERE "
If cboprocess.Text <> "" Then
strSQL = strSQL & " [Process]='" & cboprocess.Text & "'"
End If
If cboaudittype.Text <> "" Then
If cboprocess.Text <> "" Then
strSQL = strSQL & " AND [Audit_Type]='" & cboaudittype.Text & "'"
Else
strSQL = strSQL & " [Audit_Type]='" & cboaudittype.Text & "'"
End If
End If
If cbouser1.Text <> "" Then
If cboprocess.Text <> "" Or cboaudittype.Text <> "" Then
strSQL = strSQL & " AND [User_Name]='" & cbouser1.Text & "'"
Else
strSQL = strSQL & " [User_Name]='" & cbouser1.Text & "'"
End If
End If
If cborptmgr.Text <> "" Then
If cboprocess.Text <> "" Or cboaudittype.Text <> "" Or cbouser1.Text <> "" Then
strSQL = strSQL & " AND [Reporting_Manager]='" & cborptmgr.Text & "'"
Else
strSQL = strSQL & " [Reporting_Manager]='" & cborptmgr.Text & "'"
End If
End If
If cbotranstyp.Text <> "" Then
If cboprocess.Text <> "" Or cboaudittype.Text <> "" Or cbouser1.Text <> "" Or cborptmgr.Text <> "" Then
strSQL = strSQL & " AND [Transaction_Type]='" & cbotranstyp.Text & "'"
Else
strSQL = strSQL & " [Transaction_Type]='" & cbotranstyp.Text & "'"
End If
End If
If cboperiod.Text <> "" Then
If cboprocess.Text <> "" Or cboaudittype.Text <> "" Or cbouser1.Text <> "" Or cborptmgr.Text <> "" _
Or cbotranstyp.Text <> "" Then
strSQL = strSQL & " AND [Period]='" & cboperiod.Text & "'"
Else
strSQL = strSQL & " [Period]='" & cboperiod.Text & "'"
End If
End If
If cbolocation.Text <> "" Then
If cboprocess.Text <> "" Or cboaudittype.Text <> "" Or cbouser1.Text <> "" Or cborptmgr.Text <> "" _
Or cbotranstyp.Text <> "" Or cboperiod.Text <> "" Then
strSQL = strSQL & " AND [Location]='" & cbolocation.Text & "'"
Else
strSQL = strSQL & " [Location]='" & cbolocation.Text & "'"
End If
End If
If cbofatnfat.Text <> "" Then
If cboprocess.Text <> "" Or cboaudittype.Text <> "" Or cbouser1.Text <> "" Or cborptmgr.Text <> "" _
Or cbotranstyp.Text <> "" Or cboperiod.Text <> "" Or cbolocation.Text <> "" Then
strSQL = strSQL & " AND [Fatal_NonFatal]='" & cbofatnfat.Text & "'"
Else
strSQL = strSQL & " [Fatal_NonFatal]='" & cbofatnfat.Text & "'"
End If
End If
If cbostatus.Text <> "" Then
If cboprocess.Text <> "" Or cboaudittype.Text <> "" Or cbouser1.Text <> "" Or cborptmgr.Text <> "" _
Or cbotranstyp.Text <> "" Or cboperiod.Text <> "" Or cbolocation.Text <> "" Or cbofatnfat.Text <> "" Then
strSQL = strSQL & " AND [Remarks]='" & cbostatus.Text & "'"
Else
strSQL = strSQL & " [Remarks]='" & cbostatus.Text & "'"
End If
End If
If txtfromauditdt.Text <> "" Then
If cboprocess.Text <> "" Or cboaudittype.Text <> "" Or cbouser1.Text <> "" Or cborptmgr.Text <> "" _
Or cbotranstyp.Text <> "" Or cboperiod.Text <> "" Or cbolocation.Text <> "" Or cbofatnfat.Text <> "" _
Or cbostatus.Text <> "" Then
strSQL = strSQL & " AND [Audit_Date] BETWEEN '" & txtfromauditdt.Text & "' AND GETDATE()"
Else
strSQL = strSQL & " [Audit_Date] BETWEEN '" & txtfromauditdt.Text & "' AND GETDATE()"
End If
End If
Debug.Print strSQL
Set rs.ActiveConnection = cnn
rs.Open strSQL, cnn
Sheet1.Range("A42").CopyFromRecordset rs
rs.Close
cnn.Close
End Sub
通过不同的Excel从Excel中提取数据,但是当我尝试从日期到系统日期之间检索数据时,我在Excel中遇到自动化错误。
我检查了各种文章,但我无法验证这些条件,因为我没有SQL Server,所以我直接进入Excel编码,但我又得到同样的错误。
请帮助......
SELECT *
FROM [Error_Log$]
WHERE [Audit_Date] BETWEEN '16-Nov-2015' AND getdate()
以下是来自strsql的调试打印
<input type="radio" id="nav-expand" name="nav" />
<input type="radio" id="nav-collapse" name="nav" checked="checked" />
<header>
<div class="logo">
<h1>Header Text</h1>
</div>
<label for="nav-expand" class="btn-nav-expand">
<span class="top"></span>
<span class="middle"></span>
<span class="bottom"></span>
</label>
<label for="nav-collapse" class="btn-nav-collapse">
<span class="top"></span>
<span class="middle"></span>
<span class="bottom"></span>
</label>
<nav>
<ul>
<li><a data-title="Home">Home</a></li>
<li><a data-title="News">News</a></li>
<li><a data-title="Out People">Our People</a></li>
<li><a data-title="About Us">About Us</a></li>
<li><a data-title="Product and Services">Product and Services</a></li>
<li><a data-title="Case Studies">Case Studies</a></li>
<li><a data-title="Contact Us">Contact Us</a></li>
</ul>
</nav>
<label for="nav-collapse" class="overlay"></label>
</header>
错误截图
答案 0 :(得分:0)
我认为你的问题是GetDate()
构建WHERE子句的方法也可以简单得多:
Sub get_data()
Dim strSQL As String, strWhere As String
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim DBPath As String
Dim sconnect As String
DBPath = "\\abc\Quality Report.xlsx"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
cnn.Open sconnect
strSQL = "SELECT * FROM [Error_Log$] WHERE "
strWhere = ""
BuildWhere strWhere, cboprocess.Text, "Process"
BuildWhere strWhere, cboaudittype.Text, "Audit_Type"
BuildWhere strWhere, cbouser1.Text, "User_Name"
BuildWhere strWhere, cborptmgr.Text, "Reporting_Manager"
BuildWhere strWhere, cbotranstyp.Text, "Transaction_Type"
BuildWhere strWhere, cboperiod.Text, "Period"
BuildWhere strWhere, cbolocation.Text, "Location"
BuildWhere strWhere, cbofatnfat.Text, "Fatal_NonFatal"
BuildWhere strWhere, cbostatus.Text, "Remarks"
If txtfromauditdt.Text <> "" Then
strWhere = strWhere & IIf(strWhere <> "", " AND ", "") & "[Audit_Date] BETWEEN '" & _
txtfromauditdt.Text & "' AND #" & Format(Date, "mm/dd/yyyy") & "# "
End If
strSQL = strSQL & strWhere
Debug.Print strSQL
Set rs.ActiveConnection = cnn
rs.Open strSQL, cnn
Sheet1.Range("A42").CopyFromRecordset rs
rs.Close
cnn.Close
End Sub
Sub BuildWhere(ByRef strWhere As String, v As String, fld As String)
If v <> "" Then
strWhere = strWhere & IIf(strWhere <> "", " AND ", "") & _
"[" & fld & "] = '" & v & "'"
End If
End Sub
答案 1 :(得分:0)
最后发现错误原因......
实际上,错误是由于我在编码中使用的引号'13-nov-15'
,但同样在#13-nov-15#
之后替换。它奏效了。
根据@Williams的回答,只有一次针对另一个日期进行了修正,但是参数日期报价即将到来,但现在它的工作很酷。感谢蒂姆·威廉姆斯......帮助很多......
答案 2 :(得分:0)
您用单引号'
包围了第一个文本日期,这将使其成为一个字符串。您需要使用数字符号#
,如下所示:
Or cbostatus.Text <> "" Then
strSQL = strSQL & " AND [Audit_Date] BETWEEN #" & txtfromauditdt.Text & "# AND GETDATE()"
Else
strSQL = strSQL & " [Audit_Date] BETWEEN #" & txtfromauditdt.Text & "# AND GETDATE()"
End If