Variable Inside the Access Pass Through Query - vba

I set a global variable in my program.
public this_is_global_var as integer
this_is_global_var=1
Then I use that variable inside my pass through query
Select * from oracle_table where id=this_is_global_var ;
But error shows "this_is_global_var: invalid identifier"
Please help.Thanks.

You can define placeholders for variable inside the query definition and replace it before execution.
qdfTemp.SQL = Replace(qdfMyQuery.SQL, "[this_is_global_var]", str(this_is_global_var))
and then execute temp query. Original query will be untouched.

If you want to use variables in your query you have to write it as a variable:
"SELECT * FROM oracle_table WHERE id = " & this_is_global_var

Related

How to use VBA variable for IN 'SourceDB' clause of MS-ACCESS query

I am trying to pass a vba string variable to an IN clause of a SQL statement in the query builder view.
the string is created by the following function:
Public Function GetBackEnd()
If Len(GetBackEnd) = 0 Then GetBackEnd = BackEnd
End Function
backend itself is derived from a dropdown box in userform, there are two entries in a table with two different addresses, one each for the live and developement DB's. The dropdown box sets the "environment" variable upon selection.
Property Get BackEnd() As String
Select Case Environment
Case Is = "Development"
BackEnd = DLookup("VariableValue", "Globals", "Variable= 'TestEnvironment'")
Case Else
BackEnd = DLookup("VariableValue", "Globals", "Variable= 'Backend'")
End Select
End Property
I have tried a couple of variations on the following but get an error each time.
SELECT *
FROM TableName IN 'GetBackEnd()';
I imagine its something simple but after staring at this for so long Ijust can't see it.
thank you.
Generally, you can do what you want - use a function to provide parameter strings.
Public Function GetName() As String
GetName = "foo"
End Function
SELECT * FROM bar WHERE floo = GetName()
But in some parts / cases, you can't use variables. Both IN clauses are among them.
These won't work:
GetInList = "'x', 'y', 'z'"
SELECT * FROM bar WHERE floo IN (GetInList())
and your use-case is not possible either:
GetDbPath = "C:\path\myDb.accdb"
SELECT * FROM bar IN GetDbPath()
You will have to construct the whole SQL on the fly:
Db.QueryDefs("myQuery").SQL = "SELECT * FROM TableName IN '" & GetBackEnd() & "'"
Missing WHERE clause in SQL query? Let's say
SELECT *
FROM TableName
WHERE Name = GetBackEnd;

How to add to the end of text column through sql?

I am trying to create an sql query that adds a string to the end of the cell. Right now I have this.
$sql = "UPDATE table SET $column1= $column1 + $newstring1, $column2 = $column2 + $newstring2, $column3 = $column3 + $newstring3, WHERE username = $user_username";
The values of $newstring1, $newstring2, and $newstring3 are formatted like "155:2,"
The idea is to add delimiters to each entry so I can easily sort them later. Right now I'm getting a syntax error on my query, but I'm new to php. Is the error because when the database is empty, there is no original variable and therefore I need to INSERT INTO instead of UPDATE, or is the comma in the string itself creating the error and do I need to somehow concatenate it?
I'm more used to C# than PHP so I'm not sure the proper way to format that type of query.

to use module in sql ms-Access-2010

I have defined a procedure in my module, returning the path of the database and I want to use that path in my sql query.
Procedure in module :
Public Function pathOfBillingSoftware() As String
pathOfBillingSoftware = """path"""
End Function
I am using the function above in my sql query :
SELECT *
FROM tableName IN pathOfBillingSoftware();
It is giving me the error: "error in from clause"
but when I am using it in vba code , it is working fine
a = pathOfBillingSoftware()
sql = "INSERT INTO tableName " & _
"IN " & a & _
" SELECT * FROM tableName"
currentdb.Execute sql
Any Solution ?
This is dynamic SQL. You seem to already have found the solution: use VBA code. You can only use functions to return values in static SQL.
If you want to return operators or other things that aren't standard values, you need to use dynamic SQL, and thus need to use VBA.
You can create queries through VBA if you want, but note that once they are created, they are static.
Sample code to create a query with your function
CurrentDb.CreateQueryDef "MyQuery", "SELECT * FROM tableName IN " & pathOfBillingSoftware();

