visual basic excel access

i have this: in access i hava a table that contains emails:
   1 aaa@aaa.com
   2 bbb@bb.com
   3 ccc@ccc.com and so...

well in excel i have something like that..
1 aaa@aaa.com
2 bbb@bb.com
3 ddd@ddd.com

so i want to compare the access with the excel and make a report in excel like this
1 aaa@aaa.com  already exist
2 bbb@bb.com   already exist
3 ddd@ddd.com  new

so i want to take each cell of excel and compare it with the table in access, so if it is the same, i want to make another excel with the results that already exist, and if it doesn't match with anyone, put the result that is new.
silviaaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WintersVineCommented:
first you want to query yor database inside of Excel
make a new worksheet
then go to Cell A1 and select:

Data>>Import External Data>>New Database Query

This will bring up a window that asks you to select a data source. Choose "MS Access Database" from the list
then locate the Access Database on your computer or server.
Excel will propt you to choose a table and fields. Choose the fields you need and then say "ok"
Excel will now query this database as often as you choose
Right-click cell A1 to set Query options

Now you will have a list of email address in one worksheet and a list in the other

use a formula like this:
=IF((MATCH(A1,Sheet2!A:A,0)=0),"New","Exists")

This looks for value in cell A1 (on Sheet1) in column "A" on Sheet2, the Match function returns a row number so if its zero no match was found; thus if it = 0 then its new.

*However* the Match function will produce an error if it can't find a match, so if its a new email address you'll get a "#N/A" if its new, and a "exists" if its not

Well hopefully this will point you in the right direction.
If you need more help, let me know. (There are a number of steps I haven't included here.)

Also there is more than one way to skin this cat, so if this isn't what you are hoping for let me know.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WintersVineCommented:
you can test to see if the match funtion returns an error like this:
=IF(IsNA(MATCH(A1,Sheet2!A:A,0)),"New","Exists")

This will give you the true/false you are looking for.

Note: the IsNA returns a boolean value (true or false)
0
ignacioperezCommented:
Silvia,
       there are two good ways to operate Excel from Visual Basic. One is using the Microsoft Excel Object Library (you can add the reference in your project, the file is EXCEL9.OLB) that allows you to control Excel documents very easily. This way, you could read any cell you wanted applying any alogythm.
      The other way is using ADO. Using ADO you can open an Excel file as a database (always it has the proper table format)
      I hope this can help you.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

hvdbergCommented:
Hi Silvia,

I slapped together a small routine together to do this.
Create a new project with a commandbutton Command1 on it, then copy the below code into the project. Make sure you change the names in the thing I wrote to match your database and worksheet name and location.

Hope this helps you,
Henk
Option Explicit
Dim db As Database, rs As Recordset


Sub Command1_Click()
Dim xlSource As Excel.Workbook, xlDestination As Excel.Workbook, rng As Range, n, thefile
Set db = OpenDatabase("d:\documents and settings\lanlord\desktop\db1.mdb")
Set rs = db.OpenRecordset("tabel1")
Set xlSource = Excel.Workbooks.Open("d:\documents and settings\lanlord\desktop\map1.xls")
Set xlDestination = Excel.Workbooks.Add
thefile = "d:\documents and settings\lanlord\desktop\testing.xls"
xlDestination.SaveAs thefile
rs.MoveFirst
n = 1
Do While Not rs.EOF
   With xlSource.Worksheets(1).Range("A1:A500")
      Set rng = .Find(rs.Fields(1))
      xlDestination.Worksheets(1).Cells(n, 1) = rs.Fields(1)
      If rng Is Nothing Then
         xlDestination.Worksheets(1).Cells(n, 2) = "new"
      Else
         xlDestination.Worksheets(1).Cells(n, 2) = "already exists"
      End If
   End With
   rs.MoveNext
   n = n + 1
Loop
xlSource.Close
xlDestination.Save
xlDestination.Close
Set xlSource = Nothing
Set xlDestination = Nothing

End Sub

0
hvdbergCommented:
oh yeah, I forgot to mention: Add a reference to the Excel object library and one to the DAO library in the project.
0
hvdbergCommented:
Oops, I misread your original post. The posted code tests if the addresses in the MDB are new in the XLS or not, and you asked for a test the other way round. Replace the command1_click code with the following.

Sub Command1_Click()
Dim xlSource As Excel.Workbook, xlDestination As Excel.Workbook, rng As Range, n, thefile
Set db = OpenDatabase("d:\documents and settings\lanlord\desktop\db1.mdb")
Set rs = db.OpenRecordset("tabel1", dbOpenDynaset)
Set xlSource = Excel.Workbooks.Open("d:\documents and settings\lanlord\desktop\map1.xls")
Set xlDestination = Excel.Workbooks.Add
thefile = "d:\documents and settings\lanlord\desktop\testing.xls"
xlDestination.SaveAs thefile
n = 1
Do While xlSource.Worksheets(1).Cells(n, 1) <> ""
   rs.FindFirst "email = '" & xlSource.Worksheets(1).Cells(n, 1) & "'"
   xlDestination.Worksheets(1).Cells(n, 1) = xlSource.Worksheets(1).Cells(n, 1)
   If rs.NoMatch = True Then
      xlDestination.Worksheets(1).Cells(n, 2) = "new"
   Else
      xlDestination.Worksheets(1).Cells(n, 2) = "already exists"
   End If
   n = n + 1
Loop
xlSource.Close
xlDestination.Save
xlDestination.Close
Set xlSource = Nothing
Set xlDestination = Nothing
End

End Sub
0
WintersVineCommented:
Hydberg's is a "Macro" solution that works when you click a button.

my solution is a "spreadsheet" solution, meaning Excel will keep it updated without clicking a button.

Both are good solutions that will work.
Its just a matter of how you want to use the spreadsheet
0
hvdbergCommented:
Well, actually mine isn't a macro solution, I wrote it in the IDE of Visual Basic 5.0. Only for the sake of ease of testing I attached it to a command button, but you could also create a project that has no forms, just code. That way you could turn it into a stand alone executable program that you can run anytime you want to.

Also, my solution doesn't generate a #N/A, it just enters "doesn't exist" in the cell

It's a matter of taste I guess, which solution you prefer. I'll happily admit that I'm biassed to mine, since it was ME that wrote it. :-)
0
CleanupPingCommented:
Hi silviaaa,
This old question (QID 20559060) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
GPrentice00Commented:
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in a week or two.  I would appreciate any comments by the experts that would help me in making a recommendation.
It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:
http://www.experts-exchange.com/help/closing.jsp

GPrentice00
Cleanup Volunteer
0
hvdbergCommented:
What recommendation will you be making? I'm Dutch and perhaps there's some linguistic nuance I'm missing in your comment, but I don't know what kind of comments you're looking for.

Henk
0
GPrentice00Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->Split between WintersVine and ignacioperez and hvdberg

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.