I'm trying to use seek to check for the existence of a record before
saving, so there are no duplicate entries (is there another way?). I
have a "groups" table, which has
GroupID
Island
GroupName
My code right now is giving me a "type mismatch" error, and this is what
it looks like:
Dim rsGroups As Recordset
Set rsGroups = CurrentDb.OpenR ecordset("tblGr oups")
rsGroups.Seek Form_frmGroups. txtGroupName
If rsGroups.EOF Then
MsgBox "Value not found"
' DoCmd.Save
' Form_frmGroups. AllowAdditions = False
' Form_frmGroups. AllowEdits = False
Else
MsgBox "Value already found in database"
End If
Can anyone offer some suggestions or hints?
Kevin 33 11320
To use Seek, you must specify the index name (which must be on the field you
are seeking), and then you must specify the operator (probably "=".)
But that approach is not very flexible, e.g. if fails if you split the
database later, because Seek works only on recordsets of type dbOpenTable
(i.e. not attached tables.) A DLookup() would be simpler and more flexible.
This example assumes tblGroups has a primary key named GroupID, and it
avoids the search if the value has not changed (so an existing record does
not find itself):
Dim strWhere As String
Dim varResult As Variant
With Me.txtGroupName
If IsNull(.Value) Or (.Value = .OldValue) Then
'do nothing
Else
strWhere = "[GroupName] = """ & .Value & """"
varResult = DLookup("GroupI D", "tblGroups" , strWhere)
If Not IsNull(varResul t) Then
MsgBox "Group" & varResult & " has the same value."
End If
End If
End With
Note: If GroupName is a Number field (not a Text field), lose the extra
quotes:
strWhere = "[GroupName] = " & .Value
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Kevin Brammer" <kd*****@msn.co m> wrote in message
news:MP******** ********@tornad o.socal.rr.com. .. I'm trying to use seek to check for the existence of a record before saving, so there are no duplicate entries (is there another way?). I have a "groups" table, which has
GroupID Island GroupName
My code right now is giving me a "type mismatch" error, and this is what it looks like:
Dim rsGroups As Recordset Set rsGroups = CurrentDb.OpenR ecordset("tblGr oups")
rsGroups.Seek Form_frmGroups. txtGroupName
If rsGroups.EOF Then MsgBox "Value not found"
' DoCmd.Save ' Form_frmGroups. AllowAdditions = False ' Form_frmGroups. AllowEdits = False
Else MsgBox "Value already found in database"
End If
Can anyone offer some suggestions or hints?
Kevin
Per Allen Browne: To use Seek, you must specify the index name (which must be on the field you are seeking), and then you must specify the operator (probably "=".)
But that approach is not very flexible, e.g. if fails if you split the database later, because Seek works only on recordsets of type dbOpenTable (i.e. not attached tables.) A DLookup() would be simpler and more flexible.
Actually, you can .Seek an attached table.
The trick is to Dim/Set a pointer to the database that the table is in.
e.g.
-------------------------------------------------------------------
Public Function EmployeeExist(b yVal theEmployeeNumb er As Long) As Boolean
Dim myDB as DAO.Database
Dim myRS As DAO.Recordset
Set myDB = dbEngine(0).Ope nDatabase("M:\W hatever\Payroll .mdb")
Set myRS = myDB.OpenRecord set("tblEmploye e", dbOpenTable)
With myRS
.Index = "PrimaryKey "
.Seek "=", theEmployeeNumb er
If .NoMatch = False Then
EmployeeExist = True
End If
(or, if you want to be cute: EmployeeExist = Not .NoMatch
.Close
End With
Set myRS = Nothing
set myDB = Nothing
End Function
-------------------------------------------------------------------
--
PeteCresswell
(PeteCresswell) wrote: Per Allen Browne: To use Seek, you must specify the index name (which must be on the field you are seeking), and then you must specify the operator (probably "=".)
But that approach is not very flexible, e.g. if fails if you split the database later, because Seek works only on recordsets of type dbOpenTable (i.e. not attached tables.) A DLookup() would be simpler and more flexible.
Actually, you can .Seek an attached table.
The trick is to Dim/Set a pointer to the database that the table is in.
e.g. ------------------------------------------------------------------- Public Function EmployeeExist(b yVal theEmployeeNumb er As Long) As Boolean
Dim myDB as DAO.Database Dim myRS As DAO.Recordset
Set myDB = dbEngine(0).Ope nDatabase("M:\W hatever\Payroll .mdb")
Set myRS = myDB.OpenRecord set("tblEmploye e", dbOpenTable) With myRS .Index = "PrimaryKey " .Seek "=", theEmployeeNumb er If .NoMatch = False Then EmployeeExist = True End If (or, if you want to be cute: EmployeeExist = Not .NoMatch .Close End With
Set myRS = Nothing set myDB = Nothing End Function
-------------------------------------------------------------------
When ducks honk they are geese.
--
Lyle Fairfield
Hi Kevin
I wouldn't use Seek at all.
Just open your recordset based on "WHERE GroupName = " & QUOTE &
Me.txtGroupName & QUOTE
The QUOTE is a global that I use which is simply a double set of
quotation marks. When using a string as a parameter for an SQL
statement it needs to be enclosed in these double quotes. That is why
you are getting your mismatch error.
After opening the recordset, your next line is:
If rsGroups.BOF and rsGroups.EOF then
'You've got an empty recordset and therefore the group isn't in
the table
' Do what you wanndo
end if
Kevin, I'm going to join this mishmash of suggestions, although you
have probably already implemented one. What I would do is simply make
the GroupName a primary key (No Duplicates) in your underlying table.
You would just have an extra primary key (as opposed to making
GroupName your ONLY primary key). In this approach, any effort to add a
record with the same GroupName as an existing one would result in an
error. On Error Resume next works well, but you could trap that
specific error if you wanted to.
Kevin Brammer <kd*****@msn.co m> wrote in
news:MP******** ********@tornad o.socal.rr.com: I'm trying to use seek to check for the existence of a record before saving
Why not just do a SELECT on the values that you're trying to match,
and if the result has a non-zero number of records, you know it's a
duplicate?
I never use SEEK. Ever.
--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
"Steve" <th*********@gm ail.com> wrote in
news:11******** **************@ o13g2000cwo.goo glegroups.com: Kevin, I'm going to join this mishmash of suggestions, although you have probably already implemented one. What I would do is simply make the GroupName a primary key (No Duplicates) in your underlying table. You would just have an extra primary key (as opposed to making GroupName your ONLY primary key). In this approach, any effort to add a record with the same GroupName as an existing one would result in an error. On Error Resume next works well, but you could trap that specific error if you wanted to.
This response confuses me.
A primary key, by definition, can only be "primary" if there's only
one of them in a table. Suggesting adding another primary key would
be like saying "this is the first, and this is the other first."
I believe what you mean is adding a unique index on the other field.
The downside of this is that for that to work, it has to prohibit
Nulls.
--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
David W. Fenton wrote: "Steve" <th*********@gm ail.com> wrote in news:11******** **************@ o13g2000cwo.goo glegroups.com:
Kevin, I'm going to join this mishmash of suggestions, although you have probably already implemented one. What I would do is simply make the GroupName a primary key (No Duplicates) in your underlying table. You would just have an extra primary key (as opposed to making GroupName your ONLY primary key). In this approach, any effort to add a record with the same GroupName as an existing one would result in an error. On Error Resume next works well, but you could trap that specific error if you wanted to.
This response confuses me.
A primary key, by definition, can only be "primary" if there's only one of them in a table. Suggesting adding another primary key would be like saying "this is the first, and this is the other first."
I believe what you mean is adding a unique index on the other field. The downside of this is that for that to work, it has to prohibit Nulls.
Hehe, yes you can only have one primary key :)
--
regards,
Bradley
A Christian Response http://www.pastornet.net.au/response
On Thu, 22 Dec 2005 15:16:35 -0600, David W. Fenton wrote: Kevin Brammer <kd*****@msn.co m> wrote in news:MP******** ********@tornad o.socal.rr.com:
I'm trying to use seek to check for the existence of a record before saving
Why not just do a SELECT on the values that you're trying to match, and if the result has a non-zero number of records, you know it's a duplicate?
I never use SEEK. Ever.
I saw that in one of my searches as well. I may have to give it a try,
thanks!
Kevin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ST |
last post by:
Hi,
I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to
write the syntax to check a sql table to see if the record already exists
based on firstname and lastname text fields (will match to firstname and
lastname in SQL table). I can't figure out the syntax!!! I would like the
Error msg to just display and exit the sub if the row exists. help!!!!
thanks!
|
by: Average Bear |
last post by:
If anyone could help with this one, I am a bit puzzled. I understand
you can create an index using two fields of a database, then use the
seek method to find a record based on both fields. My code isn't
working. It finds the record but the record exists under a different
customer record. I want it only to find the record if both conditions
(customer, number) are met. Here is the code:
Dim dbs As Database, tdf As TableDef, idx As...
|
by: David C. Barber |
last post by:
I'm trying to determine if any matching records exist on a LIKE query
performing a partial match of last names to a remote back-end database in
the most efficient manner possible. LAN Traffic appears to be the
bottleneck. This column is already indexed, and all I'm trying to determine
is whether no records, or at least one record, exists. My solution is the
following:
SELECT COUNT(*) FROM
(SELECT TOP 1 FROM table WHERE LastName LIKE...
|
by: Sarah Smith via AccessMonster.com |
last post by:
I am creating a database of documents that need to be worked on, are int eh proress of being worked on, and have been completed. Sometimes the same document (an updated version) comes back for more work. I am keeping all the info in one table and using an Index numberas my Primary Key. I have a form based on this table with a Findrecord button (used the Command Button Wizard for this) that populates the form when it finds the particular record....
|
by: kufre |
last post by:
How can I use three criteria to find a record? I've done this before
where I only use one criteria to find a record and set the focus to
that criteria only.
Thanks in advance.
| |
by: Randy |
last post by:
I have been able to set up a Find Record Button on my switchboard to
take me to a form with the correct case number by using a parameter
query and macro.
When I try to run the Find Record button from the macro on the form I
want to find the record in it just sits there and does not run.
Could someone tell me how to fix this? Please be specific as I am new
to Access.
|
by: Opie |
last post by:
What would be a more efficient way for me to determine if a record in an SQL
DB table exists? Right now, I have a try/catch like this:
try
{
if(checkcom.ExecuteScalar().ToString()==tbBillNumber.Text)
{
....
}
}
|
by: gjoneshtfc |
last post by:
Hello
I want to search my database for a vehicle registration number but
before i can search using the Find Record button i created i have to
click in the registration field so that it is that one that is being
searched. Is there any way to just click the button and by default it
searches the registration field?
Or... is there a way where I can get the user to type in the
registration number in a text field then click a command button...
|
by: Matt |
last post by:
I need some guidance on how to handle an issue. I have an .asp page
that correctly queries a table and returns data if a 'job number' and
week ending date exist and the user can update the information that is
there. What I need to do is, if a record does not exist the page
needs to create one and then refresh/requery so the user can edit the
data. Any suggestions on how to accomplish this?
THanks
|
by: Helena666 |
last post by:
Hi
Its been a while since I have built a database using access and vba and am a bit rusty. I am using a command button on a form to write a record to a table, using an append query. However I need to test if a "live" record exists in that table and if it does to let the user know that the record has not been written. The append query will only write the record to the table if one doesn't exist already.
The criteria is emp_id and a field...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |