Access 2007 Query Def Syntax Error - sql

this is really frustrating and I'm hoping you can help me. I keep getting a syntax error no matter what I do on this query. I've read about putting parantheses on multiple joins but I did that and it doesn't work. What am I doing wrong here? Thanks for your time.
Set PTI = CurrentDb.CreateQueryDef("qry_PTImport", "SELECT DISTINCT" & _
" bl.LoanNumber" & _
", qcq.Code" & _
", t.ecType" & _
", eb.QueryNo" & _
", q.QueryName" & _
", q.Description" & _
", q.suggestedAction" & _
", bd.datetransfer" & _
", bd.datebrd " & _
"FROM ((((dbo.eCs_QCqueue qcq " & _
"JOIN dbo.eCs_Queries q ON q.QueryNo = eb.QueryNo) " & _
"JOIN brd_loan bl ON bl.serialID = qcq.serialID) " & _
"JOIN dbo.eCs_Type t ON t.ectypeid = q.ectypeid) " & _
"JOIN brd_deal bd ON bd.Code = bl.Code) " & _
"WHERE qcq.Code = '" & Deal & "'")

just wanted to say user HansUp was right in this instance of what my problem was. VBA doesn't do just JOINs.

Related

item cannot be found in the collection corresponding to the requested name or ordinal

My code gives the following error. How can I correct this?
Item cannot be found in the collection corresponding to the requested name or ordinal
ElseIf Me.chkItem.Checked = True Then
Dim CheckNumber As String = ""
Dim CheckRef As String = ""
dsvoucheritem.Clear()
DSVoucher_Expense.Clear()
DSVoucher_Check.Clear()
Try
Me.lstCV.Items.Clear()
strDiscount = Nothing
rec.Open("select billpaymentcheckline.txnnumber, billpaymentcheckline.txndate" _
& ", billpaymentcheckline.payeeentityreffullname" _
& ", billpaymentcheckline.amount, billitemline.itemlineitemreffullname" _
& ", billitemline.memo" _
& ", billpaymentcheckline.appliedtotxndiscountamount" _
& ", billpaymentcheckline.appliedtotxnrefnumber, billpaymentcheckline.bankaccountreffullname" _
& ", billpaymentcheckline.appliedtotxndiscountaccountreffullname" _
& ", billpaymentcheckline.appliedtotxntxndate, billpaymentcheckline.appliedtotxnamount" _
& ", billpaymentcheckline.refnumber, account.AccountNumber from (billitemline inner join" _
& " billpaymentcheckline on billitemline.refnumber=billpaymentcheckline.appliedtotxnrefnumber) left outer join" _
& " account on billitemline.APAccountreflistid=account.listid where" _
& " billpaymentcheckline.bankaccountreflistid='" &Me.lblBankID.Text & "' and" _
& " billpaymentcheckline.refnumber between '" & CInt(Me.txtRefFR.Text)
& "' and '" & CInt(Me.txtRefTO.Text) & "'", con, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly)
It's saying it doesn't recognize one of your column names. Double check all of them. You can also try removing fields until you find the culprit.
memo is a reserved word in Access SQL.
Try billitemline.[memo]

Trying to process SQL query within Excel VBA; incorrect syntax near 'a'

I'm a novice programmer at work and I'm struggling with a little snippet of code I need. I've read through various posts but I did not find anything that helped at last.
What I'm trying to do is to launch my SQL-Query within my vba macro but i keep getting "incorrect syntax near 'a'" error and i couldn't figure out why.
Set con = New ADODB.Connection
Set rs = CreateObject("ADODB.Recordset")
con.Open sConStr
Set rs = con.Execute("SELECT CONVERT(char(10), a.[Starttime], 104) as Date " & _
",CONVERT(char(8),a.[Starttime], 114) AS Starttime " & _
",CONVERT(char(8),a.[Endtime], 114) AS Endtime " & _
",DATEDIFF(s,a.[Starttime], a.[Endtime]) AS Duration " & _
",a.[BCMLogin] " & _
",a.[Type] " & _
",a.[PRSProfil] " & _
",b.[Location] " & _
"FROM [xxxxxxx].[dbo].[Report] AS a LEFT JOIN [xxxxxxx].[dbo].[Stammdaten] AS b on a.[Loginname] = b.[Username] " & _
"where " & _
"cast(a.[Starttime] as date) between #" & str_DFrom & "# AND #" & str_DUntil & "#" & _
"AND " & _
"a.[PRSProfil] != ''" & _
"AND " & _
"a.[Type] != 'StatusWorking'" & _
"Order BY " & _
"1,2")

VBA SQL Join Query

