Run time Error 3709

hsk

Well-known Member
Joined
Oct 19, 2006
Messages
567
Code:
Sub getdata()
cur_mon = Date - (Weekday(Date, 2) - 1)
cur_fri = Date + (5 - Weekday(Date, 2))

mondt = Year(cur_mon) & "-" & Day(cur_mon) & "-" & Month(cur_mon) & " 00:00:00"
fridt = Year(cur_fri) & "-" & Day(cur_fri) & "-" & Month(cur_fri) & " 00:00:00"

mydb = ............... (my sheet name)
mypath = ...... (path C:\ ...etc) & mydb

' Connet to database
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
With cn
    .Provider = "MSDASQL"
    .ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & "DBQ=" & mypath & "; ReadOnly=False;"
    .Open
End With

Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM [Sheet1$]"
cn.Close
Set cn = Nothing
End Sub


I am trying to query a sheet , getting run time error 3709 on the line rs.Open.
any help ?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
oohh, thanks .....

It's working now ....

When I display rs.RecordCount it displays -1 ...

My sheet is having records ...

When i tried -
rs.Open "SELECT * FROM [Sheet1$]", cn, adOpenStatic, adLockReadOnly

rs.RecordCount is zero ....

Is it not selecting any records?
 
Upvote 0
You don't appear to be doing anything with the recordset returned? If you want to copy it to a sheet the easiest way is to use the CopyFromRecordset method:

Code:
Activesheet.Range("A2").CopyFromRecordset rs
 
Upvote 0
Thanks Richard,

Actually my requirement is .....

- Fetch Records from DB with diff criteria
(want to select few fields not select *)
- Reformat Record and put the values in sheet
(I already have a template, just want to put the values from cursor into it)
I would want to handle each field in each record individually, as i may add few more validations before putting it in template.

Also
I want to add WHERE clause to this query on some Date Fields.

e.g. I am having a column as Planned Date (in mydb)-
value is 16th Nov.
The cell is in Custom format "m\d\yyyy h:mm"
Hence the date appears as "11/16/2007 0:00"
While the Formula bar shows "11/16/2007 12:00:00 AM"

Now how can i put sql query which will fetch record for planned date = 16 Nov.
 
Upvote 0
You need to write the SQl as:

Code:
"SELECT * FROM [Sheet1$] WHERE DateField = #11/16/2007#"

Amend the DateField for whatever the column header name is that contains the date you want to filter on.
 
Upvote 0
Finally ...... I get that working !!!

Thanks Richard, thanks a lot !!
 
Upvote 0

Forum statistics

Threads
1,214,399
Messages
6,119,279
Members
448,884
Latest member
chuffman431a

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