Solved

Pulling data from multiple tables

Posted on 2001-06-24
3
146 Views
Last Modified: 2010-05-02
Yet another Data question!
Hi - I need to pull data from multiple tables that match,
Example: Using the code below I can get data from 1 table, but not more - I need to do something simular to below - except pull the customer or vendor info from another table, Does this require relationships? Or a join?


'Search Example
Dim Check
List1.Clear
List2.Clear
List3.Clear
List4.Clear
Dim strSQL As String
'If txtSKUNum.Text = txtLastSKU Then Exit Sub
'txtLastSKU = txtSKUNum.Text
strSQL = "SELECT ItemDesc, SellPrice,Category,Type,Image from NewItems " & _
           "Where ItemDesc like '" & "%" & Text1.Text & "%'"
On Error Resume Next
rs.Close
On Error GoTo 0
rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly

If Text1.Text = "" Then
MsgBox "Please enter a search term"
Else
Do Until rs.EOF = True
Check = rs!Itemdesc
'If rs.EOF = True And Check = "" Then
'MsgBox "Sorry No matches found"
'End If
List1.AddItem rs!Itemdesc
List2.AddItem Format(rs!SellPrice, "$####.##")
List3.AddItem rs!Category
List4.AddItem rs!Type
rs.MoveNext
Loop

If rs.EOF = True And Check = "" Then
MsgBox "Sorry No matches found"
End If
  End If
Set rs = Nothing
'lstDesc.AddItem rs!Itemdesc
'lstPrice.AddItem rs!sellprice
'lstQTY.AddItem "1"

End Sub

All's I get are errors when I attempt to pull data from multiple tables - This is an access database, what I would need is something like:

Select Customer, InvoiceNum, Item,Date,CheckNum,Image From Customers, Items, Invoices Where InvoiceNum = Text1.text.

All's I get is alot of errors when I try anything like this.
0
Comment
Question by:flosoft
3 Comments
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
Try this

You need to have relationships. Since you are joining 3 tables, it is natural that you would need 3-1=2 criterias. Example below.

Customers (Fields)
===================
CustID, Customer, DOB

Items
=====
Item, Image

Invoices
=========
CustID, InvoiceNum, CheckNum, Item, Date

What do you notice from above? Notice that there are common fields from the 3 tables so that they can be linked something like below.

sql = "Select t1.Customer, t3.InvoiceNum, t2.Item, t3.Date, t3.CheckNum, t2.Image From Customers t1, Items t2, Invoices t3 Where t1.CustID = t3.CustID, t2.Item = t3.Item"

hongjun
0
 
LVL 5

Accepted Solution

by:
ianouii earned 100 total points
Comment Utility
> strSQL = "SELECT ItemDesc, SellPrice,Category,Type,Image
> from NewItems " & "Where ItemDesc like '" & "%" & _
> Text1.Text & "%'"

ok, just to modify your SQL statement.

select .... from NewTems WHere ItemDesc like .... & text1.text ....

inner join with table name,
left outer join with table name,
right outer join with ....

this will be much easier for u to take control.

u can always refer to MSDN for more info.

but remember, in your SQL statement, in the select statement, it's a better practice that u specify the table name for the field when u select.

etc,

select tblNm1.item1, tblNm1.item2, tblNm2.item1, tblNm2.item2, ...

good luck.

0
 
LVL 9

Expert Comment

by:Valliappan AN
Comment Utility
You could test on queries, and how it returns data, by using MS Access, go to Query tab. Click New, then select the tables and fields that you with to Join and retrieve and test the results. Once you got it, you could right click and go to SQL mode, and copy the SQL statement, paste it to your VB and modify it. This especially, helps, when you are trying on multiple table Joins.

As others have specified, you need to know which fields are to be linked between tables, normally by the primary key fields of the tables, after you identify the fields needed to be retrieved. Unless, you link them, the SQL will result in lot of records, putting all combinations between the tables, and so, you will get erroneous results.

rs.Close
On Error GoTo 0
rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly

You don't use On Error GoTo 0. Put a statement like, instead of the line.

Set rs = New Recordset

You may need to study how to open recordset, etc. in MSDN, as ianouii has pointed out to read MSDN.

If Text1.Text = "" Then
MsgBox "Please enter a search term"
Else

You could check if it is empty, even before opening the recordset, so, once an error, it won't open the recordset, retrieve records, unnecessarily. like this:

If Text1.Text = "" Then
   Text1.Setfocus
   MsgBox "Please enter a search term"
   Exit Sub
Endif

I hope this would help you.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now