prevent duplicates when passing values between two forms (with Args)

40 Views Asked by At

I have two forms: transfert Form with its subform and intransfert Form. I am using

DoCmd.OpenForm "intransfert", , , , acFormAdd, acDialog, Me!Text83 

(where text83 is =[transfertasubform].[Form]![transfertadetailid] under

Private Sub Command78_Click() 

in transfet form and

Private Sub Form_Load()
On Error Resume Next
If Me.NewRecord Then Me!trnrin = Me.OpenArgs 

in intransfet form. intransfert form is based in transfertdetailquery. i wont to prevent passing text83 value more then one time

i tried to explain my problem and expect a help to prevent duplicates when used Arge

1

There are 1 best solutions below

2
mazoula On

assuming trnrin is the name of a variable in your record source. assuming you mean that you want to avoid adding two records where trnrin has the same value and the user sent the same open args twice. assuming trnrin is also the name of a control in the detail section of the intransfert form.

'form load only runs when the form is opened so you have to close the form to pass new args
'but you can just move the same code to a button or whatever

Private Sub IntransferForm_Load()
If Len(Me.OpenArgs) > 0 Then
Me.txtBoxintheHeader = Me.OpenArgs 'the load event is the right place to set controls 
'most of this code is to check if there is already an instance where trnrin = the OpenArgs in the record source
Dim lookedupArgs As String
lookedupArgs = Nz(lookedupArgs = DLookup("trnrin", "MyTable", "trnrin = " & "'" & Me.OpenArgs & "'"), "ValuethatisneveranArg")

If lookedupArgs = "ValuethatisneveranArg" Then

'Debug.Print "trnrin = '" & Me.OpenArgs & "'" 'note the string delimiters
'Me.trnrin = Me.OpenArgs 'this surprisingly works but will break easily and doesn't handle complex cases

Dim rs As Recordset
Set rs = Me.Recordset 'if recordset is bound to a table then table will also be updated.
'you can also bind to the table directly but you may need to call me.requery to show the changes
rs.AddNew
rs!trnrin = Me.OpenArgs
rs.Update
End If
End If
End Sub