Button to add contact (1 Viewer)

Robbin-R

student
Local time
Today, 06:31
Joined
Jul 13, 2010
Messages
9
Hi,

I've designed a form to add companies to a list of companies.
Now I would like to have a button on the form "Add contact",
so that a new contact person for the added company can be added.

The button opens the frmAddContact, in which the person's details can be
inserted.

However I would like to only open frmAddContact if the fields CompanyID,
CompanyName and CompanyType are filled in. If not, a message box
would appear stating "Not all fields are filled in."

Does anyone have an idea how to do this? It may either be done by
programming code or by using "Build up happing" (when right-mouse clicking the button in design mode, the top option).

Regards,
Robbin
 

vbaInet

AWF VIP
Local time
Today, 05:31
Joined
Jan 22, 2010
Messages
26,374
Robin, have you considered using a subform? The subform could serve as a display for the list of contacts that relates to a particular company. That way you don't have to worry about saving it because it will be done automatically for you.
 

Robbin-R

student
Local time
Today, 06:31
Joined
Jul 13, 2010
Messages
9
Well, now I have tried to use a subform, but that won't do, because form and subform will be related by CompanyID (a 5 letter abbreviation that serves as primary key). The subform will then only display the contacts that are marked with that CompanyID, however since you're adding the company, all fiels in the subform are grey and cannot be edited.
 

vbaInet

AWF VIP
Local time
Today, 05:31
Joined
Jan 22, 2010
Messages
26,374
So you would prefer a new form popping up correct?

Here's how:

1. You can use the WHERE argument of the OpenForm method to go that particular CompanyID
2. Open the form
3. Then create a new contact record
Code:
docmd.openform "Name of Form",,,"CompanyID = '" & Me![CompanyID] & "'"
docmd.runcommand accmdrecordnew
 

Robbin-R

student
Local time
Today, 06:31
Joined
Jul 13, 2010
Messages
9
I appreciate your effort, but obviously I have not been clear enough;)
Therefore, I now have uploaded some pics to explain what I meant.

I have two forms: frmAddContact and frmAddCompany:





Contacts can only be added to companies that are in the tblCompanies. So when a person form a new company has been approached,
first that new company needs to be added to tblCompanies.
This happens via frmAddCompany, and works fine for me.

Second, I've created a button "Add contact" on frmAddCompany that allows the user to Add a contact for this new company. On clicking the frmAddContact pops up correctly.
However I would like the frmAddContact only to pop up, if all fields in frmAddCompany are filled in.

So I guess I would have to create a condition for the included macro:

* Sorry for the Dutch

It would be something like =IF(AND(CompanyID<>"";CompanyName<>"";CompanyType<>"");(THEN)perform popping up; (ELSE)show message box "Fiels are not filled in correctly")

But I don't know how to implement this in Access.
 

vbaInet

AWF VIP
Local time
Today, 05:31
Joined
Jan 22, 2010
Messages
26,374
Right, got you. What you would need to do is put something like the following code in the BEFORE INSERT event of the FORM.
Code:
private sub form_[B]beforeinsert[/B]([COLOR=Magenta]cancel[/COLOR] as integer)
     If Len(CompanyID & "") = 0 OR Len(CompanyName & "") = 0 OR Len(CompanyType & "") = 0 OR Len(Country & "") = 0 then
           [COLOR=Magenta]cancel[/COLOR] = true
           msgbox "Please complete all boxes before proceeding", vbexclamation, "Incomplete form"
     end if
end sub
This would only affect new records, but for current records, you can put the same code in the BEFORE UPDATE event.
 

Users who are viewing this thread

Top Bottom