UseVariablesInSQL in access2base

Discuss the database features
Post Reply
geom
Posts: 1
Joined: Sun Nov 06, 2022 1:57 pm

UseVariablesInSQL in access2base

Post by geom »

I'm new to OpenOffice Base and find the extension access2base very usefull since I was programming Access DB a while ago.
I'm trying now to implement the UseVariablesInSQL as described in the http://www.access2base.com/access2base. ... lesInSQL]].

Code: Select all

Sub updateArt_click(e as Object)
    Dim rs As Object
    Dim frm As Object
    Dim sName As String  'search Parameter
    Set frm = Application.Forms("frm_Etiketten_drucken")
    sName = frm.Controls("cmbSorte").Value
    MsgBox sName   'shows the selected value from cmbSorte
    
    Set rs = Application.CurrentDb().RunSQL("SELECT art FROM artikel WHERE sorte_de = " & sName )
    MsgBox rs  'Returns Error#1523 
    
    'further code will follow, when the RunSQL-code works
End Sub
as marked in the code, the RunSQL code gives an Error:

Code: Select all

Error #1523 (SQL Error, SQL statement = 'SELECT art FROM artikel WHERE sorte_de = Bittenfelder') aufgetreten in ein Funktionsaufruf 'Database.RunSQL'
As far as I understand is this the cause of the missing quotes around the string 'Bittenfelder'?
In the original description on access2base the following code is used to add quotes :

Code: Select all

Sub updateArt_click(e as Object)
    Dim rs As Object
    Dim frm As Object 
    Dim sName As String  'search Parameter
    Const cstQuote = "'"		'	Single quote
    Set frm = Application.Forms("frm_Etiketten_drucken")
    sName = Join(Split(frm.Controls("cmbSorte").Value, cstQuote), cstQuote & cstQuote)   'Replace single ' by ' '
    MsgBox sName  'shows the selected value from cmbSorte
    Set rs = Application.CurrentDb().RunSQL("SELECT art FROM artikel WHERE sorte_de = " & cstQuote & sName & cstQuote)
    MsgBox rs
    
    'further code will follow, when the RunSQL-code works
End Sub
In this case I get the following error: No access to object. Incorrect use of object.
I've tried already all possible variations to quote out the string, but without success.
OpenOffice 4.1.13 with MacOS 12.6.1
Bidouille
Volunteer
Posts: 577
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: UseVariablesInSQL in access2base

Post by Bidouille »

geom wrote: Sun Nov 06, 2022 5:17 pm SQL Error, SQL statement = 'SELECT art FROM artikel WHERE sorte_de = Bittenfelder'
Did you understand that WHERE clause must be quoted (") with string?
eremmel
Posts: 1080
Joined: Tue Dec 30, 2008 1:15 am

Re: UseVariablesInSQL in access2base

Post by eremmel »

This line:
sName = Join(Split(frm.Controls("cmbSorte").Value, cstQuote), cstQuote & cstQuote) 'Replace single ' by ' '
is used to replace a single quote into a double single quote in case the Value contains a single quote. Is there error in this line?

But what happens when you use:

Code: Select all

Set rs = Application.CurrentDb().RunSQL("SELECT art FROM artikel WHERE sorte_de = '" & sName & "'")
Note the added single quotes.

Keep in mind that there are many type of quotes. Sometimes quotes are transformed by some MS editors into the wrong types. So be careful. But the code you copied here looks OK.

Note: In the page you are referring to, they use [ ] around field names in the where clause. I just noticed, not sure if this is mandatory...
It's Microsoft marketing that tells you computers are qualified for non-technicians
W11 22H2 (build 22621), LO 7.4.2.3(x64)
Post Reply