Solved

Pulling data from multiple tables

Posted on 2001-06-24
3
149 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
ID: 6223157
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
ID: 6223166
> 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
ID: 6223300
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 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

13 Experts available now in Live!

Get 1:1 Help Now