I am amateur in MS Access VBA and I would like to ask you for help.
I have MyTable in Access, with many fields, 5 of them are:
[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
[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. 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 [] =...
@Trung Nguyen Code:
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)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.