filtering sorting table

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Filtering & sorting a table while accessing it | Excel forum

Filtering & sorting a table while accessing it | Excel forum

resolvedResolved · Urgent Priority · Version 2003

Rob has attended:
Excel VBA Advanced course

Filtering & sorting a table while accessing it

I have recently attended the advanced VBA course with Stephen Williams and I have successfully accessed data from an Access database and moved it to a new sheet in a new workbook. My next big problem is to filter and sort the data before it is carried into the new sheet (per the example used in the course), as the database is largely not applicable to the dataset I am working with and is too large to for a 2003 excel sheet. The question is how do I filter the and sort the data, more specifiocally what is the form of the syntax and where is it presented? Here is an extract of my script.

Sub GetData()

Const CString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=X:\MAD\MAD2009.mdb;Persist Security Info=False"

Const SQL As String = _
"SELECT CompanyCode, AccountCode, AccountName, CentreCode, CentreName, Level3Code, Level3Desc, Type, YTD_Ccy FROM tblPvtLink"

Dim cnConnection As ADODB.Connection
Dim rstRecordset As ADODB.Recordset

Set cnConnection = New ADODB.Connection
cnConnection.ConnectionString = CString

cnConnection.Open

Set rstRecordset = New ADODB.Recordset

Call rstRecordset.Open(SQL, cnConnection)

With rstRecordset

If .BOF And .EOF Then
MsgBox "There are no records"
Exit Sub
End If

.MoveFirst

Call ClearCollection

Do While Not .EOF

Set aDataProd = New CODAProdDataSource

aDataProd.dbCompanyCode = .Fields("CompanyCode")
aDataProd.dbAccountCode = .Fields("AccountCode")
aDataProd.dbAccountName = .Fields("AccountName")
aDataProd.dbCentreCode = .Fields("CentreCode")
aDataProd.dbCentreName = .Fields("CentreName")
aDataProd.dbLevel3Code = .Fields("Level3Code")
aDataProd.dbLevel3Desc = .Fields("Level3Desc")
aDataProd.dbType = .Fields("Type")
aDataProd.dbYTD_Ccy = .Fields("YTD_Ccy")

Call theDataProd.Add(aDataProd)

.MoveNext

Loop

End With

cnConnection.Close

End Sub

RE: Filtering & sorting a table while accessing it

Hi Rob

Thank you for your question

To filter the data I suggest using a Where clause in the SQL. The syntax of this is typically

SELECT * FROM table Where field = value

You can also sort it by using the Order by clause

So

SELECT * FROM table Where field = value Order By Field

For example

SELECT * FROM Employees where Department = 'Maintenance' Order By FirstName


Hope this helps

Regards

Stephen

RE: Filtering & sorting a table while accessing it

Hi Stephen,

Thanks that works, but when I try to filter by more than one field say...

SELECT CompanyCode, CentreCode, Level3Code, YTD_Ccy FROM tblPvtLink WHERE CompanyCode = 'MAINCO' & CentreCode = '7302' ORDER BY Level3Code

I get an error so I'm assuming the error is to do with the sytax from the & sign...

Can you help?

Cheers
Rob

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Using Alt in Save Dialog Box

When you are saving (or opening) a file, try these;
(Note the numbers are on the keyboard not the numeric keypad)
1. Go to previous folder Alt+1
2. Go up one folder level Alt+2
3. Search the Web Alt+3
4. Delete selected file Alt+4 or Delete
5. Create a new folder Alt+5
6. Cycle through all views Alt+6 repeatedly
7. Display the Tools menu Alt+7

View all Excel hints and tips


Server loaded in 0.08 secs.