公元 Sub get_data() Dim strSQL As String Dim cnn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim DBPath As String Dim s
公元公元 Sub get_data() Dim strSQL As String Dim cnn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim DBPath As String Dim s,sql,excel,vba,Sql,Excel,Vba,公元 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=" & DBPat
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
SELECT *
FROM [Error_Log$]
WHERE [Audit_Date] BETWEEN '16-Nov-2015' AND getdate()
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
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