I am having troubles with a VBA SQL JOIN. I Keep Getting A "Join Expression Not Supported" Error. The Following Code Works In The Query Design View but seems to throw an error when in vba.
Dim Rs As DAO.RecordSet
Set Rs = CurrentDb.OpenRecordset( _
"SELECT Schools.ID, Schools.[School Name],Schools.Address, Schools.Postcode, Schools.[Principal name], " & _
"Schools.[E-Mail], Schools.Phone, Schools.Region, Schools.JTHE, Schools.[Social Status], Events.Program " & _
"FROM Schools INNER JOIN Events ON Schools.ID = Events.School WHERE ((Schools.Region = '" & RegionOne & _
"' Or Schools.Region = '" & RegionTwo "' Or Schools.Region = '" & RegionThree "' Or Schools.Region = '" & _
RegionFour "') AND (Schools.JTHE = " & JTHE1 & " Or Schools.JTHE = " & JTHE2 ") AND (Schools.[Social Status] = '" & _
StatusBox.Value "') AND (Events.Program = '" & ProgramBox.Value & "'));")
This Similar Query Works
Set Rs = CurrentDb.OpenRecordset("SELECT * FROM Schools WHERE " & _
"(((Schools.Region)='" & RegionOne & _
"' Or (Schools.Region)='" & RegionTwo & _
"' Or (Schools.Region)='" & RegionThree & _
"' Or (Schools.Region)='" & RegionFour & _
"') AND ((Schools.[Social Status])='" & StatusBox.Value & _
"') AND ((Schools.JTHE)=" & JTHE1 & " Or (Schools.JTHE)=" & JTHE2 & "));")
Any help would be greatly appreciated.
I'm not entirely sure why is that. It is hard to spot error when your doing it on VBA, unlike if your in an actual SQL Management studio where you can spot the lines that errors out. Nonetheless, you may try this:
Set Rs = CurrentDb.OpenRecordset( _
"SELECT Schools.ID, Schools.[School Name], Schools.Address, " & _
"Schools.Postcode, Schools.[Principal name], Schools.[E-Mail], " & _
"Schools.Phone, Schools.Region, Schools.JTHE, Schools.[Social Status], " & _
"Events.Program " & _
"FROM Schools " & _
"INNER JOIN Events " & _
"ON Schools.ID = Events.School " & _
"WHERE Schools.Region IN (" & _
"'" & RegionOne & "'," & _
"'" & RegionTwo & "'," & _
"'" & RegionThree & "'," & _
"'" & RegionFour & "') " & _
"AND Schools.JTHE IN (" & JTHE1 & ", " & JTHE2 & ") " & _
"AND Schools.[Social Status]='" & StatusBox.Value & "' " & _
"AND Events.Program='" & ProgramBox.Value & "';")
I formatted it as such to give you the story of the query (and that is how I will write it in SQL). Not really a direct to the point answer to your question but I just simplified your OR statements and instead uses IN. You might get a:
Too many continuous line error
So adjust the concatenation of strings. I have not tested this of course (although it compiles) but my goal is to give you idea on a possible way to do it. HTH.

SQL Query works in MS-Access but cannot implement it in vb.net form

The block of code immediately following (given to me by a stackoverflow solver) works perfectly in MS-Access. I'm trying to convert it to work in a vb.net form accessing the very same MS-Access database. I get an error and cannot see my mistake. Are there any vb.net coders that can see what I'm doing wrong. The first block of code works in MS-Access and is the code I'm trying to convert. And the second block of code is my conversion attempt.
SELECT at.animalID, amt.milestoneType
FROM
animals_Table at
LEFT JOIN
(
SELECT animalID, milestoneType
FROM animalMilestones_Table
WHERE milestoneType = 'Intake'
) amt
ON at.animalID = amt.animalID
Now, my conversion attempt:
dim selectAnimal as string
selectAnimal = "SELECT at.animalID, amt.milestoneType" & _
" FROM animals_Table at" & _
" LEFT JOIN" & _
" (" & _
" SELECT animalID, milestoneType" & _
" FROM animalMilestones_Table" & _
" WHERE milestoneType = '" & "Intake" & "'" & _
" ) amt" & _
" ON at.animalID = amt.animalID"
The error code I get is
!ErrorInfo.GetDescription failed with E_FAIL(0x80004005)
It appears that ACE.OLEDB doesn't like at as a table alias. Try this instead
Dim selectAnimal As String
selectAnimal = "SELECT atbl.animalID, amtbl.milestoneType" & _
" FROM animals_Table atbl" & _
" LEFT JOIN" & _
" (" & _
" SELECT animalID, milestoneType" & _
" FROM animalMilestones_Table" & _
" WHERE milestoneType = '" & "Intake" & "'" & _
" ) AS amtbl" & _
" ON atbl.animalID = amtbl.animalID"

VBA sql string error: missing operator

Writing an sql string in vba and getting syntax error for a missing operator. I thought the error was with the inner joins so I tried taking them out but still same error.
Here is the query string:
sql1 = "SELECT WorkOrder.ProjectID, tref_dep.department, live_project.project_codename " _
& "FROM WorkOrder " _
& "INNER JOIN tlive_project " _
& "ON tlive_project.project_id = WorkOrder.ProjectID " _
& "INNER JOIN tref_dep " _
& "ON tref_dep.dep_id = WorkOrder.ToDepartment " _
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& " CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"
I am probably missing something simple but any help is greatly appreciated!
You have forgotten a logic operator before CONTAINS(..) :
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& "AND CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"
VBA requires parenthesis for 2 or more JOIN clauses.
sql1 = "SELECT WorkOrder.ProjectID, tref_dep.department, live_project.project_codename " _
& "FROM (WorkOrder " _
& "INNER JOIN tlive_project " _
& "ON tlive_project.project_id = WorkOrder.ProjectID) " _
& "INNER JOIN tref_dep " _
& "ON tref_dep.dep_id = WorkOrder.ToDepartment " _
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& " CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"