Paradox DB SQL Multiple JOINS - sql

I'm working on a legacy VB6 project and I need to make a JOIN call like this:
SELECT C.Cnum, C.RealDate, M.Name, R.Price, R.Qnt, R.RealPrice, R.QntP, R.QntR, M.Name
FROM "CHECK" C
LEFT JOIN "RCHECK" R ON C.Cnum = R.Cnum
LEFT JOIN "PCHECK" P ON C.Cnum = P.Cnum
LEFT JOIN "MONEY" M ON P.Curency = M.Sifr
LEFT JOIN "MENU" MN ON R.Sifr = MN.Sifr
WHERE C.Cnum > 0 ORDER BY C.Cnum
I use "Driver={Microsoft Paradox Driver (*.db )};DriverID=538" as a part of connection string but it seems it doesn't support more than one join! Which is weird.
Any ideas how to solve/workaround it?
And yes, when I run this query in Borland Database Desktop, it works fine.
Update 1:
My VB Code:
Dim Conn As New ADODB.Connection
Dim sConnStr As String
Dim sQuery As String
sConnStr = "Driver={Microsoft Paradox Driver (*.db )};DriverID=538;Fil=Paradox 5.X;CollatingSequence=ASCII;DBQ=C:\DBTOFTP\BUFF;DefaultDir=C:\DBTOFTP\BUFF;PWD=SOMEPASS;"
sQuery = "SELECT C.Cnum, C.RealDate, M.Name, R.Price, R.Qnt, R.RealPrice, R.QntP, R.QntR, M.Name " & _
"FROM ""CHECK"" C " & _
"LEFT JOIN ""RCHECK"" R ON C.Cnum = R.Cnum " & _
"LEFT JOIN ""PCHECK"" P ON C.Cnum = P.Cnum " & _
"LEFT JOIN ""MONEY"" M ON P.Curency = M.Sifr " & _
"LEFT JOIN ""MENU"" MN ON R.Sifr = MN.Sifr " & _
"WHERE C.Cnum > 0 " & _
"ORDER BY C.Cnum"
Conn.ConnectionString = sConnStr
Conn.Open

Some old drivers often requires that multiple JOINs must be enclosed in parentheses.
Try something like this:
FROM
(
"CHECK" C
INNER JOIN
"RCHECK" R
ON C.Cnum = R.Cnum
)
INNER JOIN
"PCHECK" P
ON P.Cnum = C.Cnum

Related

Problem error msg "The SELECT statement includes a reserved word or an argument name that is misspelled or missing , or the punctuation is incorrect"

