Hello All!
I'm new using Access 2007 (& VBA) and i am having trouble with the following:
What I'm trying to do is this:
I have a form (frmOpportunityDetails) to view and change the details of an opportunity from the table tblProspect.
I have made a button on this form called "cmdConvertToCustomer".
When the user clicks this button, Access should:
- first check if the company name (field "CompanyName") already exists in the tblCustomer. Because it is possible to have the same company is the table tblProspect and tblCustomer. (We could do business with someone in the region "TC 1" and have a opportunity to do business with this someone in the region "TC 2")
- When the name does not already exists, an append query will transfer the data from the table tblProspect to tblCustomer.
- When the name does exists, a message box should appear saying "This customer already exists, change info manually".
- Then the form "frmCustomerDetails" should open so the user can update the info AND the opportunity should be deleted from the tblProspect.
I am having trouble to make the vba code work...
Maybe someone could have a look at it and give me some advise?
Thanks in advance!
I'm new using Access 2007 (& VBA) and i am having trouble with the following:
What I'm trying to do is this:
I have a form (frmOpportunityDetails) to view and change the details of an opportunity from the table tblProspect.
I have made a button on this form called "cmdConvertToCustomer".
When the user clicks this button, Access should:
- first check if the company name (field "CompanyName") already exists in the tblCustomer. Because it is possible to have the same company is the table tblProspect and tblCustomer. (We could do business with someone in the region "TC 1" and have a opportunity to do business with this someone in the region "TC 2")
- When the name does not already exists, an append query will transfer the data from the table tblProspect to tblCustomer.
- When the name does exists, a message box should appear saying "This customer already exists, change info manually".
- Then the form "frmCustomerDetails" should open so the user can update the info AND the opportunity should be deleted from the tblProspect.
I am having trouble to make the vba code work...
Maybe someone could have a look at it and give me some advise?
Thanks in advance!
Code:
Private Sub cmdConvertToCustomer_Click()
Dim intAnswer As Integer
Dim str As String
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim strSQL As String
Dim rstProspect As Recordset
Set ws = DBEngine(0)
Set db = ws(0)
Set rstProspect = Me.Recordset
If DCount("*", "[tblCustomer]", "[CompanyName] = " & CompanyName) > 0 Then
str = "Open details form + delete the opportunity"
intAnswer = MsgBox(str, vbInformation + vbOKCancel, "Info")
End If
If intAnswer = vbOK Then
DoCmd.OpenForm "frmActiveCustomers"
DoCmd.OpenQuery "qryDeleteProspect"
CurrentDb.Execute "DELETE * FROM tblCompany WHERE CompanyID = " & Me.CompanyID
Else
str = "This will convert the opportunity to a customer"
intAnswer = MsgBox(str, vbInformation + vbOKCancel, "Info")
End If
If intAnswer = vbOK Then
strSQL = "INSERT INTO tblCustomer ( CompanyID ) VALUES (" & rstProspect("CompanyID") & ")"
db.Execute strSQL, dbFailOnError
CurrentDb.Execute "DELETE * FROM tblProspect WHERE CompanyID = " & Me.CompanyID
DoCmd.Close
Else
Exit Sub
End If
End Sub