Sharpefiction
New Member
- Joined
- Sep 29, 2010
- Messages
- 33
Hi all,
I've been browsing this site and using suggestions/tips for ages and until now, I've always found the answer I needed..
I have a huge Database I want to get data from just by using VBA coding in Excel. I know how (or at least have some coding from a course I attended) to get the data from a table, however I need to specify that I only want data from X date to Y date. For example, get all data from 01 Aug 2010 to 31 Aug 2010.
Unfortunately it's this part I'm unsure of. I don't really deal with Access too much, so I'm a bit of newb when it comes to stuff like that. I'm sure it's not much extra coding I need, but I'm hoping someone out there can help me.. Please?
The coding I have to get all data is:
Option Explicit
Option Compare Text
Dim theSales As New Collection
Dim aSale As Sale
Sub GetData()
Const ConnString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=z:\Northwind For Excel.mdb;Persist Security Info=False"
Const SQL As String = "SELECT * FROM qryOrdersForExcel"
' * means all info in field
Dim Con1 As ADODB.Connection
' sets a variable for connection
Dim RecordSet As ADODB.RecordSet
' sets which table/query in database will be referencing
Set Con1 = New ADODB.Connection
Con1.ConnectionString = ConnString
Con1.Open
Set RecordSet = New ADODB.RecordSet
Call RecordSet.Open(SQL, Con1)
With RecordSet
If .BOF And .EOF Then
MsgBox "There are no records"
Exit Sub
End If
' checks to see if the end of the record & beginning of the record are the same
.MoveFirst
Call ClearSalesCollection
Do While Not .EOF
Set aSale = New Sale
aSale.EmployeeName = .Fields("LastName") ' sets name of field in brackets
aSale.CustomerName = .Fields("CompanyName")
aSale.ProductName = .Fields("ProductName")
aSale.DateOfSale = .Fields("OrderDate")
aSale.PricePerUnit = .Fields("UnitPrice")
aSale.OrderQuantity = .Fields("Quantity")
aSale.OrderNumber = CStr(.Fields("OrderLine"))
Call theSales.Add(aSale, aSale.OrderNumber)
.MoveNext
Loop
End With
Con1.Close
' closes the connection to the database
End Sub
Thanks in advance
I've been browsing this site and using suggestions/tips for ages and until now, I've always found the answer I needed..
I have a huge Database I want to get data from just by using VBA coding in Excel. I know how (or at least have some coding from a course I attended) to get the data from a table, however I need to specify that I only want data from X date to Y date. For example, get all data from 01 Aug 2010 to 31 Aug 2010.
Unfortunately it's this part I'm unsure of. I don't really deal with Access too much, so I'm a bit of newb when it comes to stuff like that. I'm sure it's not much extra coding I need, but I'm hoping someone out there can help me.. Please?
The coding I have to get all data is:
Option Explicit
Option Compare Text
Dim theSales As New Collection
Dim aSale As Sale
Sub GetData()
Const ConnString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=z:\Northwind For Excel.mdb;Persist Security Info=False"
Const SQL As String = "SELECT * FROM qryOrdersForExcel"
' * means all info in field
Dim Con1 As ADODB.Connection
' sets a variable for connection
Dim RecordSet As ADODB.RecordSet
' sets which table/query in database will be referencing
Set Con1 = New ADODB.Connection
Con1.ConnectionString = ConnString
Con1.Open
Set RecordSet = New ADODB.RecordSet
Call RecordSet.Open(SQL, Con1)
With RecordSet
If .BOF And .EOF Then
MsgBox "There are no records"
Exit Sub
End If
' checks to see if the end of the record & beginning of the record are the same
.MoveFirst
Call ClearSalesCollection
Do While Not .EOF
Set aSale = New Sale
aSale.EmployeeName = .Fields("LastName") ' sets name of field in brackets
aSale.CustomerName = .Fields("CompanyName")
aSale.ProductName = .Fields("ProductName")
aSale.DateOfSale = .Fields("OrderDate")
aSale.PricePerUnit = .Fields("UnitPrice")
aSale.OrderQuantity = .Fields("Quantity")
aSale.OrderNumber = CStr(.Fields("OrderLine"))
Call theSales.Add(aSale, aSale.OrderNumber)
.MoveNext
Loop
End With
Con1.Close
' closes the connection to the database
End Sub
Thanks in advance