My problem is: I get an error that appears in my code every time I run it and I don't know why; please I need your help.
When I run the query, it worked but when I tried to code it, I get an error
A Runsql action requires an argument consisting of an SQL statement
This is my code:
Sub Import_Cable_list()
Dim strSQL As String
strSQL =" SELECT SPI_query.Loop_name, Panel_from_ITR.PANEL_FROM, Cable_List_ITR.ITR AS ITR_Panel_FROM, Panel_from_ITR.[ITR State] AS ITR_State_Panel_FROM, PANEL_TO_ITR.PANEL_TO, PANEL_TO_ITR.ITR AS ITR_Panel_TO, PANEL_TO_ITR.[ITR State] AS ITR_State_Panel_TO, Cable_List_ITR.CABLE_NUM, Cable_List_ITR.ITR AS ITR_Cable_Number, Cable_List_ITR.[ITR State] AS ITR_State_Cable_Number, Jb_Cabinet.PANEL_TO AS Cabinet, PANEL_TO_ITR.ITR AS ITR_Cabinet, PANEL_TO_ITR.[ITR State] AS ITR_state_Cabinet" & _
" FROM SPI_query INNER JOIN (PANEL_TO_ITR INNER JOIN (Jb_Cabinet INNER JOIN (Panel_from_ITR INNER JOIN Cable_List_ITR ON Panel_from_ITR.PANEL_FROM = Cable_List_ITR.PANEL_FROM) ON Jb_Cabinet.PANEL_FROM = Panel_from_ITR.PANEL_FROM) ON PANEL_TO_ITR.PANEL_FROM = Panel_from_ITR.PANEL_FROM) ON SPI_query.Clean_Tag_Number = Panel_from_ITR.PANEL_FROM;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End Sub
Try adding a space:
strSQL = "SELECT SPI_query.Loop_name, Panel_from_ITR.PANEL_FROM, Cable_List_ITR.ITR AS ITR_Panel_FROM, Cable_List_ITR.[ITR State] AS ITR_State_Panel_FROM, PANEL_TO_ITR.PANEL_TO, PANEL_TO_ITR.ITR AS ITR_Panel_TO, PANEL_TO_ITR.[ITR State] AS ITR_State_Panel_TO, Cable_List_ITR.CABLE_NUM, Cable_List_ITR.ITR AS ITR_Cable_Number, Cable_List_ITR.[ITR State] AS ITR_State_Cable_Number, Jb_Cabinet.PANEL_TO AS Cabinet, Cable_List_ITR.ITR AS ITR_Cabinet, Cable_List_ITR.[ITR State] AS ITR_State_Cabinet " & _
"FROM SPI_query INNER JOIN (((PANEL_TO_ITR INNER JOIN Panel_from_ITR ON PANEL_TO_ITR.PANEL_FROM = Panel_from_ITR.PANEL_FROM) INNER JOIN Cable_List_ITR ON Panel_from_ITR.PANEL_FROM = Cable_List_ITR.PANEL_FROM) INNER JOIN Jb_Cabinet ON Panel_from_ITR.PANEL_FROM = Jb_Cabinet.PANEL_FROM) ON SPI_query.Clean_Tag_Number = Panel_from_ITR.PANEL_FROM ; "
Try to use this query
strSQL = "SELECT SPI_query.Loop_name,
Panel_from_ITR.PANEL_FROM,
Cable_List_ITR.ITR AS ITR_Panel_FROM,
`Cable_List_ITR.ITR State` AS ITR_State_Panel_FROM,
PANEL_TO_ITR.PANEL_TO,
PANEL_TO_ITR.ITR AS ITR_Panel_TO,
`PANEL_TO_ITR.ITR State` AS ITR_State_Panel_TO,
Cable_List_ITR.CABLE_NUM,
Cable_List_ITR.ITR AS ITR_Cable_Number,
`Cable_List_ITR.ITR State` AS ITR_State_Cable_Number,
Jb_Cabinet.PANEL_TO AS Cabinet,
Cable_List_ITR.ITR AS ITR_Cabinet,
`Cable_List_ITR.ITR State` AS ITR_State_Cabinet" & _
"FROM SPI_query
INNER JOIN (
(
(PANEL_TO_ITR INNER JOIN Panel_from_ITR ON PANEL_TO_ITR.PANEL_FROM = Panel_from_ITR.PANEL_FROM)
INNER JOIN Cable_List_ITR ON Panel_from_ITR.PANEL_FROM = Cable_List_ITR.PANEL_FROM
)
INNER JOIN Jb_Cabinet ON Panel_from_ITR.PANEL_FROM = Jb_Cabinet.PANEL_FROM
)
ON SPI_query.Clean_Tag_Number = Panel_from_ITR.PANEL_FROM ; "

how to use an in clause to check if a column is a certain string or not when referencing 3 tables

