我每天都需要为推销员做一份报告,我们有2个dbf文件来自我自动想要在Excel中生成报告。来自1 dbf文件的报告工作正常,但我不知道如何在VBA中加入2个dbf文件。
我必须遵循以下脚本:
选项明确
Sub ReadDBF()
Dim con As Object
Dim rs As Object
Dim DBFFolder As String
Dim FileName As String
Dim FileName1 As String
Dim sql As String
Dim myValues() As String
Dim i As Integer
Dim j As Integer
Application.ScreenUpdating = False
DBFFolder = ThisWorkbook.Path & "\"
FileName = "project1.dbf"
FileName1 = "project2.dbf"
On Error Resume Next
Set con = CreateObject("ADODB.connection")
If Err.Number <> 0 Then
MsgBox "Connection was not created!", vbCritical, "Connection error"
Exit Sub
End If
On Error GoTo 0
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBFFolder & ";Extended Properties=dBASE IV;"
sql = "SELECT project_id, COUNT(*) AS total, salesman, MAX(date) AS max_date, projectname FROM " & FileName & FileName1 & " where DateValue(datumtijd) = Date() and FileName.project_id = FileName1.project_id " & "group by project_id, salesman"
On Error Resume Next
Set rs = CreateObject("ADODB.recordset")
If Err.Number <> 0 Then
MsgBox "Connection was not created!", vbCritical, "Connection error"
Exit Sub
End If
On Error GoTo 0
rs.CursorLocation = 3
rs.CursorType = 1
rs.Open sql, con
ReDim myValues(rs.RecordCount, 20)
i = 1
If Not (rs.EOF And rs.BOF) Then
'Go to the first record.
rs.MoveFirst
Do Until rs.EOF = True
myValues(i, 1) = rs!project_id
myValues(i, 2) = rs!salesman
myValues(i, 3) = rs!Total
myValues(i, 4) = rs!max_date
myValues(i, 5) = rs!project
rs.MoveNext
i = i + 1
Loop
Else
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Application.ScreenUpdating = True
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
Sheet1.Activate
For i = 1 To UBound(myValues)
For j = 1 To 4
Cells(i + 1, j) = myValues(i, j)
Next j
Next i
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Columns("A:D").EntireColumn.AutoFit
Application.ScreenUpdating = True
MsgBox "The values were read from recordset successfully!", vbInformation, "Done"
End Sub
答案 0 :(得分:0)
仅通过将两个表一起添加为源,查询就不起作用。你想要实际得到的是什么?此外,您永远不应该连接字符串来构建查询。它们应该始终参数化。
现在,那就是说,你的语法是不正确的。您的变量称为&#34; FileName&#34;和&#34; FileName1&#34;,但您要查询的基础表是&#34; Project1&#34;和&#34; Project2&#34;分别。您应该了解别名以帮助简化查询,并学习正确的JOIN语法。
通过一个接一个地列出表,没有逗号会导致它失败。这是一个更准确的语法,并为可读性而格式化。然后我编辑了原始查询以匹配上下文。您还应始终从相应的表中限定字段名称,以便其他人尝试帮助了解事物的来源。在下面的示例中,我只使用GUESSED在哪个表中使用别名&#34; P1。&#34;和&#34; P2。&#34;分别。您可能需要更改它们。此外,因为&#34;日期&#34;可以解释为保留字,我已将其包装在[]中,但可能需要更改为刻度字符(在数字1旁边)``
select
P1.Project_ID,
COUNT(*) as Total,
P1.SalesMan,
MAX( P2.[Date] ) as Max_Date,
P1.ProjectName
from
Project1 P1
JOIN Project2 P2
on P1.Project_ID = P2.Project_ID
where
DateValue( P2.datumtijd ) = date()
group by
P1.Project_ID,
P1.SalesMan
JOIN子句标识各个列上两个表的BETWEEN关系。 WHERE子句是您要查找的其他条件。
sql = "SELECT project_id, COUNT(*) AS total, salesman, " & _
"MAX(date) AS max_date, projectname " & _
" FROM " & FileName & " P1 " & _
" JOIN " & FileName1 & " P2 ON P1.Project_ID = P2.Project_ID" & _
" where DateValue(datumtijd) = Date() " & _
" group by project_id, salesman"