Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MS Access VBA将Access数据库中满足多种条件的记录导出到Excel_Excel_Vba_Ms Access - Fatal编程技术网

MS Access VBA将Access数据库中满足多种条件的记录导出到Excel

MS Access VBA将Access数据库中满足多种条件的记录导出到Excel,excel,vba,ms-access,Excel,Vba,Ms Access,我是MS Access VBA的业余爱好者,我想向您寻求帮助 我在Access中有MyTable,有许多字段,其中5个是: [Date] format DD/MM/YYYY HH:MM:SS [Priority] ("Urgent", "Normal") [MOC] ("Yes","No") [AffectProduction]

我是MS Access VBA的业余爱好者,我想向您寻求帮助

我在Access中有MyTable,有许多字段,其中5个是:

[Date]                format DD/MM/YYYY HH:MM:SS
[Priority]            ("Urgent", "Normal")
[MOC]                 ("Yes","No")
[AffectProduction]    ("Yes","No")
[Status]              (" ","Following","Closed","Cancelled")
我有一个用户表单,用于选择要导出的条件,其中有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")
我想导出上面表格中选择的所有符合条件的记录。选择所有表示不使用该字段的筛选器

我可以做if或select案例陈述,但案例太多了,有什么方法可以帮助我吗。下面是我的一些nope代码的优先级和状态。 请帮我做得更好!非常感谢

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

为此,我建议在VBA中使用DAO与SQL相结合 示例集db=database.openrecordsetSELECT*FROM表,dbdynaset
然后在Excel工作表snamheet.Range.copyfromrecordset db中忘记了在sql条件中包含WHERE 从表中选择*,其中[]=…

@Trung-Nguyen 代码:


希望这有帮助;0

查看您是否无法基于优先级和状态生成查询?然后,您可以使用返回的记录集来填充工作表,而不是像@June7所说的那样检查每个记录,使用类似于发布的链接的代码为记录集构建SQL,然后使用CopyFromRecordset将整个数据集转储到Excel中。谢谢@June7和Applecore。我会尝试这样做。这不是一个真正的答案,只添加了评论中的内容。你能解释一下@Boni的更多细节吗?谢谢hanks@Boni,我已经有了代码,但我只是发布了选择案例的代码
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