I have this SQL statement and it joining 2 tables...
SELECT TRIDENT.Maintenance.RCFAs.*, TRIDENT.Maintenance.Equipment.EquipmentName
FROM TRIDENT.Maintenance.RCFAs
LEFT JOIN TRIDENT.Maintenance.Equipment
ON TRIDENT.Maintenance.Equipment.EquipmentId = TRIDENT.Maintenance.RCFAs.EquipmentId
WHERE 1 = 1
Now I have to reference a third table because I have a dropdown by the name of components that we will use to lookup stuff on the table named TRIDENT.Maintenance.RCFAXrefComponents. So if "bolts" is selected from the dropdown it will look up the YEAR and RCFAID which makes a unqiue key for both RCFAXrefComponents and RCFAs and checks if there are any rows in RCFAs that had "bolts" in the component column when looking up that YEAR and RCFAId. A coworker suggested I use an IN clause to check if the YEAR and RCFAId is in that RCFAXrefComponents. He said it might be tricky, but I'm a little lost on how to do this.
Below is the RCFAs table that I want to grab the YEAR and RCFAId values together and lookup on the other to see if that bolt is also there. Then it should bring up the line item on RCFAs table to output to user
this is RCFAXrefComponents table below
Public Shared Function GetRCFAList(rcfaNumber As Integer?, description As String,
failureType As String, equipmentDescription As String, componentSelection As String) As List(Of RCFA)
Dim sql = "SELECT r.*, e.EquipmentName FROM TRIDENT.Maintenance.RCFAs AS r LEFT JOIN TRIDENT.Maintenance.Equipment AS e ON e.EquipmentId = r.EquipmentId JOIN TRIDENT. Maintenance.RCFAXrefComponents AS c ON c.Year = r.Year AND c.RCFAId = r.RCFAId WHERE"
If failureType <> "" Then
sql += " AND RCFAs.FailureType = '" & failureType.ToUpper & "'"
End If
If description <> "" Then
sql += " AND RCFAs.ShortDesc LIKE '%" & description.ToUpper & "%'"
End If
If equipmentDescription <> "" Then
sql += " AND Equipment.EquipmentName LIKE '%" & equipmentDescription.ToUpper & "%'"
End If
If componentSelection <> "" Then
sql += " c.ComponentId = '%" & componentSelection.ToUpper & "%'"
End If
Dim dbConn As New Trident.Core.DBConnection
Dim ds = dbConn.FillDataSet(sql)
Dim tmpList As New List(Of RCFA)
For Each dr As DataRow In ds.Tables(0).Rows
tmpList.Add(New RCFA(New Trident.Objects.Maintenance.RCFA.RCFA(dr)))
Next
Return tmpList
End Function
Just use a simple JOIN with the RFCAXrefComponents table, and a WHERE clause that filters the component ID.
SELECT r.*, e.EquipmentName
FROM TRIDENT.Maintenance.RCFAs AS r
LEFT JOIN TRIDENT.Maintenance.Equipment AS e
ON e.EquipmentId = r.EquipmentId
JOIN TRIDENT.Maintenance.RCFAXrefComponents AS c
ON c.Year = r.Year
AND c.RCFAId = r.RCFAId
WHERE c.ComponentID = 'BOLTS'
I may be confused but it sounds like you need to use EXISTS.
SELECT r.*,
e.EquipmentName
FROM TRIDENT.Maintenance.RCFAs r
LEFT JOIN TRIDENT.Maintenance.Equipment e ON e.EquipmentId = r.EquipmentId
WHERE EXISTS ( SELECT 1
FROM TRIDENT.Maintenance.RCFAXrefComponents c
WHERE c.Year = r.Year
AND c.RCFAId = r.RCFAId
AND c.ComponentId LIKE '%BOLTS%' )
you may not need the c.ComponentId LIKE '%BOLTS%' you might just want c.ComponentId = 'BOLTS'

Access 2003 SQL Syntax error

