Option Explicit
'This are the field names from the Order table in the OrderDatabase.mdb database.
'OrderNumber
'OrderDate
'RequiredDate
'ShippedDate
'Freight
'ShipVia
'ShipCountry
'ShipName
'ShipAddress
'ShipCity
'ShipRegion
'ShipPostalCode
'NOTE :
'First line: Path/name of the Access file, Table name
'Second-Eighth line: You can fill in seven criteria,
'and if you not fill in any criteria it return all records
'
'The first three criteria are only for Text fields
'The fourth and fifth are for numbers fields
'The sixth and seventh are for date fields
'
'Line nine: Destination sheet/range
'Line ten: Which field names (* = all), Copy field names, clear all cells on Destination sheet first
'Instead of enter field values in the code you can also use a cell value
' "ShipVia", "=", Sheets("Sheet1").Range("A2").Value
'Note: look at the other example workbook where you save your different criteria (very easy)
'You can use Data>Validation cells in that workbook
Sub Test1()
'This example retrieves the data for the records in which ShipCountry = Germany
GetDataFromAccess ThisWorkbook.Path & "/OrderDatabase.mdb", "Orders", _
"ShipCountry", "=", "Germany", _
"", "=", "", _
"", "=", "", _
"", ">", "", _
"", "<", "", _
"", ">=", "", _
"", "<=", "", _
Sheets("test").Range("A8"), _
"*", True, True
End Sub
Sub Test2()
'This example retrieves also the data for the records in which ShipCountry = Germany
'It only retrieves this four fields: OrderNumber, ShipName, ShipAddress, ShipPostalCode
'I changed the "*" for WhichFields in the code to the names of the fields
GetDataFromAccess ThisWorkbook.Path & "/OrderDatabase.mdb", "Orders", _
"ShipCountry", "=", "Germany", _
"", "=", "", _
"", "=", "", _
"", ">", "", _
"", "<", "", _
"", ">=", "", _
"", "<=", "", _
Sheets("test").Range("A8"), _
"OrderNumber, ShipName, ShipAddress, ShipPostalCode", True, True
End Sub
Sub Test3()
'This example retrieves the data for the records in which
'ShipCountry = Germany and ShipVia = Speedy Express
GetDataFromAccess ThisWorkbook.Path & "/OrderDatabase.mdb", "Orders", _
"ShipCountry", "=", "Germany", _
"ShipVia", "=", "Speedy Express", _
"", "=", "", _
"", ">", "", _
"", "<", "", _
"", ">=", "", _
"", "<=", "", _
Sheets("test").Range("A8"), _
"*", True, True
End Sub
Sub Test4()
'This example retrieves the data for the records in which
'ShipCountry = Germany and ShipVia = Speedy Express
'and Freight = between 100 and 300
GetDataFromAccess ThisWorkbook.Path & "/OrderDatabase.mdb", "Orders", _
"ShipCountry", "=", "Germany", _
"ShipVia", "=", "Speedy Express", _
"", "=", "", _
"Freight", ">", "100", _
"Freight", "<", "300", _
"", ">=", "", _
"", "<=", "", _
Sheets("test").Range("A8"), _
"*", True, True
End Sub
Sub Test5()
'This example retrieves the data for the records in which
'ShipCountry = Germany and ShipVia = Speedy Express
'and ShippedDate = between 1/1/1998 and 3/1/1998
GetDataFromAccess ThisWorkbook.Path & "/OrderDatabase.mdb", "Orders", _
"ShipCountry", "=", "Germany", _
"ShipVia", "=", "Speedy Express", _
"", "=", "", _
"", ">", "", _
"", "<", "", _
"ShippedDate", ">=", "1/1/1998", _
"ShippedDate", "<=", "3/1/1998", _
Sheets("test").Range("A8"), _
"*", True, True
End Sub
Sub Test6()
'This example retrieves all records
GetDataFromAccess ThisWorkbook.Path & "/OrderDatabase.mdb", "Orders", _
"", "=", "", _
"", "=", "", _
"", "=", "", _
"", ">", "", _
"", "<", "", _
"", ">=", "", _
"", "<=", "", _
Sheets("test").Range("A8"), _
"*", True, True
End Sub