Excel macro to query more than one criteria - ADODB Connection

Lee.

Board Regular
Joined
Dec 15, 2012
Messages
170
Hi,
Is it possible for a excel macro to query an Access database with more than one criteria and download it to excel?

I've done it so it downloads based on one criteria, but I want to to download based on Person as well as date, so for example it will return the work for John Smith on the 10th December 2013.

This is the query code I have which works fine for one criteria

Code:
    sSQL = "SELECT * FROM workflowdata WHERE Date ='" & ShDest.Range("K1").Value & "'"


This is all of the code I've got so far

Code:
Sub ReturnWork()
   Dim cnn As ADODB.Connection    
Dim rst As ADODB.Recordset
    Dim fld As ADODB.Field
    Dim MyConn
    Dim i As Long
    Dim ShDest As Worksheet
    Dim sSQL As String
    
    Set ShDest = Sheets("Workflow")


    sSQL = "SELECT * FROM workflowdata WHERE Date ='" & ShDest.Range("K1").Value & "'"
    Set cnn = New ADODB.Connection
    MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
    
    With cnn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Properties("Jet OLEDB:Database Password") = "test"
        .Open MyConn
    End With


    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseServer
    rst.Open Source:=sSQL, ActiveConnection:=cnn, _
             CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, _
             Options:=adCmdText
    
    'clear existing data on the sheet
    ShDest.Activate
    Range("A1").CurrentRegion.Clear
    
    'create field headers
    i = 0
    With Range("A1")
        For Each fld In rst.Fields
            .Offset(0, i).Value = fld.Name
            i = i + 1
        Next fld
    End With
     
    'transfer data to Excel
    Range("A2").CopyFromRecordset rst
    
    ' Close the connection
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
End Sub

Thanks,
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Dont you just need to add to your WHERE clause, for example:

Rich (BB code):
sSQL = "SELECT * FROM workflowdata WHERE Date ='" & ShDest.Range("K1").Value & "' AND NameofPersonfield = 'John Smith'"

Obviously this NameofPersonfield needs to be changed to the actual name of the field.
 
Upvote 0

Forum statistics

Threads
1,203,399
Messages
6,055,175
Members
444,767
Latest member
bryandaniel5

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top