syntax error in string in query in VBA access - sql

I get 3075 error running this line in vba access:
Dim sqlMZG As String
sqlMZG = "SELECT MAZeitenGesamt.* Where MAZeitenGesamt.MA = 'JPA' FROM MAZeitenGesamt;"
Where JPA is a constant value. I tried the following forms and none of them worked.
SELECT MAZeitenGesamt.* Where MAZeitenGesamt.[MA] = '" & "JPA" & "' FROM MAZeitenGesamt;
SELECT MAZeitenGesamt.* Where MAZeitenGesamt.MA = '" & "JPA" & "' FROM MAZeitenGesamt;
SELECT MAZeitenGesamt.* Where MAZeitenGesamt.MA = ""JPA"" FROM MAZeitenGesamt;
SELECT MAZeitenGesamt.* Where MAZeitenGesamt.MA = \"JPA\" FROM MAZeitenGesamt;
SELECT MAZeitenGesamt.* Where MAZeitenGesamt.MA = \'JPA\' FROM MAZeitenGesamt;
Any ideas?

I'd recommend you use a parameter rather than quoting a literal. That said, #gizlmeier is right, in that your syntax is wrong to begin with.
Dim sqlMZG As String
sqlMZG = "parameters [MAParam] text; " & _
"SELECT MAZeitenGesamt.* FROM MAZeitenGesamt Where MAZeitenGesamt.MA = [MAParam];"
From there, when you create your query you can set the value for the parameter:
Set qry = CurrentDb.CreateQueryDef("GetMaz", sqlMZG)
qry.Parameters("MAParam") = JPA
No messy quoting to worry about and such.

Related

Instead of 'table variable+column name =' is there any method to simplify that?

