INNER JOIN problems - sql

I tried to make a code on VB6, but it seems an error occured...
can you help me master?
Here's my code :
"SELECT guru.nip, guru.nama_guru, set_hr_kerja.bulan, set_jam_kerja.hari, set_hr_kerja.jlh_kerja, set_jam_kerja.jam_masuk " & _
"FROM (guru INNER JOIN set_hr_kerja ON guru.nip = set_hr_kerja.nip) INNER JOIN set_jam_kerja ON guru.nip = set_jam_kerja.nip " & _
"WHERE " & _
"nama_guru LIKE '%" & Kutip(TBox(Index).Text) & "%' OR " & _
"hari LIKE '%" & sHari & "%' " & _
"ORDER BY nama_guru")
The Error says : "ODBC driver does not support the requested properties" and also when I press
debug button an error "Operation is not allowed when object is closed"
What should I do? Can you help me?
I'm glad if you can help me...
Thanks

You have an error in your statement. The parenthesis after the FROM are incorrect and not really needed either.
"SELECT guru.nip, guru.nama_guru, set_hr_kerja.bulan, set_jam_kerja.hari, set_hr_kerja.jlh_kerja, set_jam_kerja.jam_masuk " & _
"FROM guru INNER JOIN set_hr_kerja ON guru.nip = set_hr_kerja.nip) INNER JOIN set_jam_kerja ON guru.nip = set_jam_kerja.nip " & _
"WHERE " & _
"nama_guru LIKE '%" & Kutip(TBox(Index).Text) & "%' OR " & _
"hari LIKE '%" & sHari & "%' " & _
"ORDER BY nama_guru"

Related

SQL Converting Access DB null value to blank string

I am stuck trying to fix this error and have been at it for far too long. I've done a lot of Googleing and searching this site but can't seem to find the right answer for me even though there are many different variations of this question.
I am writing a front end GUI using VB.Net with an Access database on the back end. The DB has NULL values in PartnerFirstName, PartnerLastName, and Organization in my Partner Table. I am loading these into a Datatable in my application so they can be searched by the user. I just found out last week that some partners (the one's with NULL values) are not loading.
Code below:
("SELECT IDPartner, IIF(ISNULL(PartnerFirstName),'',PartnerFirstName) as PartnerFirstName, IIF(ISNULL(PartnerLastName), '', PartnerLastName) AS PartnerLastName, IIF(ISNULL(Organization), '', Organization) AS Organization, " & _
"Address1, City, State, Zip, Country, Active " & _
"FROM tblPartner " & _
"WHERE IDPartner like '%" & txtPartnerID.Text & "%' AND " & _
" PartnerFirstName like '%" & txtFirstName.Text & "%' AND " & _
" PartnerLastName like '%" & txtLastName.Text & "%' AND " & _
" Organization like '%" & txtOrganization.Text & "%' AND " & _
" Address1 like '%" & txtStreet.Text & "%' AND " & _
" City like '%" & txtCity.Text & "%' AND " & _
" State like '%" & txtState.Text & "%' AND " & _
" Zip like '%" & txtZipCode.Text & "%' AND " & _
" Country like '%" & txtCountry.Text & "%' " & _
"ORDER BY IDPartner, partnerlastname, partnerfirstname", cn)
The reason for the likes are because I want the search to update in real time based on the form's text box values. This is the source of my problem since they are blanks by default and the DB has nulls.
===================
EDIT:
Thank for the initial comments. I realize now that I was using an improper function. the IIF suggested by Baro gets me through the SQL now which is great!
It appears the issue I'm having with records not showing up is in my WHERE clause. If I remove that then all the records load into my Datatable. When I include the Where clause as shown the records will NULL values are excluded.
I have tried to use an OR PartnerFirstName IS NULL (as well as the other fields) but that leaves too many extra results that also have NULL values. I cannot figure out what to do to allow the search now that I know the problem is in the Where section of the statement.
The issue is that the WHERE predicate is targeting the original field(s), which have not had the IIF(ISNULL(...)) expression applied.
It is difficult to see because you've aliased the field in the SELECT statement as the same name as the underlying field. So, the query is doing what you've instructed it to do, albeit not what you've meant for it to do.
To resolve, I would use a subquery which converts the NULL values to empty strings and then in the outer query add your WHERE predicate. Something like the following should do the trick:
("SELECT q.* FROM (SELECT IDPartner, IIF(ISNULL(PartnerFirstName),'',PartnerFirstName) as PartnerFirstName, IIF(ISNULL(PartnerLastName), '', PartnerLastName) AS PartnerLastName, IIF(ISNULL(Organization), '', Organization) AS Organization, " & _
"Address1, City, State, Zip, Country, Active " & _
"FROM tblPartner) q " & _
"WHERE q.IDPartner like '%" & txtPartnerID.Text & "%' AND " & _
" q.PartnerFirstName like '%" & txtFirstName.Text & "%' AND " & _
" q.PartnerLastName like '%" & txtLastName.Text & "%' AND " & _
" q.Organization like '%" & txtOrganization.Text & "%' AND " & _
" q.Address1 like '%" & txtStreet.Text & "%' AND " & _
" q.City like '%" & txtCity.Text & "%' AND " & _
" q.State like '%" & txtState.Text & "%' AND " & _
" q.Zip like '%" & txtZipCode.Text & "%' AND " & _
" q.Country like '%" & txtCountry.Text & "%' " & _
"ORDER BY q.IDPartner, q.partnerlastname, q.partnerfirstname", cn)
Thanks to all for you help and suggestions, I have learned a lot from the few suggestions and really appreciate it!
I have solved the problem by going back into Access. I ran an update query on any NULL field and changed it to a "". Such a simple solution!
If I didn't have the WHERE requirements in place that I do this wouldn't have been such a big deal.

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.

Access 2007 Query Def Syntax Error

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.

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"

How do I delete files from a table in MSDE 2000 that is selected by 3 joins?

I have a VB6 program that uses a n Access backend. The query that I am currently using is
sQuery = "DELETE tblResultNotes.* " & _
"FROM (tblJobs INNER JOIN tblResults ON tblJobs.JobID=tblResults.JobID) INNER JOIN tblResultNotes ON tblResults.ResultID=tblResultNotes.ResultID " & _
"WHERE (tblJobs.CreateDate)< #" & strDate & "# " & _
"AND tblResults.StartTime < #" & strDate & "#;"
I have changed my backend to MSDE 2000 and now this query is giving me a syntax error near '*'. Could someone help me out?
Thanks,
Tom
Try changing your SQL to this:
sQuery = "DELETE FROM tblREsultNotes " & _
"FROM " & _
" tblJobs" & _
" INNER JOIN tblResults ON tblJobs.JobID=tblResults.JobID" & _
" INNER JOIN tblResultNotes ON tblResults.ResultID=tblResultNotes.ResultID" & _
"WHERE tblJobs.CreateDate < '" & strDate & "'" & _
"AND tblResults.StartTime < '" & strDate & "'"
Note the date delimiter change to ' instead of #.