Solved

Only 1 ContactID can be added to each table

Posted on 2014-01-06
14
262 Views
Last Modified: 2014-01-06
Hi - I use Access 2010.

I have a form which I use to add data to a table.  And I have another table which has all my Contacts Details including a field unique to them called ContactID.

Basically, when a user starts to fill in the form they have to enter their ContactID.  If they have already filled in the form before and try entering their ContactID again, it will display a message saying that their ContactID has already been used and so they can't carry on which is great and this is the code I use.

---------------------------
if dcount("*","tbl1", "ContactID=" & me.cblContactId) > 0 then
   msgbox "Contact ID already exists! I'm sorry but you have already completed this form before"
   cancel=true
end if
---------------------------

However, I have since added a search form which allows a user to click on a button within the main form which opens up another form called "frmContactsSearch".  This form basically has a list box inside which displays all Contacts details (Contact ID, Title, First Name, Last Name, DOB, Telephone).  All the user has to do is click on their name, and click a button called 'input to form' which basically puts all their details into the original form.  this is the code that sits within the 'on click' event.

--------------------------------------------------
Private Sub btnInput_Click()

On Error GoTo Err_btnInput_DblClick

Forms("frm1")![cboContactID] = ListBox.Column(0)
Forms("frm1")![cboTitle] = ListBox.Column(1)
Forms("frm1")![cboFirstName] = ListBox.Column(2)
Forms("frm1")![cboLastName] = ListBox.Column(3)
Forms("frm1")![cboDOB] = ListBox.Column(4)
Forms("frm1")![cboTelephone] = ListBox.Column(5)
Me.Refresh

DoCmd.Close

If IsNull(ListBox.Value) Then

DoCmd.Close
DoCmd.OpenForm ("frm1")

End If

Exit_btnInput_DblClick:
    Exit Sub

Err_btnInput_DblClick:
   
    Resume Exit_btnInput_DblClick

End Sub
------------------------------------------

I basically need it to do the same thing as the code at the top that when a user selects their name from this searchform, if when they click the button 'input to form' that ContactID has already been used in the form, it will display a message box informing them that that ContactID has already been used and will not allow them to add it.
0
Comment
Question by:CptPicard
[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
  • 7
  • 7
14 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39759558
First question, why are your Exit and Err lines titled DblClick but the sub is Click?

At the beginning of that click event, you need to insert:

strCriteria = "ContactID=" & me.ListBox.Column(0)
if dcount("*","tbl1", "ContactID=" & me.ListBox.Column(0)) > 0 then
   msgbox "Contact ID already exists in that form!"
   Exit Sub
end if
0
 

Author Comment

by:CptPicard
ID: 39759596
I don't actually know why it's showing that.  I need to tidy this up...  And that worked, thank you.

But with first my bit of code (below) I want it to remove the ContactID after you click OK on the message box that pops up so I thought adding 'me.cboContactID.value = Null' would work, but it doesn't.  Any ideas?  Otherwise the user will have to click on the ESC button on the keyboard each time to clear the data in that field.

---
Private Sub cboContactID_BeforeUpdate(Cancel As Integer)

If DCount("*", "tbl1", "ContactID=" & Me.cboContactID) > 0 Then
   MsgBox "Contact ID already exists in that form!"
   me.cboContactID.Value = Null
   Cancel = True
End If

End Sub
---
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39759624
"I need to tidy this up"
Just housekeeping, but important!

Wait a second, you have backed up to your original code again, not the listbox code.  Are you talking about clearing the combo in the original form?

me.cboContactID = Null

should work.  Or are you talking about the listbox?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:CptPicard
ID: 39759645
Yes the combobox not the listbox.  For some reason when I put

me.cboContactID = Null

within the code, it doesn't clear the value for cboContactID and instead gives me a run-time error '-s147352567 (800200009)':

Also, very annoying it keeps creating blank records. :(
0
 

Author Comment

by:CptPicard
ID: 39759663
Is it because I have this in my afterupdate?

Private Sub cboContactID_AfterUpdate()
       
        Me![cboTitle] = Me![cboContactID].Column(1)
        Me![cboFirstName] = Me![cboContactID].Column(2)
        Me![cboLastName] = Me![cboContactId].Column(3)
        Me![cboDOB] = Me![cboContactID].Column(4)
        Me![cboTelephone] = Me![cboContactID].Column(5)
        Me.Refresh
       
End Sub
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39759668
Ok, see the problem.  You are trying to reset the value of that control in its BeforeUpdate event, which you cannot do.

I generally don't like to use the control BeforeUpdate event for this type of stuff.  Instead, move that to the Control AfterUpdate event.  Assuming that the form is in Add record (data entry) mode and on a New record then this should work.

Private Sub cboContactID_AfterUpdate

    If DCount("*", "tbl1", "ContactID=" & Me.cboContactID) > 0 Then
       MsgBox "Contact ID already exists in that form!"
       me.undo   'this would undo the entire record
       'me.cboContactID = Null          'This would only set the combo back to NULL
    End If

End Sub
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39759678
With the other stuff in the AfterUpdate I would simply add an ELSE before the EndIf in my previous code and insert the code you just posted between the ELSE and the End IF
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39759686
Hopefully, all of those other controls you are filling in are not bound controls.  You should not be storing that contact info in multiple locations, just the ContactID should be stored in the recordsource that populates that form.

If you want to display all of the contact info associate with the combo, I'd put a subform on the control, bound to the ContactID, and display the data that is in your Contacts table in that subform.
0
 

Author Comment

by:CptPicard
ID: 39759701
Works fantastically!!   Thanks for your time.  Will look into adding a subform at a later stage.
0
 

Author Comment

by:CptPicard
ID: 39759770
I've just noticed that after putting this code in the after update for the combo box and when I enter a ContactID and then try removing the ContactID from the box I get an error message saying

Run-time error '3075':
Syntax error (missing operator) in query expression 'ContactID=".

This is the code below.

---------
Private Sub cboContactID_AfterUpdate()

If DCount("*", "tbl1", "ContactID=" & Me.cboContactID) > 0 Then
       MsgBox "This ContactID has already been added!"
       Me.Undo
   
    Else
   
        Me![cboTitle] = Me![cboContactID].Column(1)
        Me![cboFirstName] = Me![cboContactID].Column(2)
        Me![cboLastName] = Me![cboContactID].Column(3)
        Me![cboDOB] = Me![cboContactID].Column(4)
        Me![cboTelephone] = Me![cboContactID].Column(5)

Me.Refresh
   
    End If

End Sub
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39759822
What line gets highlighted when that error occurs?

Not sure why you are including the Me.Refresh line in  your code?

You only need that to refresh the data that was in the underlying recordsource of your form when it was opened.
0
 

Author Comment

by:CptPicard
ID: 39759977
On this line

If DCount("*", "tbl1", "ContactID=" & Me.cboContactID) > 0 Then
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39760006
That would normally indicate that the combo is empty.  Try changing:

Private Sub cboContactID_AfterUpdate()

If DCount("*", "tbl1", "ContactID=" & Me.cboContactID) > 0 Then

to

Private Sub cboContactID_AfterUpdate()

if me.cboContactID & "" = "" then Exit Sub  
   
If DCount("*", "tbl1", "ContactID=" & Me.cboContactID) > 0 Then

this would check to see whether the combo is empty and if so, would exit the AfterUpdate event.  Did you get rid of the me.refresh line?
0
 

Author Comment

by:CptPicard
ID: 39760072
Yep I got rid of the me.refresh line.  Works now thx.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 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