Open existing or new record

W

Wayne

Here's the code I have so far:

Private Sub OpenPrintReq_Click()
On Error GoTo Err_OpenPrintReq_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "PrintRequisition"

If IsNull([JobNumber]) Then
MsgBox "You must select a Job #."
DoCmd.GoToControl "JobNumber"
Else
stLinkCriteria = "[ProjectID]=" & "'" &
Me![JobNumber] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria,
acFormPropertySettings
Me.Visible = False

End If

Exit_OpenPrintReq_Click:
Exit Sub

Err_OpenPrintReq_Click:
MsgBox Err.Description
Resume Exit_OpenPrintReq_Click

End Sub

When i select a ProjectID from the JobNumber combo and
click the open button it opens the PrintRequistion form
containing the record with the selected ProjectID.
Here's where I need some help....
If there is no record in the PrintRegister table containing
the selected ProjectID, I want it to automatically create a
new record with that ProjectID.

I hope someone can help me with this.

Thanks
 
D

Dirk Goldgar

Wayne said:
Here's the code I have so far:

Private Sub OpenPrintReq_Click()
On Error GoTo Err_OpenPrintReq_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "PrintRequisition"

If IsNull([JobNumber]) Then
MsgBox "You must select a Job #."
DoCmd.GoToControl "JobNumber"
Else
stLinkCriteria = "[ProjectID]=" & "'" &
Me![JobNumber] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria,
acFormPropertySettings
Me.Visible = False

End If

Exit_OpenPrintReq_Click:
Exit Sub

Err_OpenPrintReq_Click:
MsgBox Err.Description
Resume Exit_OpenPrintReq_Click

End Sub

When i select a ProjectID from the JobNumber combo and
click the open button it opens the PrintRequistion form
containing the record with the selected ProjectID.
Here's where I need some help....
If there is no record in the PrintRegister table containing
the selected ProjectID, I want it to automatically create a
new record with that ProjectID.

I hope someone can help me with this.

Thanks

Add a few lines after opening form PrintRequisition:

' ...
DoCmd.OpenForm stDocName, , , stLinkCriteria
Me.Visible = False

' If no record exists for this JobNumber,
' create one.
With Forms(stDocName)
If .NewRecord Then
!ProjectID = Me!JobNumber
End If
End With

End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top