|
-
Feb 2nd, 2015, 11:40 AM
#1
Thread Starter
Addicted Member
[RESOLVED] VB6 ADO error when adding WHERE clause in query
I have a csv file with the following information:
Code:
NOMBRE,EDAD,FECHA
MARIO,30,24/12/2002
MARIA,90,24/12/2001
PEDRO,10,24/12/2004
Then I want to interact with it using ADO like this (I've added Microsoft ActiveX Data Objects 2.0 Library reference):
Code:
Dim oConn As New ADODB.Connection
Dim oRS As New ADODB.Recordset
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Environ("temp") & ";" _
& "Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"
Set oRS = oConn.Execute("select * from datos.csv")
Dim ofield As ADODB.Field
Do Until oRS.EOF
For Each ofield In oRS.Fields
Debug.Print "Field Name = " & ofield.Name & " Field Value = " & ofield.Value
Next ofield
oRS.MoveNext
Loop
It works! Returns:
Code:
Field Name = NOMBRE Field Value = MARIO
Field Name = EDAD Field Value = 30
Field Name = FECHA Field Value = 24/12/2002
Field Name = NOMBRE Field Value = MARIA
Field Name = EDAD Field Value = 90
Field Name = FECHA Field Value = 24/12/2001
Field Name = NOMBRE Field Value = PEDRO
Field Name = EDAD Field Value = 10
Field Name = FECHA Field Value = 24/12/2004
But If I try to do a query like this:
Code:
SELECT EDAD FROM datos.csv WHERE NOMBRE='MARIO'
I have the following error:
Code:
-2147467259 (800040005) Error in 'Execute' method of '_Connection' object.
What am I dismissing? The only thing I've done is adding a 'where' clause in the query...
If I compile the code, the same error appears:
Last edited by *PsyKE1*; Feb 2nd, 2015 at 01:44 PM.
-
Feb 2nd, 2015, 11:49 AM
#2
Re: VB6 ADO error when adding WHERE clause in query
Does bracketing the table name resolve the problem?
Code:
SELECT EDAD FROM [datos.csv] WHERE NOMBRE='MARIO'
-
Feb 2nd, 2015, 11:54 AM
#3
Thread Starter
Addicted Member
Re: VB6 ADO error when adding WHERE clause in query
Hi LaVolpe,
Thank you, but I'm afraid it returns the same error.
-
Feb 2nd, 2015, 11:54 AM
#4
Thread Starter
Addicted Member
Re: VB6 ADO error when adding WHERE clause in query
Hi LaVolpe,
Thank you, but I'm afraid it returns the same error.
-
Feb 2nd, 2015, 12:47 PM
#5
Re: VB6 ADO error when adding WHERE clause in query
Just guessing here. I'm not on a VB machine & I've used similar code in dozens of projects to open/query csv files.
One thing you are doing that I don't. I do not use .Execute to return recordsets only to execute update/delete queries. Suggest using oRS.Open vs oCnn.Execute. Also pick the type of recordset to return in the .Open command, i.e., OpenDynamic.
-
Feb 2nd, 2015, 01:21 PM
#6
Thread Starter
Addicted Member
Re: VB6 ADO error when adding WHERE clause in query
Thanks,
following with your advice, this is my new code:
Code:
Dim oConn As New ADODB.Connection
Dim oRS As New ADODB.Recordset
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Environ("temp") & ";" _
& "Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"
oRS.Open "SELECT EDAD FROM [datos.csv] WHERE NOMBRE='MARIO'", oConn, adOpenDynamic, adLockReadOnly
Dim ofield As ADODB.Field
Do Until oRS.EOF
For Each ofield In oRS.Fields
MsgBox "Field Name = " & ofield.Name & " Field Value = " & ofield.Value
Next ofield
oRS.MoveNext
Loop
But returns:
Code:
-2147467259 (800040005) Error in 'Open' method of '_Recordset' object.
-
Feb 2nd, 2015, 01:35 PM
#7
Re: VB6 ADO error when adding WHERE clause in query
Is your system locale set for Spanish? If so, bracket the field names too. On English systems, Name is a key word and without brackets, queries will fail. Similar with Date and other keywords used by SQL.
-
Feb 2nd, 2015, 02:20 PM
#8
Thread Starter
Addicted Member
Re: VB6 ADO error when adding WHERE clause in query
Yes, is set in spanish.
But 'NOMBRE' or 'EDAD' are not keywords like 'NAME' or 'DATE'.
Anyway I've changed it to:
Code:
oRS.Open "SELECT [EDAD] FROM [datos.csv] WHERE [NOMBRE]='MARIO'", oConn, adOpenDynamic, adLockReadOnly, adCmdText
And now the error is:
Code:
2147217904 (8004e10) There are no specified values for some of the required parameters.
Last edited by *PsyKE1*; Feb 2nd, 2015 at 03:35 PM.
-
Feb 2nd, 2015, 02:31 PM
#9
Re: VB6 ADO error when adding WHERE clause in query
 Originally Posted by *PsyKE1*
Yes, is set in spanish.
But 'NOMBRE' or 'EDAD' are not keywords like 'NAME' or 'DATE'.
And now the error is:
Code:
2147217904 (8004e10) There are no specified values for some of the required parameters.
Well it was worth a shot, didn't know if locale's would be a factor or not.
Since I really can't play along (i.e., test stuff myself), I'll back off and hopefully others may see the problem. In the mean time, have you looked at the FAQ portion of the forums regarding DBs?
-
Feb 2nd, 2015, 03:00 PM
#10
Re: VB6 ADO error when adding WHERE clause in query
I would suggest changing your reference from ADO 2.0 to ADO 2.5 as that is what most of us would be using. I haven't used 2.0 in many years can't remember what it does or does not support nor what bugs were in it. Try using 2.5 and see if you still get the error.
-
Feb 2nd, 2015, 03:36 PM
#11
Thread Starter
Addicted Member
Re: VB6 ADO error when adding WHERE clause in query
I've change the reference tu ADO 2.5 and now it works! THank you very much, DataMiser!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|