Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 16th, 2008, 11:57 AM
awyeah awyeah is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 18 awyeah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 6 sec
Reputation Power: 0
Finding common elements (values) in two tables in MS access db using VB 6.0

Hello!
I am new to VB 6.0. Currently I am doing some data pre-processing for a project, and not being good in VB I had difficulty in something.

Suppose I have two tables in MS access:
1) Jan2006
2) Feb2006

And these two tables have equal number columns. My focus is only to select a column named "CustomerNo" in both Tables in my MS access database (I converted from excel files using VB 6.0).

I am trying to find the common elements in both the CustomerNo columns in Jan2006 and Feb2006, using VB 6.0 (rs.find) currently, however I am unable to do so. After finding the common elements I wish to extract them into a new table and then go ahead with my other data files.

Exceptions:
(1) There are duplicate values in the CustomerNo column, which need to be skipped, when the common values are extracted
(2) I have 50,000+ rows of data, resulting in 600MB db file for pre-processing in this manner, needed a fast and optimizable way
(3) The number of rows are not same in any of the "CustomerNo" columns. However the data is numerical value like:

TABLE: JAn2006
CustomerNo
00200002
00200003
00200004
00200005
00200006
00200006
00200007
00200008
00200010
00200011
00200012
00200015
00200016
00200020

TABLE: Feb2006
CustomerNo
00200002
00200003
00200004
00200005
00200007
00200008
00200010
00200011
00200012
00200015
00200016
00200021
00200022

Here is the code I was working on, currently having a problem with the pointers using rs.find:

Code:
Dim cn            As New ADODB.Connection
Dim cnstr         As String
Dim sql           As String
Dim rs1           As New ADODB.Recordset
Dim rs2           As New ADODB.Recordset
Dim rs3           As New ADODB.Recordset
Dim strFilePath   As String
Dim strloop       As String

strFilePath = App.Path & "\data.mdb"
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFilePath & ";"

cn.Open cnstr
sql1 = "SELECT CustomerNo FROM Jan2006"
sql2 = "SELECT CustomerNo FROM Feb2006"

Set rs1 = cn.Execute(sql1)
Set rs2 = cn.Execute(sql2)



Do Until rs2.EOF
   rs1.MoveFirst
   strloop = rs1.Fields("CustomerNo").Value
   MsgBox rs1.Fields("CustomerNo").Value
   rs2.Find ("CustomerNo='strloop'"), , adSearchForward, 1
   
If (rs2.EOF = True) Then
Debug.Print rs1.Fields("CustomerNo")
End If

rs2.MoveNext
Loop

rs1.Close
rs2.Close

End Sub

Reply With Quote
  #2  
Old April 16th, 2008, 01:40 PM
medialint's Avatar
medialint medialint is offline
spirit duplicator
Click here for more information.
 
Join Date: Apr 2004
Location: \\Firecrate\
Posts: 12,304 medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)  Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 3 Weeks 6 h 50 m 9 sec
Reputation Power: 2575
That's what SQL is for

Code:
SELECT Feb2006.CustomerNo
FROM Feb2006 INNER JOIN Jan2006 ON Feb2006.CustomerNo = Jan2006.CustomerNo;


Voila the result of this query is those records with matching CustomerNo in both tables ...
__________________
medialint.com

"Energy has the opportunity to change the climate if it's done right." - Sen. John Ensign, R-Nev. (quoted out of context)

Reply With Quote
  #3  
Old April 17th, 2008, 03:32 AM
awyeah awyeah is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 18 awyeah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 6 sec
Reputation Power: 0
Wow thanks! Works perfect and very fast indeed for my recordset.

I guess I will have to read more about the SELECT option in SQL to understand it better.

Reply With Quote
  #4  
Old April 17th, 2008, 11:59 AM
medialint's Avatar
medialint medialint is offline
spirit duplicator
Click here for more information.
 
Join Date: Apr 2004
Location: \\Firecrate\
Posts: 12,304 medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)medialint User rank is General 24th Grade (Above 100000 Reputation Level)  Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1Folding Points: 232775 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 3 Weeks 6 h 50 m 9 sec
Reputation Power: 2575
Load up access and try using the query builder. Then switch back and forth from design view to SQL view. It doesn't always generate the cleanest SQL but it will help you get a grasp on basic query semantics. For building queries like this it's pretty straightforward just tie the tables together on the common field and set the join type to include only those in both tables (note the different join types and how they become INNER/OUTER/LEFT/RIGHT joins in the SQL as you play around too) ...

Reply With Quote
  #5  
Old April 17th, 2008, 06:16 PM
zynder's Avatar
zynder zynder is offline
Not much of a contributor
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2006
Location: Hidden
Posts: 800 zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)zynder User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 140963 Folding Title: Super Ultimate Folder - Level 1Folding Points: 140963 Folding Title: Super Ultimate Folder - Level 1Folding Points: 140963 Folding Title: Super Ultimate Folder - Level 1Folding Points: 140963 Folding Title: Super Ultimate Folder - Level 1Folding Points: 140963 Folding Title: Super Ultimate Folder - Level 1Folding Points: 140963 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Week 2 Days 22 h 6 m 32 sec
Reputation Power: 647
Send a message via Yahoo to zynder
If you want to study SQL go to www.w3schools.com and click on the SQL section. It's a good place to start.

Reply With Quote
  #6  
Old April 18th, 2008, 05:05 AM
awyeah awyeah is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 18 awyeah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 6 sec
Reputation Power: 0
Quote:
Originally Posted by medialint
Load up access and try using the query builder. Then switch back and forth from design view to SQL view. It doesn't always generate the cleanest SQL but it will help you get a grasp on basic query semantics. For building queries like this it's pretty straightforward just tie the tables together on the common field and set the join type to include only those in both tables (note the different join types and how they become INNER/OUTER/LEFT/RIGHT joins in the SQL as you play around too) ...


Thank you for the suggesstion, I will lookup the query wizard in Microsoft Access, that will definitely help and improve my understanding of sql query select.

Reply With Quote
  #7  
Old April 19th, 2008, 04:07 AM
awyeah awyeah is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 18 awyeah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 6 sec
Reputation Power: 0
Thanks for all the suggestions people. I will keep them in mind, when using SQL select next time.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Finding common elements (values) in two tables in MS access db using VB 6.0


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway