[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Pulling data from multiple tables

Posted on 2001-06-24
3
Medium Priority
?
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses

650 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