最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - MS Access SELECT query, cannot retrieve values - Stack Overflow

programmeradmin2浏览0评论

I'm using VBA for applications and trying to get some rows from a MS Access table.

I have this code:

KKS = "11LAB10"
id& = SQLOpen("DSN=MJC_Experiencia;DBQ=c:\equipamentos.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;",,3)

query_SQL = "SELECT * FROM my_table WHERE kks=" & Chr(39) & KKS & Chr(39)
msgbox query_SQL
qry& = SQLExecQuery(id&,querySQL)
i% = SQLBind(id&, b, 3)
i% = SQLBind(id&, b, 1)
i% = SQLBind(id&, b, 2)
i% = SQLBind(id&, b, 4)
l& = SQLRetrieve(id&, c)

msgbox "KKS = " & c(0,0)

The first msgbox is displaying this:

SELECT * FROM my_table WHERE kks="'11LAB10'"

But I get an error

9 - Subscript out of range

(referring to c(0,0))

If I substitute the query_SQL with this:

query_SQL = "SELECT * FROM my_table WHERE kks='11LAB10'"

Then the code works, and I get the correct value, in the second msgbox.

What am I doing wrong with the variable KKS in the first query?

I'm using VBA for applications and trying to get some rows from a MS Access table.

I have this code:

KKS = "11LAB10"
id& = SQLOpen("DSN=MJC_Experiencia;DBQ=c:\equipamentos.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;",,3)

query_SQL = "SELECT * FROM my_table WHERE kks=" & Chr(39) & KKS & Chr(39)
msgbox query_SQL
qry& = SQLExecQuery(id&,querySQL)
i% = SQLBind(id&, b, 3)
i% = SQLBind(id&, b, 1)
i% = SQLBind(id&, b, 2)
i% = SQLBind(id&, b, 4)
l& = SQLRetrieve(id&, c)

msgbox "KKS = " & c(0,0)

The first msgbox is displaying this:

SELECT * FROM my_table WHERE kks="'11LAB10'"

But I get an error

9 - Subscript out of range

(referring to c(0,0))

If I substitute the query_SQL with this:

query_SQL = "SELECT * FROM my_table WHERE kks='11LAB10'"

Then the code works, and I get the correct value, in the second msgbox.

What am I doing wrong with the variable KKS in the first query?

Share Improve this question edited Feb 16 at 6:43 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 15 at 22:48 Mario CordeiroMario Cordeiro 1254 silver badges16 bronze badges 1
  • Try this query_SQL = "SELECT * FROM my_table WHERE kks = '" & Replace(KKS, "'", "''") & "'" and see if it works, I can add an answer if so. – Je Campos Commented Feb 15 at 23:10
Add a comment  | 

2 Answers 2

Reset to default 1

to change this:

 SELECT * FROM my_table WHERE kks="'11LAB10'"

to this

 SELECT * FROM my_table WHERE kks='11LAB10'

use the following visualbasic

 query_SQL = "SELECT * FROM my_table WHERE kks='" & KKS & "'"

Today I finally realize that the KKS variable include the quotation marks itself, that´s why the query_sql don't work. What I do is to get rid of the quotation marks with this:

KKS = Mid(KKS,2, Len(KKS) -2)

Now it's working, thanks all for your help.

发布评论

评论列表(0)

  1. 暂无评论