I am new with access 2003 and been stuck on this query I have wrote for a while now. The tables and column names, operators and brackets i believe are all correct however I am getting a syntax error only after I inserted the following join operation
FROM (tDailyEntries
INNER JOIN tLEDGERS ON tLEDGERS.Action = tDailyEntries.ActionNo)
INNER JOIN (tProjects
below is my full code
SELECT DISTINCT tProjects.CC_IO AS ProjectNo,
Year([DateFrom]) & " Accrual " & MonthName(Month([DateFrom])) & " - "+[CompanyName] & " ( "+([LastName]) & ")" AS [Line/Item/Text],
tUsers.LastName AS Last_Name,
tDailyEntries.UserId AS UserID,
contractordailyrate AS DailyRate,
contractordailyhours AS Hours,
ROUND(contractordailyrate / contractordailyhours, 2) AS HourlyRate,
ROUND(SUM(tDailyEntries.CalculatedDailyHours), 2) AS MonthlyHours,
ROUND((HourlyRate * MonthlyHours), 2) AS Charge,
ROUND(Charge+ROUND((Charge*0.2),2),2) AS Accruals, tProjects.Project AS Project
FROM (tDailyEntries
INNER JOIN tLEDGERS ON tLEDGERS.Action = tDailyEntries.ActionNo)
INNER JOIN (tProjects
RIGHT JOIN (textcontractor
RIGHT JOIN (tTitle
RIGHT JOIN ((Location
RIGHT JOIN (tDepartments
RIGHT JOIN tUsers
ON tProjectType.ProjectTypeID = tProjects.ProjectTypeID)
ON tDepartments.DeptID = tUsers.DeptID)
ON tLocation.LocationID = tUsers.LocationID)
RIGHT JOIN (((tDailyEntries
LEFT JOIN tDepartments AS tDepartments_1
ON tDailyEntries.DeptCharged = tDepartments_1.DeptShortName)
LEFT JOIN tActions ON tDailyEntries.ActionNo = tActions.ActionID)
LEFT JOIN tLookups
ON tDailyEntries.Zone = tLookups.LookupID)
ON tUsers.UserID = tDailyEntries.UserID)
LEFT JOIN textmain
ON tUsers.UserID = textmain.userID)
ON tTitle.TitleID = tUsers.TitleID)
ON textcontractor.companyid = textmain.contractorcompany)
ON tProjects.ProjectID = tDailyEntries.ProjectNo
WHERE tTitle.TitleID = 37
AND Month([DateFrom]) = MonthNum
AND Day([DateFrom]) <21
GROUP BY tProjects.CC_IO, Year([DateFrom]) & " Accrual " & MonthName(Month([DateFrom])) & " - "+[CompanyName] & " ( "+([LastName]) & ")", tUsers.LastName, tDailyEntries.UserId, textmain.contractordailyrate,
Month([DateFrom]), textmain.contractordailyhours, tProjects.Project;
Any help would be great.

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

Ms-Access: Join 3 Tables

Does anyone have a example of how to join 3 tables, I have the following statement but I'm getting a missing a (syntax error "operator error")
da = New OleDbDataAdapter("SELECT [S].[Scheduled Vege], [V].[Description],
[DS].[Task], [DS].[Task Date], [DS].[Completed] FROM [Scheduled] AS S
INNER JOIN [Date Schedules] AS DS ON [S].[SchedID] = [DS].[SchedID]
INNER JOIN [Vegetables Descriptions] AS V ON [V].[Task] = [DS].[Task]
WHERE [DS].[TaskNumber] >= " & aFromDate & " AND [DS].[TaskNumber] <= " & aToDate & "
AND [DS].[Completed] = '" & aCompleted & "' ", conn)
thanks
Found the problem, because it's an access database the fisrt from statement and INNER JOIN needs to be in brackets as shown below.
da = New OleDbDataAdapter("SELECT [S].[Scheduled Vege], [V].[Description],
[DS].[Task], [DS].[Task Date], [DS].[Completed] FROM ([Scheduled] AS S
INNER JOIN [Date Schedules] AS DS ON [S].[SchedID] = [DS].[SchedID])
INNER JOIN [Vegetables Descriptions] AS V ON [V].[Task] = [DS].[Task]
WHERE [DS].[TaskNumber] >= " & aFromDate & " AND [DS].[TaskNumber] <= " & aToDate & "
AND [DS].[Completed] = '" & aCompleted & "' ", conn)
Does your query execute if you remove your WHERE clause? The only thing that immediately jumped out at me was your dates are not necessarily going to be in a format that the OleDB provider is going to like.
My guess is that you need to encapsulate your dates like '1-Jan-2009' or something similar.
Your join looks okay, but I think your problem might be where you're comparing 'TaskNumber' to 'aFromDate'; numbers and dates are different types in SQL, and should cause a problem trying to compare them like this.