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

filtering sorting table

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

Filtering & sorting a table while accessing it

ResolvedVersion 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:

Change the Default Width of All Columns in Excel 2010

If you want to change the width of the columns in your Excel 2010 spreadsheet, making them either larger or smaller, here's how:

In the Cells group on the Home tab, click Format.

Hover over the section called Cell Size and a drop down list will appear, select Default Width from this list.

In the Standard Width dialog box, enter the size you want to set as the default width and click OK.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.