Instead of table variable+column name = is there any method to simplify that?
update [hopi].[dbo].[hdc
set dostat = 'i'
from [hopi].[dbo].[hdc] as b
inner join [hopi].[dbo].hperson as a
on b.hpercode = a.hpercode
inner join [hopi].[dbo].[hprocm] as c
on b.proccode = c.proccode
where b.dostat = 'a'
and datepart(yy,b.dodate)='" & yr & "'
and datepart(mm,b.dodate)='" & mnth & "'
and datepart(dd,b.dodate)='" & dy & "'
and a.last= '" & & "'enter code here
and a.first= '" & & "'
and (c.procdesc='prec' or c.procdesc='pros' or c.procdesc='vat' or c.procdesc='vet' or c.procdesc='pak' or c.procdesc='pren' or c.procdesc='maser' or c.procdesc='lolo' or c.procdesc='yawa')
You can use an IN clause here to avoid the repetition i.e.
AND (c.procdesc IN ('prec', 'pros', 'vat', 'vet', 'pak', 'pren', 'maser', 'lolo', 'yawa'))

Access Run Time Error 3464 data type mismatch in criteria expression

What is wrong with that code? I can't figure out why I keep getting this error.
Sub renttt()
Dim rent_list As Recordset
Dim query As String
query = "SELECT * FROM (Rent INNER JOIN Movies ON Rent.Movie_ID = Movies.ID) INNER JOIN Customers ON Rent.Customer_ID = Customers.ID WHERE Rent.Movie_ID = '" & txtbxmovieID.Value & "' AND Rent.Date_Returned is Null;"
Set rent_list = CurrentDb.OpenRecordset(query)
If rent_list.RecordCount = 1 Then
rent_List.MoveFirst
txtbxname.Value = (rent_list![CusName])
txtbxsurname.Value = (rent_list![Surname])
txtbxcardID.Value = (rent_list![Id_Card_number])
txtbxaddress.Value = (rent_list![Address])
txtbxrented.Value = (rent_list![Date_Rent])
End If
End Sub
Wouldn't MovieId be a numeric? If so, no quotes:
WHERE Rent.Movie_ID = " & txtbxmovieID.Value & " AND ...

How to make stored procedure in if..End if condition

I am beginner at stored procedures. I tried the following stored procedure in IF...End If condition so how to make it ... I am confused.... so anyone create it
strSql = "SELECT count(*) " & _
" FROM hist_billgen_report r, hist_billgen_header h " & _
" WHERE r.invoice_number=h.invoice_number " & _
" and h.macnum = '" & l_macnum & "' " & _
" and r.rep_type = 1 " & _
" and r.rep_call_type = '" & line_type & "' " & _
" and h.billing_job_id = '" & arg_job & "' "
'Special code for data lines for Newcore
If gcompany = "NCW" Then
strSql += " and r.rep_number not in ( "
strSql += "select distinct a.mdn from order_wireless a where"
strSql += " a.id in"
strSql += " ("
strSql += " select c.serviceid from cust_charge_file b, service_charges c, main_company_utilities d"
strSql += " where(b.chg_main_index = c.chargeid)"
strSql += " and c.serviceid = a.id"
strSql += " and (b.chg_main_category_id = d.utilities_id and d.utilities_type = 'CS' and (utilities_desc_short like '%FDS1%' or utilities_desc_short like '%FDS2%' or utilities_desc_short like '%FDS3%' or utilities_desc_short like '%MBS1%' or utilities_desc_short like '%MBS2%' or utilities_desc_short like '%MBS3%' ) )"
strSql += " )"
strSql += " and a.accountnumber = '" & l_macnum & "' "
strSql += " )"
End If
Putting a query into a stored procedure doesn't necessarily always mean a performance increase. Depending on the data in your tables, this query could be quite slow due to the OR LIKES '%%' in it, but you could do something like this:
create procedure [dbo].[spname]
#l_macNum int -- note, you haven't given a lot of information, create all query parameters with appropriate types here
-- more parameters
#arg_job int -- same
AS
BEGIN
if (#company = 'NCW')
begin
SELECT count(*)
FROM hist_billgen_report r, hist_billgen_header h
WHERE r.invoice_number=h.invoice_number
and h.macnum = #l_macNum
-- etc
and r.rep_number not in (
-- etc
)
end
else
begin
SELECT count(*)
FROM hist_billgen_report r, hist_billgen_header h
WHERE r.invoice_number=h.invoice_number
and h.macnum = #l_macNum
-- etc
end
END
GO
note the If and else logic are completely separate queries, as you cannot do what I think you were hoping to do in a contiguous query, without using dynamic sql. there are certain caveats to this but given you're new to sql, going to stick with that
I used -- etc as place holders for your text, as I'm not going to provide the entire solution :P
If that doesn't make sense, let me know.

Syntax error (missing operator) in query expression when update table using other table records

When trying to execute this SQL statement, I get error :
Syntax error (missing operator) in query expression Orders.BuyPrice" From Pro INNER JOIN Orders On Pro.ProID = Orders.ProID
Trying to Update Balance and BuyPrice in Pro Table from Orders table
dim query as string = "update Pro set Pro.Balance = Pro.Balance + Orders.Qu," & _
"Pro.BuyPrice = Orders.BuyPrice" & _
" From Pro INNER JOIN Orders On Pro.ProID = Orders.ProID " & _
"AND orders.OrderID = " & orderID
execute(query)
Execute:
execute(q as string)
connectDB
dim cmd as new ODBCCommand(q, DBcon)
cmd.executeNonQuery()
I don't know what is wrong in query?
ODBC use MySql so try this code
update
Pro
Inner join orders
on
Pro.ProID = Orders.ProID
set
Pro.Balance = Pro.Balance + Orders.Qu,
Pro.BuyPrice = Orders.BuyPrice
where
orders.OrderID = orderID
You can not do a join within an update query. You have to do a select query first to retrieve Orders.Qu and Orders.BuyPrice and use the result in your update query which will looks like:
dim query as string = "update Pro set Pro.Balance = Pro.Balance + " & qu & ", Pro.BuyPrice = " & buy_price & ";"
This query worked with me
Query = " update Pro Inner join orders on Pro.ProID = Orders.ProID " & _
"set Pro.Balance = Pro.Balance + Orders.Qu," & _
"Pro.BuyPrice = Orders.BuyPrice" & _
" where orders.OrderID = " & orderID

Visual Basic: Can't read record/data from recordset.

Here is my Code:
Dim CompanyName, _
CompanyDomain, _
CompanyEmail, _
CompanySupportPhone
Call GetEmailList
Sub GetEmailList
dim sql
dim companydata
sql = ""
sql = sql & " DECLARE #CompanyName VARCHAR(100);"
sql = sql & " DECLARE #CompanyDomain VARCHAR(100);"
sql = sql & " DECLARE #CompanyActivityEmail VARCHAR(100);"
sql = sql & " DECLARE #CompanySupportPhone VARCHAR(100);"
sql = sql & " SELECT"
sql = sql & " #CompanyName = CASE WHEN Setting = 'CompanyName'"
sql = sql & " THEN StringValue ELSE #CompanyName END,"
sql = sql & " #CompanyDomain = CASE WHEN Setting = 'CompanyDomain'"
sql = sql & " THEN StringValue ELSE #CompanyDomain END,"
sql = sql & " #CompanyActivityEmail = CASE WHEN Setting = 'CompanyActivityEmail'"
sql = sql & " THEN StringValue ELSE #CompanyActivityEmail END,"
sql = sql & " #CompanySupportPhone = CASE WHEN Setting = 'CompanySupportPhone'"
sql = sql & " THEN StringValue ELSE #CompanySupportPhone END"
sql = sql & " FROM ClientSettings"
sql = sql & " WHERE Setting in ('CompanyDomain','CompanyActivityEmail','CompanySupportPhone','CompanyName')"
sql = sql & " SELECT ISNULL(#CompanyName, '') AS CompanyName, ISNULL(#CompanyDomain, '') AS CompanyDomain, ISNULL(#CompanyActivityEmail, '') AS CompanyEmail, ISNULL(#CompanySupportPhone, '') AS CompanySupportPhone"
set companydata = getRecordset(sql)
CompanyName = companydata("CompanyName") ' LINE 80
CompanyDomain = companydata("CompanyDomain")
CompanyEmail = companydata("CompanyEmail")
CompanySupportPhone = companydata("CompanySupportPhone")
companydata.Close
Set companydata = Nothing
End Sub
This throws an error:
Line 80
Item cannot be found in the collection corresponding to the requested
name or ordinal.
I marked line 80 above. I run this exact same SQL in SQL Server Manager and it returns results:
CompanyName CompanyDomain CompanyEmail CompanySupportPhone
MyCompanyName http://localhost MyCompanyName#email.com 801-555-1212
Any idea what I am doing wrong here?
GetRecordSet correctly loads and processes the database call, this function works in 1,000 other places. I'm sure the problem isn't there.
Add
sql = sql & " SET NOCOUNT ON;"
as the first SQL statement.
SET NOCOUNT ON usage