Open form btn with filter; create if no record exist??Help??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a button on my main form frmOrders with the following in the onclick
event:

Private Sub btnFreight_Click()
On Error GoTo Err_btnFreight_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmOrderLoads"

stLinkCriteria = "[OrderID]=" & Me![OrderID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_btnFreight_Click:
Exit Sub

Err_btnFreight_Click:
MsgBox Err.Description
Resume Exit_btnFreight_Click

End Sub

It opens the frmOrderLoads form and displays the records related to current
OrderID.
However, what I want to acheive is add some lines to the above procedure so
that if there is no existing records in the frmOrderLoads for the current
OrderID then it should automatically create a new record with current
OrderID.....
Can anyone help me out on that please?

Thanks in advance.
 
Not sure that it is what you are looking for..

Private Sub btnFreight_Click()
On Error GoTo Err_btnFreight_Click

Dim stDocName As String
stDocName = "frmOrderLoads"
If DCount("*", "tblOrderLoads?", "OrderID=" & Me!OrderID)>0 Then
Dim stLinkCriteria As String
stLinkCriteria = "[OrderID]=" & Me![OrderID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
DoCmd.OpenForm stDocName, , , , acFormAdd
Forms!frmOrderLoads!OrderID = Me.OrdersID
End If

Exit_btnFreight_Click:
Exit Sub

Err_btnFreight_Click:
MsgBox Err.Description
Resume Exit_btnFreight_Click

End Sub
 
Back
Top