VBA sql query filter problem.

kashif.special2005

Active Member
Joined
Oct 26, 2009
Messages
443
Hi,

I am using below code to filter the result as per selection from the user form, but it is giving an invalid sql, please help me to achieve this task.

Code:
Sub EmpFilter()




Dim objConnection As ADODB.Connection
Dim objRecordset As ADODB.Recordset
Dim VarMyData() As Variant
Dim Inti As Integer
Dim StrSqlWhere As String




Set objConnection = New ADODB.Connection
Set objRecordset = New ADODB.Recordset


objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=C:\Users\Kashif\Desktop\MyData.xlsm;" & _
        "Extended Properties=""Excel 12.0;HDR=Yes;"";"


If UserForm1.lstProject.Value <> "All" Then
    StrSqlWhere = "Where [Projects]='" & UserForm1.lstProject.Value & "'"
Else
    StrSqlWhere = "Where [Projects]='*'"
End If


If UserForm1.cmbName.Value <> vbNullString Then
    StrSqlWhere = StrSqlWhere & " And [Name]='" & UserForm1.cmbName.Value & "'"
Else
    StrSqlWhere = StrSqlWhere & " And [Name]='*"
End If




objRecordset.Open "Select [Employee] FROM [Sheet1$A1:BW4609] " & StrSqlWhere & " group by [Employee] order by [Employee]", _
    objConnection, adOpenStatic, adLockOptimistic, adCmdText


    ReDim VarMyData(0 To objRecordset.RecordCount - 1, 1 To 1)  [I][B]'Error Code Here "Subscript Out of Range"[/B][/I]


    For Inti = 0 To objRecordset.RecordCount - 1
        VarMyData(Inti, 1) = objRecordset.Fields(0)
        objRecordset.MoveNext
    Next Inti
    
    UserForm1.lstEmployee.List = VarMyData


End Sub

Thanks
Kashif
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi
but it is giving an invalid sql
It is not true. Simple add before objRecordset.Open
Code:
objRecordset.CursorLocation = adUseClient
Without it recordset cannot return record count.
Regards,
 
Upvote 0
Hi anvg,

Thanks for reply, however it is giving the error.

Is this query is correct.

Select [Employee] FROM [Sheet1$A1:BW4609] Where [Projects]='*' And [Name]='*' group by [Employee] order by [Employee]

If not how can I write query for the condition, for the below condition.

If UserForm1.lstProject.Value <> "All" Then
StrSqlWhere = "Where [Projects]='" & UserForm1.lstProject.Value & "'"
Else
StrSqlWhere = "Where [Projects]='*'"
End If



If UserForm1.cmbName.Value <> vbNullString Then
StrSqlWhere = StrSqlWhere & " And [Name]='" & UserForm1.cmbName.Value & "'"
Else
StrSqlWhere = StrSqlWhere & " And [Name]='*"
End If

Thanks
Kashif
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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