?
Solved

visual basic excel access

Posted on 2003-03-21
12
Medium Priority
?
243 Views
Last Modified: 2013-11-25
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.
0
Comment
Question by:silviaaa
[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
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 2

Accepted Solution

by:
WintersVine earned 136 total points
ID: 8183711
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
 
LVL 2

Expert Comment

by:WintersVine
ID: 8183746
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
 
LVL 3

Assisted Solution

by:ignacioperez
ignacioperez earned 132 total points
ID: 8186148
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Assisted Solution

by:hvdberg
hvdberg earned 132 total points
ID: 8186931
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
 

Expert Comment

by:hvdberg
ID: 8186939
oh yeah, I forgot to mention: Add a reference to the Excel object library and one to the DAO library in the project.
0
 

Expert Comment

by:hvdberg
ID: 8189485
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
 
LVL 2

Expert Comment

by:WintersVine
ID: 8190190
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
 

Expert Comment

by:hvdberg
ID: 8190374
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
 

Expert Comment

by:CleanupPing
ID: 8531854
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
 
LVL 6

Expert Comment

by:GPrentice00
ID: 8982568
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
 

Expert Comment

by:hvdberg
ID: 8984318
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
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9046145
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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

777 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