第2个回答 2015-01-28
Private Sub CommandButton2_Click()
'STATISTICS Accoding to custom code
Dim objData As Object
Dim strSQL As String
Dim blnStatus As Boolean
Dim intStatus As Integer
Dim lngCount As Long
Dim strBegin, strEnd As String
Dim qufen As String
Dim strCode As String
Dim row As Long
Dim row2 As Long
Dim col As Long
Dim col1, col2, col3, col4, col5, col6, col7, col8 As Single
col1 = 1
col2 = 2
col3 = 3
col4 = 4
col5 = 5
col6 = 6
col7 = 7
col8 = 8
qufen = "00" '伝票区分=标准発放=制品
strCode = ""
strBegin = dateBegin.Text
strEnd = dateEnd.Text
If strBegin = "" Or strEnd = "" Then
MsgBox ("Please input the begin and end date,then try again!")
Exit Sub
End If
intStatus = MsgBox("Ater clear the data of cell,then get the data from ORACLE database." & vbCrLf & vbCrLf & "Is it OK?", vbYesNo + vbDefaultButton2)
If intStatus <> vbYes Then
Exit Sub
End If
On Error GoTo Button1_Click_Error
With Sheets("Statistics")
row = 3
'Clear the data of statistics display
Do While .Cells(row, col1).Value <> ""
.Cells(row, col1).Value = "" 'custom code
.Cells(row, col2).Value = "" 'custom name
.Cells(row, col3).Value = "" 'amount
row = row + 1
Loop
strSQL = ""
strSQL = "SELECT"
strSQL = strSQL & " TRIM(I_CUSTOMER_CD) AS CUSTOMER_CD," & vbCrLf 'custom code
strSQL = strSQL & " SUM(I_AMT) AS AMT" & vbCrLf 'amount
strSQL = strSQL & " FROM T_SHIP_TR" & vbCrLf
strSQL = strSQL & " WHERE" & vbCrLf
strSQL = strSQL & " TO_CHAR(I_SHIP_DATE,'YYYYMMDD') >= '" & strBegin & "'" & vbCrLf 'start date
strSQL = strSQL & " AND TO_CHAR(I_SHIP_DATE,'YYYYMMDD') <= '" & strEnd & "'" & vbCrLf 'end date
strSQL = strSQL & " AND TRIM(I_SHIP_CLS) = '" & qufen & "'" & vbCrLf 'made products
strSQL = strSQL & " GROUP BY I_CUSTOMER_CD" & vbCrLf
strSQL = strSQL & " ORDER BY I_CUSTOMER_CD"
blnStatus = COM_CreateDynaset(strSQL, objData)
If Not blnStatus Then
Exit Sub
End If
If objData.RecordCount = 0 Then 'miss find data from database
MsgBox ("Could not find the data in the database!")
Exit Sub
End If
row = 3
Do While Not objData.EOF
strCode = Trim(COM_GetFieldString(objData.Fields("CUSTOMER_CD").Value))
.Cells(row, col1).Value = strCode 'export custom code to excel
With Sheets("codeAndName")
row2 = 1
Do While .Cells(row2, col1).Value <> ""
If strCode = .Cells(row2, col1).Value Then
Sheets("Statistics").Cells(row, col2).Value = .Cells(row2, col2).Value 'export custom name to excel
Exit Do
End If
row2 = row2 + 1
Loop
End With
.Cells(row, col3).Value = Trim(COM_GetFieldString(objData.Fields("AMT").Value)) 'export amount to excel
row = row + 1
objData.MoveNext
Loop
End With
'close database
objData.Close
Set objData = Nothing
'Sheets("Statistics").Select
'Range("A2").Select
'Success message
MsgBox ("Success to stastic the data of record!")
Exit Sub
Button1_Click_Error:
MsgBox ("Error happened," & vbCrLf & "Please make sure!")
End Sub