UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Command button with record checking    
 
   
gretsch
post Nov 24 2010, 07:33 AM
Post #1

UtterAccess Member
Posts: 45



Hi guys,

On one form, I need to open another form using a button. The catch is that the code must check whether a record has been created with that Project_ID. If it has, then it must display that record. If not then it must make a new record.

The db is attached. The button is called "Form 1" on the "Contract Details" tab of frm_Project_Details.

I need it to make a new record in tblContract_Record using frm_ContractRec_Details or, if the record already exists, then open that record.

I have searched this forum, trawled the net, and tried many things and hit many frustrating dead ends. It can't be that difficult, surely!

Andy
Attached File(s)
Attached File  SRLSA_24112010___Embeded_Form1.zip ( 511.86K ) Number of downloads: 4
 
Go to the top of the page
 
+
nschoonover
post Nov 24 2010, 09:53 AM
Post #2

UtterAccess Veteran
Posts: 494
From: New Hampshire



Try something like this:

CODE
Private Sub cmdMakeForm1_Click()
On Error GoTo cmdMakeForm1_Click_Err
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM tblContract_Records WHERE Project_ID = " & Project_number & ";", dbOpenDynaset, dbSeeChanges)
If Nz(rst.RecordCount, 0) > 0 Then
    'there is a record with the same projectId, so open the form to that project.
    DoCmd.OpenForm "frm_ContractRec_Details"
    Forms!frm_ContractRec_Details!Form.RecordSource = "SELECT * FROM tblContract_Records WHERE Project_ID = " & Project_number & ";"
Else
    'there are no matching projectids, so open the form to a new project.
    DoCmd.OpenForm "frm_ContractRec_Details", , , , acFormAdd
    
    Forms!frm_ContractRec_Details.Form.RecordSource = "SELECT * FROM tblContract_Records;"
    
End If

Set db = Nothing
Set rst = Nothing

cmdMakeForm1_Click_Exit:
    Exit Sub

cmdMakeForm1_Click_Err:
    MsgBox Error$
    Resume cmdMakeForm1_Click_Exit

End Sub


Good luck! (IMG:style_emoticons/default/smile.gif)
Nate
Go to the top of the page
 
+
Alan_G
post Nov 24 2010, 11:13 AM
Post #3

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,886
From: Devon UK



Hi

You could do something similar to Nate's suggestion, but condense it down a bit and not have to bother with the 'overhead' of using recordsets. The built in DCount() function could be used. Assuming the form you're opening is bound to the appropriate table/query and that the Project_ID is numerical, something along the lines of

CODE
Private Sub cmdMakeForm1_Click()
On Error GoTo cmdMakeForm1_Click_Err

If DCount("*","tblContract_Records","Project_ID = " & Me.Project_number)  > 0 Then
    'there is a record with the same projectId, so open the form to that project.
    DoCmd.OpenForm "frm_ContractRec_Details", , , "Project_ID = " & Me.Project_number
Else
    'there are no matching projectids, so open the form to a new project.
    DoCmd.OpenForm "frm_ContractRec_Details", , , , acFormAdd    
End If

cmdMakeForm1_Click_Exit:
    Exit Sub

cmdMakeForm1_Click_Err:
    MsgBox "Error Number: " & Err.Number & "  Description: " & Err.Description
    Resume cmdMakeForm1_Click_Exit

End Sub
Go to the top of the page
 
+
gretsch
post Nov 26 2010, 05:25 AM
Post #4

UtterAccess Member
Posts: 45



Awesome, thanks guys, it worked. As I'm sure you knew it would (IMG:style_emoticons/default/smile.gif)

(IMG:style_emoticons/default/thumbup.gif)
Go to the top of the page
 
+
Alan_G
post Nov 26 2010, 09:34 AM
Post #5

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,886
From: Devon UK



(IMG:style_emoticons/default/yw.gif) Glad we could help
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 25th May 2013 - 03:58 PM