473,378 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.OpenRecordset("Level Data", dbOpenTable)

bFound = False
rs.MoveFirst
Do
If ThisWorkbook.Worksheets("Loss Model").Cells(17, 3).Value =
rs.Fields("Policy/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.Worksheets("Loss Model").Cells(Q, 3).Value =
rs.Fields("Policy/Quote Number")
it will give me a 1004 runtime error: application defined or object
defined.

If I use the code above

If ThisWorkbook.Worksheets("Loss Model").Cells(17, 3).Value =
rs.Fields("Policy/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 10995
Got it, Should have used:
If ThisWorkbook.Worksheets("Loss Model").Range("Q" & "3").Value =
rs.Fields("Policy/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.OpenRecordset("Level Data", dbOpenTable)

bFound = False
rs.MoveFirst
Do
If ThisWorkbook.Worksheets("Loss Model").Cells(17, 3).Value =
rs.Fields("Policy/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.Worksheets("Loss Model").Cells(Q, 3).Value =
rs.Fields("Policy/Quote Number")
it will give me a 1004 runtime error: application defined or object
defined.

If I use the code above

If ThisWorkbook.Worksheets("Loss Model").Cells(17, 3).Value =
rs.Fields("Policy/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.Worksheets("Loss Model").Cells(3, 17).Value

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

Set rs = db.OpenRecordset( _
"SELECT [Policy/Quote Number] " & _
"FROM [Level Data] " & _
"WHERE [Policy/Quote Number]=""" & _
Replace(PolicyNum, """", """""", , , 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
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...
6
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...
7
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...
9
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...
1
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
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...
15
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...
3
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...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.