473,398 Members | 2,165 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,398 software developers and data experts.

How to write a procedure to test if a record exists in a table

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 called "record active". eg. if emp_id exists and record Active = "Y" don't write the record.

I have tried to edit the "on error" bit of the procedure attached to the command buttone but it doesn't work.

Any ideas?
Aug 8 '07 #1
6 3578
JKing
1,206 Expert 1GB
Hi. Could you post the code you're currently using so we can what you've attempted and help you to fix it up?
Aug 8 '07 #2
Hi. Could you post the code you're currently using so we can what you've attempted and help you to fix it up?
This is the code connected to the command button on the form

Private Sub Command24_Click()
On Error GoTo Err_Command24_Click

Dim stDocName As String


stDocName = "permit update for issued permits"
DoCmd.OpenQuery stDocName, acNormal, acEdit

Exit_Command24_Click:
Exit Sub


Err_Command24_Click:
MsgBox " A live permit already exists, deactivate if necessary", vbOKOnly, "pleb head"
Resume Exit_Command24_Click

End Sub

The next bit is the query
"INSERT INTO [Permit details] ( Permit_user_id, Permit_decsion, Permit_issue_no, Permit_date_issued )
SELECT DISTINCT CStr(Forms![select staff form]![staff details1f].form!emp_id) AS Expr1, 1 AS Expr2, IIf([MaxOfPermit_issue_no] Is Null,1,[MaxOfPermit_issue_no]+1) AS Expr5, Date() AS Expr3
FROM ([staff details] LEFT JOIN [max permit issue no] ON [staff details].emp_id = [max permit issue no].Permit_user_id) LEFT JOIN livepermits ON [staff details].emp_id = livepermits.Permit_user_id
WHERE (((livepermits.Permit_user_id) Is Null) AND (([staff details].emp_id)=[Forms]![select staff form]![staff details1f].[form]![emp_id]));"

The code is running an append query. In that query I have a subquery that pulls out all "live" permits from the Permits Table. The record will only be appended emp_id does not exist in the "live permits" query. At the moment i have the "action queries" confirm on and all that happens is the message "0 records appended appears".

What I really want to do is run the "live permits" query first and if the "emp_id" exists in this query place a msgbox on the screen that states "This person already has a permit" and then not run the append query. But if it doesn't exist to run the append query and give the user the message "Permit created"

Thanks

Helena
Aug 8 '07 #3
Peter99
10
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 called "record active". eg. if emp_id exists and record Active = "Y" don't write the record.

I have tried to edit the "on error" bit of the procedure attached to the command buttone but it doesn't work.

Any ideas?
Try something like this:

Set mdbCurrent = CurrentDb
strSQL = "Select * from [TableName] Where [Field] = SelectionCriteria"
Set Master = mdbCurrent.OpenRecordset(strSQL)
If Master.RecordCount > 0 Then
Do your thing
Else
Append your record
End If

mdbCurrent.Close
Aug 8 '07 #4
Try something like this:

Set mdbCurrent = CurrentDb
strSQL = "Select * from [TableName] Where [Field] = SelectionCriteria"
Set Master = mdbCurrent.OpenRecordset(strSQL)
If Master.RecordCount > 0 Then
Do your thing
Else
Append your record
End If

mdbCurrent.Close
Thanks - I've written it as below but am getting the following error message

"run time error 3061 too few parameters expected 1" on the following line.

Set Master = mdbCurrent.OpenRecordset(strSQL).

The code is as follows:

Private Sub Command29_Click()
Set mdbCurrent = CurrentDb
strSQL = "Select * from livepermits Where permit_user_id = me!staffcombo" ' staffcombo is on the form
Set Master = mdbCurrent.OpenRecordset(strSQL)
If Master.RecordCount > 0 Then
MsgBox " A live permit already exists, deactivate if necessary", vbOKOnly, "warning"
Else
MsgBox " the code is working ", vbOKOnly, "warning"
End If

mdbCurrent.Close

End Sub
Aug 8 '07 #5
Peter99
10
Thanks - I've written it as below but am getting the following error message

"run time error 3061 too few parameters expected 1" on the following line.

Set Master = mdbCurrent.OpenRecordset(strSQL).

The code is as follows:

Private Sub Command29_Click()
Set mdbCurrent = CurrentDb
strSQL = "Select * from livepermits Where permit_user_id = me!staffcombo" ' staffcombo is on the form
Set Master = mdbCurrent.OpenRecordset(strSQL)
If Master.RecordCount > 0 Then
MsgBox " A live permit already exists, deactivate if necessary", vbOKOnly, "warning"
Else
MsgBox " the code is working ", vbOKOnly, "warning"
End If

mdbCurrent.Close

End Sub


Its probably me!StaffCombo

Try something like this

strSQL = "Select * from livepermits Where permit_user_id = ' " & me!staffcombo & " ' "

You can put in a tempory msgbox to check strSQL - the me!staffcombo should show the value selected in the combo box

msgbox strSQL
Aug 8 '07 #6
Its probably me!StaffCombo

Try something like this

strSQL = "Select * from livepermits Where permit_user_id = ' " & me!staffcombo & " ' "

You can put in a tempory msgbox to check strSQL - the me!staffcombo should show the value selected in the combo box

msgbox strSQL

Thanks so much it now works.
Aug 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
1
by: Dan Loomis | last post by:
Hi guys - I have a weird condition here I'm trying to get resolution on, and unfortunately I'm not getting anywhere with my friends at Oracle. I'm hoping it's something simple, or at the very...
4
by: deprins | last post by:
Hello, I have wrote a stored procedure but its real slow. Its activated by a button on web page but its takes to long to process and the web server gives a timeout message after 5 minutes. Is...
1
by: Colin Spalding | last post by:
I am using the following code to insert records into a destination table that has a three column primary key i.e. (PupilID, TermID & SubjectGroup). The source table records all the pupils in a...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
1
by: Lauren Quantrell | last post by:
I have read the newsgroups and see this is a common issue but I saw no resolution for it: I have an Access2K frotn end and SQL Server 2K backend. In access, I create a temp table using code in a...
6
by: Varangian | last post by:
I have an SQL Server 2005 Express Stored Procedure .. which first I select if that record exists, if it exists I update that record .. if not I insert. It then selects the record with the new data...
2
by: sara | last post by:
I use Allen Browne's Audit Trail code in everything I do and I love it. Recently, I've run into a problem I can't figure out. I have a database with about 35 lookup tables. I am creating an...
2
by: kxyz | last post by:
Hello everyone, I need help with a stored procedure or two. My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.