Deleting data from Access with VBA

I'm trying to delete every record from my current Access database in VBA where OTP = txtOTP.value and VARIABLE = {NomAdminContrats,TelAdminContrats,TelecAdminContrats, [...]}
Here is my code:
Dim query As Recordset
Set query = CurrentDb.Execute("DELETE * FROM tb_SOMMAIRE WHERE OTP = '" & txtOTP.value & "' AND (VARIABLE = 'NomAdminContrats' or VARIABLE = 'TitreAdminContrats' or VARIABLE = 'UnitAdminContrats' or VARIABLE = 'AdrAdminContrats' or VARIABLE = 'VilleAdminContrats' or VARIABLE = 'TelAdminContrats' or VARIABLE = 'TelecAdminContrats' or VARIABLE = 'CourrielAdminContrats')")
I got an error 3219 Invalid Operation when trying with OpenRecordset or Expected function when trying with Execute. I've tried a lot of things but I didn't manage yet to get this query working. I also have the full table in a recordset, would it be easier/faster to do it with myRecordset.Delete? If so, how could I do it?
EDIT
Now trying with CurrentDb.Execute instead of CurrentDb.OpenRecordset. The error is now Function expected instead of Invalid Operation.
You were not supposed to use parentheses for the recordset.execute function, unless you want to send the execute function options, as well as your query.
currentDB.Execute ("SQL EXECUTABLE QUERY", Options)
options is an optional constant that you can include in the function, such as dbDenyWrite, or dbFailOnError. when you don't include options in your execute function, Microsoft uses the default dbInconsistent, and you should not use parentheses.
Set query = CurrentDb.Execute "DELETE * FROM tb_SOMMAIRE..."
The reason your code worked when you used the variable sql is because you did not use the parantheses when you used the variable.
You already solved the problem. Congratulations. See whether a different approach for your WHERE clause is easier to work with.
The WHERE clause checks whether VARIABLE matches a list of values. Instead of using multiple OR conditions to compare VARIABLE with each member of the list, you can simply ask the db engine whether VARIABLE is present IN the list of values.
AND
(
[VARIABLE] IN
(
'NomAdminContrats',
'TitreAdminContrats',
'UnitAdminContrats',
'AdrAdminContrats',
'VilleAdminContrats',
'TelAdminContrats',
'TelecAdminContrats',
'CourrielAdminContrats'
)
)
Finally managed to get it working. Had to declare another var for the query for some reason.
Dim sql as String
sql = "DELETE * FROM tb_SOMMAIRE WHERE OTP = '" & txtOTP.value & "' AND (VARIABLE = 'NomAdminContrats' or VARIABLE = 'TitreAdminContrats' or VARIABLE = 'UnitAdminContrats' or VARIABLE = 'AdrAdminContrats' or VARIABLE = 'VilleAdminContrats' or VARIABLE = 'TelAdminContrats' or VARIABLE = 'TelecAdminContrats' or VARIABLE = 'CourrielAdminContrats')"
CurrentDb.Execute sql

corona lua sql update statement correct syntax when using variables

This may seem very basic but I cannot find correct syntax when using a variable.
This works:
local updateTable = [[UPDATE userDetails SET month_id = 100 WHERE id=1]]
db:exec( updateTable)
the below doesn't:
local myVariable = 100
local updateTable = [[UPDATE userDetails SET month_id = myVariable WHERE id=1]]
db:exec( updateTable)
Simply use the concat operator, .., like this:
local updateTable = [[UPDATE userDetails SET month_id = ]] .. myVariable .. [[ WHERE id=1]]
If myVariable comes from outside your app, beware of SQL injection. See: Lua mysql, need a way to escape data, or Google for lua + "prepared statement"