473,748 Members | 4,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if data already exists in Access from Excel

Hi,

I am trying to check if a particular record already exists in an Access
database through Excel vba code. Through code obtained at another
forum, I got the following:
*************** *************** *************** *************** ***********

Sub TheButton()

Dim db As Database, rs As DAO.Recordset, r As Long
Dim PolicyNum As Variant
Dim bFound As Boolean

Set db = OpenDatabase("E :Test.mdb")
Set rs = db.OpenRecordse t("Level Data", dbOpenTable)

bFound = False
rs.MoveFirst
Do
If ThisWorkbook.Wo rksheets("Loss Model").Cells(1 7, 3).Value =
rs.Fields("Poli cy/Quote Number") Then

bFound = True
MsgBox "Record Already Exist."
End If
rs.MoveNext
Loop Until rs.EOF Or bFound = True
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub

*************** *************** *************** *************** *********

The data is entered at cell (Q,3). But if I type in

If ThisWorkbook.Wo rksheets("Loss Model").Cells(Q , 3).Value =
rs.Fields("Poli cy/Quote Number")
it will give me a 1004 runtime error: application defined or object
defined.

If I use the code above

If ThisWorkbook.Wo rksheets("Loss Model").Cells(1 7, 3).Value =
rs.Fields("Poli cy/Quote Number")

The program cannot catch whether the record already exist and it does
not show me a MsgBox.

Please help.

Nov 20 '06 #1
3 11046
Got it, Should have used:
If ThisWorkbook.Wo rksheets("Loss Model").Range(" Q" & "3").Value =
rs.Fields("Poli cy/Quote Number")

Nov 20 '06 #2

by****@bh-hc.com wrote:
Hi,

I am trying to check if a particular record already exists in an Access
database through Excel vba code. Through code obtained at another
forum, I got the following:
*************** *************** *************** *************** ***********

Sub TheButton()

Dim db As Database, rs As DAO.Recordset, r As Long
Dim PolicyNum As Variant
Dim bFound As Boolean

Set db = OpenDatabase("E :Test.mdb")
Set rs = db.OpenRecordse t("Level Data", dbOpenTable)

bFound = False
rs.MoveFirst
Do
If ThisWorkbook.Wo rksheets("Loss Model").Cells(1 7, 3).Value =
rs.Fields("Poli cy/Quote Number") Then

bFound = True
MsgBox "Record Already Exist."
End If
rs.MoveNext
Loop Until rs.EOF Or bFound = True
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub

*************** *************** *************** *************** *********

The data is entered at cell (Q,3). But if I type in

If ThisWorkbook.Wo rksheets("Loss Model").Cells(Q , 3).Value =
rs.Fields("Poli cy/Quote Number")
it will give me a 1004 runtime error: application defined or object
defined.

If I use the code above

If ThisWorkbook.Wo rksheets("Loss Model").Cells(1 7, 3).Value =
rs.Fields("Poli cy/Quote Number")

The program cannot catch whether the record already exist and it does
not show me a MsgBox.
Your parameters for the Cells() property are reversed. They are
specified as...

Cells(RowIndex, ColIndex)

....so you should be looking at Cells(3, 17), not Cells(17, 3).

Also, if your [Level Data] table is of any significant size you might
not want to scan it each time. Instead, you could do something like
this:
Private Sub CommandButton1_ Click()
Dim db As DAO.Database, rs As DAO.Recordset
Dim PolicyNum As Variant
Dim bFound As Boolean

PolicyNum = ThisWorkbook.Wo rksheets("Loss Model").Cells(3 , 17).Value

Set db = OpenDatabase("R :\Test.mdb")

Set rs = db.OpenRecordse t( _
"SELECT [Policy/Quote Number] " & _
"FROM [Level Data] " & _
"WHERE [Policy/Quote Number]=""" & _
Replace(PolicyN um, """", """""", , , vbTextCompare) & _
"""", dbOpenDynaset)

bFound = Not rs.EOF

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

If bFound Then
MsgBox "Record exists."
Else
MsgBox "Record not found."
End If

End Sub

Nov 20 '06 #3

Gord,

Thanks for your help.

Nov 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9245
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works only exporting to single worksheet. but i need to export data to multiple worksheets. it is very urgent to us. so please help me in code.
6
1641
by: Innuendo | last post by:
Hello, I'm not a specialist with Access - so I hope that you can give me some input. I've to import xml data (about 15 different data fields) into an access-database each night. As scripting language I've to use ASP. Do you think there will be a problem with about 20.0000 to 30.000 data-records, which have to be updated (about 99 % of the data-records
7
1804
by: RBohannon | last post by:
I'm using A2K. I'm inputing data from a text file into my DB, and I need to check for the data already existing in the DB. If it's already in the DB, I don't want to reenter it. The two tables being used are tblPersonal and tblListData. tblPersonal contains names, SSNs, etc. SSN is the PrimaryKey. tblListData is keyed on the combination of SSN and ExamNum. In tblListData, an SSN can be paired with more than one ExamNum, but the
9
7262
by: Carl Fenley | last post by:
I am successfully adding stored procedures to an Access database. However, I need to be able to check if the stored procedure of the same name already exists. Is there a way to do this other than waiting for the OleDbException caused when adding one that already exists? Here is the code snippet: Private Sub CreateStoredProcedures()
1
8315
by: emily | last post by:
Hi all Is there any way to know if a record is already exist in excel worksheet. Thanks a lot Emily
6
19179
by: deejow | last post by:
Hi, I want Access to return an error message that says an identical entry already exists. I do not want to set the field to 'no duplicates' because it is a name field and it is possible to have different entries with the same name. It is for a Claimant Master table in Access 2007. Each claimant could appear in the Jobs table many times but should only have one record relating to them in the Claimant Master table. When I enter a claimant...
15
16186
by: OfficeDummy | last post by:
Hello all! I searched the Internet and this forum for a similar problem, but I found no help... Also, I'm a complete newbie to the fascinating world of programming, VBA and Access, so my question can very well be very stupid. The tasks are: 1)Import an Excel table into Access 2)Add a new column and fill it with variables of date/time type. Steps 1 and 2 need to be done only once, and I've almost managed to accomplish them. Now...
3
7160
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this file but save it as an excel file. The data in this excel file will be imported into an Access database. The
0
6454
by: bharathreddy | last post by:
This article will explain you how to check weather a column already exists in a table before you add the column to the table using alter command. Using the system tables you can check to see weather a column already belongs to a specific table. SYSCOLUMNS is the system table which stores all the table columns information, from this column you can check weather a specific column exists in a specific table. ...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.