Solved

Pulling data from multiple tables

Posted on 2001-06-24
3
156 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
Updates not working for MS Windows 7 12 195
to transfer string from C lanaguage to VBA 4 79
VBA filters 2 73
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 61
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…

730 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