Ceate record if doesnt exist

W

Wayne Livingstone

I am using a form with an unbound combo to select a
JobNumber/ProjectID and use this value to open another form
at the record that contains that value. The ProjectID field
is not the primary key but it is unique to each record.
My problem is that if no record exists containing the
selected ProjectID then one should be created and displayed
in the form I am opening. I have the following code on the
On Click event of a command button on the first form:

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

' If no record exists for this JobNumber,
' create one.
With Forms(stDocName)

If .NewRecord Then
!ProjectID = Me!JobNumber
End If
End With
End If

Exit_OpenPrintReq_Click:
Exit Sub

Err_OpenPrintReq_Click:
MsgBox Err.Description
Resume Exit_OpenPrintReq_Click

End Sub

I'm sure this used to work last time I checked, but now if
I select a ProjectID that doesnt exist the new form gets
opened completly blank. It doesnt show any fields, just the
background image. And a new record is not entered into the
table.
I dont know where to begin looking for the problem.
 
M

Max Smart

You could try setting the Default Value of the ProjectID control to
[Forms]![FirstForm]![JobNumber]. It's simpler since it doesn't require any
code.

Max
 

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