[英]MS Access VBA Export records in Access database to Excel that meet multiple conditions
I am amateur in MS Access VBA and I would like to ask you for help.我是 MS Access VBA 的业余爱好者,我想请你帮忙。
I have MyTable in Access, with many fields, 5 of them are:我在 Access 中有 MyTable,有很多字段,其中 5 个是:
[Date] format DD/MM/YYYY HH:MM:SS
[Priority] ("Urgent", "Normal")
[MOC] ("Yes","No")
[AffectProduction] ("Yes","No")
[Status] (" ","Following","Closed","Cancelled")
I have a userform for choosing conditon what to export, with have 6 fields我有一个用于选择要导出的条件的用户表单,有 6 个字段
[FromDate] textbox with Date Picker
[ToDate] textbox with Date Picker
[Priority] Option group ("Urgent", "Normal", "All")
[MOC] Option group ("Yes","No", "All")
[AffectProduction] Option group ("Yes","No", "All")
[Status] Option group (" ","Following","Closed","Cancelled","All")
I want to export all record that meet condition have been chosen on above form.我想导出所有符合条件的记录已在上面的表格中选择。 (Choose "All" that mean no filter with that field) (选择“全部”表示没有该字段的过滤器)
I can make if or select case statement but it too many case, is there any way to help me.我可以制作 if 或 select 案例陈述,但案例太多了,有什么办法可以帮助我。 Below is some of my nope code only Priority and Status.下面是我的一些代码,只有优先级和状态。 Please help me to do the better way!请帮助我做得更好! Many thanks非常感谢
Do While Not rs.EOF
If ((rs!Date >= dFromDate) And (rs!Date <= dToDate)) Then
Select Case True
Case ((intPriority = 1) And (intStatus = 1))
If ((rs!Priority = "Urgent") And (rs!Status = "Following")) Then
Call ExportData(rs, xlSheet, i)
End If
Case ((intPriority = 1) And (intStatus = 2))
If ((rs!Priority = "Urgent") And (rs!Status = "Closed")) Then
Call ExportData(rs, xlSheet, i)
End If
Case ((intPriority = 1) And (intStatus = 3))
If (rs!Priority = "Urgent") Then
Call ExportData(rs, xlSheet, i)
End If
Case ((intPriority = 2) And (intStatus = 1))
If ((rs!Priority = "Normal") And (rs!Status = "Following")) Then
Call ExportData(rs, xlSheet, i)
End If
Case ((intPriority = 2) And (intStatus = 2))
If ((rs!Priority = "Normal") And (rs!Status = "Closed")) Then
Call ExportData(rs, xlSheet, i)
End If
Case ((intPriority = 2) And (intStatus = 3))
If (rs!Priority = "Normal") Then
Call ExportData(rs, xlSheet, i)
End If
Case ((intPriority = 3) And (intStatus = 1))
If (rs!Status = "Following") Then
Call ExportData(rs, xlSheet, i)
End If
Case ((intPriority = 3) And (intStatus = 2))
If (rs!Status = "Closed") Then
Call ExportData(rs, xlSheet, i)
End If
Case ((intPriority = 3) And (intStatus = 3))
Call ExportData(rs, xlSheet, i)
End Select
i = i + 1
End If
rs.MoveNext
Loop
Forgot to include WHERE in sql condition SELECT * FROM table WHERE [] =...忘记在 sql 条件 SELECT 中包含 WHERE * FROM table WHERE [] =...
@Trung Nguyen Code: @Trung Nguyen 代码:
Dim record as DAO.Recordset
Dim db as DAO.Database
Set db = DBEngine.OpenDatabase ("C:\....")
Set record = db.OpenRecordset("SELECT ...",dbopenDynaset)
' see previous posting
Sheets("name of sheet").Range("A1") record
' Excel-sheet where you want to have data copied
' for the WHERE-condition more actions may be taken with WHERE cond1 AND/OR cond2
Hope this helps;0)希望这会有所帮助;0)
I would suggest to use DAO combined with SQL in VBA for this Example set db =database.openrecordset(SELECT * FROM table,dbdynaset) and then in Excel Sheets(namesheet).Range.copyfromrecordset db I would suggest to use DAO combined with SQL in VBA for this Example set db =database.openrecordset(SELECT * FROM table,dbdynaset) and then in Excel Sheets(namesheet).Range.copyfromrecordset db
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.