Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL INNER JOIN 1

Status
Not open for further replies.

hafa2000

Technical User
Aug 14, 2009
7
IE
Hi,

I have a form with 3 unbound text boxes:

txtJob
txtSpec
txtConsignorName

The form allows a user to search on one or more Job number or Spec number. The user can enter the numbers in the txtJob field or txtSpec field to search for Job numbers or Spec numbers which begin with those numbers and click the Search button.
The form performs the search on the criteria the user enters, loads the result into a local table (tvwDelivery) from the linked server table (tblDelivery)

In other words, the code below examines the criteria entered and creates a search predicate to find the result. The code looks at the unbound text boxes one at a time to create a search string.

Private Sub cmdSearch_Click()
Dim db As DAO.Database
Dim strSQL As String, varSQLWhere As Variant, intRCount As Integer

varSQLWhere = Null

If Not IsNothing(Me.txtJob) Then
' Create a wildcard search for leading characters
varSQLWhere = "(C.JobID Like '" & Me.txtJob & "*')"
End If
If Not IsNothing(Me.txtSpec) Then

varSQLWhere = (varSQLWhere + " AND ") & _
"(C.SpecNo Like '" & Me.txtSpec & "*')"
End If
If Not IsNothing(Me.txtConsignor) Then

varSQLWhere = (varSQLWhere + " AND ") & _
"(C.ConsignorID Like '" & Me.txtConsignor & "*')"
End If
If IsNothing(varSQLWhere) Then
Call CustomError("You must enter at least one search criteria.", _
OK, "Find Jobs")
Exit Sub
End If

strSQL = "INSERT INTO tvwDelivery ( DeliveryDocketID, JobID, DeliveryDocketNo, ConsignorID, SpecNo) " & _
"SELECT C.DeliveryDocketID, C.JobID, C.DeliveryDocketNo, C.ConsignorID, C.SpecNo " & _
"FROM tblDelivery AS C " & _
"WHERE " & varSQLWhere
If fctFind(strSQL) = False Then
Call CustomError("There was a problem with the search. " & _
"Please close this window and try again.", , "Search Failure")
Exit Sub
End If

Set db = DBEngine(0)(0)
db.TableDefs.Refresh
intRCount = db.TableDefs("tvwDelivery").RecordCount

If intRCount <> 1 Then
Me.RecordCount = intRCount & " Records found."
Else
Me.RecordCount = intRCount & " Record found."
End If

Me.RecordCount.Visible = True
Set db = Nothing
Me.Requery


End Sub

At the moment the code only searches the criteria for txtJob and txtSpec from 1 table called tblDelivery and inserts into tvwDelivery

tblDelivery
Delivery DocketID Auto
JobID Number
Delivery Docket No Number
ConsignorID Number
SpecNo Text

tvwDelivery
Delivery DocketID Number
JobID Number
Delivery Docket No Number
ConsignorID Number
SpecNo Text
ConsignorName Text

The user now wants to search for a Consignor on the txtConsignorName field. But this information would have to come from the tblConsignors

tblConsignors
ConsignorID Auto
ConsignorName Text

Could one of you Access SQL Masters point me in the direction of showing me how this could be written as an INNER JOIN to the tblConsignors.ConsignorID? This does not seem correct:

strSQL = "INSERT INTO tvwDelivery ( DeliveryDocketID, JobID, DeliveryDocketNo, ConsignorID, SpecNo) " & _
"SELECT C.DeliveryDocketID, C.JobID, C.DeliveryDocketNo, C.ConsignorID, C.SpecNo, Cn.ConsignorName " & _
"FROM tblDelivery AS C INNER JOIN tblConsignors AS Cn " & _
"ON C.ConsignorID = Cn.ConsignorID " & _
"WHERE " & varSQLWhere


Any assistance would be greatly appreciated




 
Replace this:
strSQL = "INSERT INTO tvwDelivery ( DeliveryDocketID, JobID, DeliveryDocketNo, ConsignorID, SpecNo) " & _
with this:
strSQL = "INSERT INTO tvwDelivery ( DeliveryDocketID, JobID, DeliveryDocketNo, ConsignorID, SpecNo, ConsignorName) " & _


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

Thanks so much for the reply. I don't know why I didn't see it missing in the INSERT Statement - worked like a charm!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top