simple SELECT query to .mdb still fails :(

fedefring

New Member
Joined
Aug 27, 2018
Messages
8
Hi guys,
nice to meet you All!


I am struggling with Excel, trying to get data from an .mdb Access old db,
via SQL query "only".


I mean "only", cause I am able to get what I need by "looping" through records without
errors, but I assume using SQL is extremely better!


So, code you will see below, is ok in order to get connected to the db and
I can easily get recordset content by looping,
but I did a lot of tentatives with queries you will see and
in no one case (you see only two here) I was able to run correctly the query
cause I always get "query sintax error" when executed.


Where is my error ?


To be clear in advance, I cannot use ADO or DAO to connect, cause this is a very old
db and also a very old xls with dozens of macros with the same kind
of connection method you see below.


Thank you in advance,
Fred!


------------------
Private Function aggiorna_comm(stringa_connessione, valuex) As Boolean


On Error GoTo Err_aggiorna_comm


Dim db_destination As Database
Dim rs_destination As Recordset


Set db_destination = OpenDatabase(stringa_connessione)
Set rs_destination = db_destination.OpenRecordset(tabx)


1)
strQry = "SELECT * FROM '" & tabx & "' WHERE '" & fieldx & "' = '" & valuex & "'"
2)
strQry = "SELECT * FROM "&tabx&" WHERE "&fieldx&" = " & valuex & "' "


db_destination.Execute strQry


test = rs_destination!idComm


Err_aggiorna_comm_exit:
rs_destination.Close
Set rs_destination = Nothing


db_destination.Close
Set db_destination = Nothing
Exit Function


Err_aggiorna_comm:
MsgBox Error$
Resume Err_aggiorna_comm_exit


End Function
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Where are your values for tabx and fieldx
 
Upvote 0
Where are your values for tabx and fieldx

Hi Jim and thanks for support,

I am sorry, see the right copy and paste
as you imagine everything is passed by subs calling the function, bye :

Private Function aggiorna_comm(stringa_connessione, valuex, tabx, fieldx) As Boolean


On Error GoTo Err_aggiorna_comm


Dim db_destination As Database
Dim rs_destination As Recordset


Set db_destination = OpenDatabase(stringa_connessione)
Set rs_destination = db_destination.OpenRecordset(tabx)


1)
strQry = "SELECT * FROM '" & tabx & "' WHERE '" & fieldx & "' = '" & valuex & "'"
2)
strQry = "SELECT * FROM "&tabx&" WHERE "&fieldx&" = " & valuex & "' "


db_destination.Execute strQry


test = rs_destination!idComm


Err_aggiorna_comm_exit:
rs_destination.Close
Set rs_destination = Nothing


db_destination.Close
Set db_destination = Nothing
Exit Function


Err_aggiorna_comm:
MsgBox Error$
Resume Err_aggiorna_comm_exit


End Function
 
Upvote 0
What kind of values are valuex?

Since tabx is a table name and fieldx is a field name, they should not have any single-quotes around them. If they may have spaces, they will need square brackets around them.
If valuex is text, it should have single quotes around its value. If not, then it shouldn't.

If valuex text:
Code:
[COLOR=#333333]strQry = "SELECT * FROM " & tabx & " WHERE " & fieldx & " = '" & valuex & "'"[/COLOR]

If valuex numeric:
Code:
[COLOR=#333333]strQry = "SELECT * FROM " & tabx & " WHERE " & fieldx & " = " & valuex[/COLOR]
 
Upvote 0
Hi Joe,
valuex is a number and I tried both these without success, please see the strQry string result (before execute) :

strQry = "SELECT * FROM " & tabx & " WHERE " & fieldx & " = " & valuex
SELECT * FROM tTerminiPagamento WHERE TerminePagamento = 120

SELECT * FROM tTerminiPagamento WHERE [TerminePagamento] = 120

Error is 3065 "Cannot execute a select query"

I really do not know,
thanks!
 
Upvote 0
Why are you using Execute here?
Code:
db_destination.Execute strQry
Execute should only be used for action queries, and a SELECT query isn't an action query.
 
Upvote 0
And, if I try this way instead of db.execute:

case1)
strQry = "SELECT * FROM " & tabx & " WHERE " & fieldx & " = " & valuex

case2)
fieldx = "[" & fieldx & "]"
strQry = "SELECT * FROM " & tabx & " WHERE " & fieldx & " = " & valuex


Set rs_destination = db_destination.OpenRecordset(strQry)

I get error 3464

Thanks!
 
Upvote 0
That means there's a problem with the criteria.

What happens if you remove the criteria?
 
Upvote 0
this way works:

strQry = "SELECT * FROM " & tabx & " WHERE 1=1"
Set rs_destination = db_destination.OpenRecordset(strQry)


test = rs_destination(target_field)

but now... what ?

:) Thanks
 
Upvote 0
Ohhhh great!!

It was a text not a number and now after all these changes it works like a charm!!!

Thank you so much GUYS!!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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