Open form to add mode or edit mode

Gasman

Enthusiastic Amateur
Local time
Today, 11:22
Joined
Sep 21, 2011
Messages
16,597
Hi all,
I have a form Clients, that has a button on it to add related Transfer records.
It opens the form in Add Mode by supplying the ClientID in Openargs and in the target form I have in the Load event

Code:
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then ' Passing in the client ID means we want a new record.
    DoCmd.GoToRecord , , acNewRec
    Me.ClientID = Me.OpenArgs
End If

End Sub
I have another button at present which will open same form, but filtered for the clientID with
Code:
Private Sub cmdTransfers_Click()
DoCmd.OpenForm "frmTransfer", , , "ClientID = " & Me.ClientID, , acDialog
End Sub

How could I enable one button to work in both modes?. What is the 'accepted' way to do this please?

TIA
 
Possibly a daft question - but how would the button "know" which mode you wanted to use?
 
Can't you just add an Else statement in Form load?
 
Possibly a daft question - but how would the button "know" which mode you wanted to use?

Not daft, but I was looking for an 'accepted' way of doing this.

Often I can work out a way of doing someting in Access only to be told 'All you needed to do was...'

I've gone with a Dlookup into the destination table and depending on that, then open form with one of the two calls above.
 
What I meant was probably what you ended up doing. What are the criteria that determine the form to be used.

A DLookup is a simple way to find a result and switch code to the appropriate action.
 
If it works fine but why couldn't you have just added an Else clause?
 
If it works fine but why couldn't you have just added an Else clause?
Colin,

What would be in the Else block of code.?
If there is no ClientID in the openArgs, what am I going to look for.?
If I pass the openArgs then that indicates I need to add a record.

Admittedly it would be better in the form load, then wherever the form is called, I do not have to repeat the tests.This method would be repeated for other forms.

So as it is now, I can goto the form, no record exists for that client, so I can Add one. I exit and then go back in later to see who the submitter was, or more crucially add another submitter. However, when I click the * to add a new record, I do not get the clientID in the combo, so the record would not be related to it's parent.

It is putting together the mechanism to handle the data entry that I struggle at.

Writing a bit of code to process a batch of data is ok for me, but the interaction between forms etc I still struggle with.
 
The simplest method is to use the options of the OpenForm method to open either with acFormAdd or acFormEdit depending on what you want to happen. Also, when you use acFormEdit, you almost certainly also want to use the where argument to open to a specific record or set of records.
 
Hi Pat
Yes finally I think I managed to resolve my problem. I will see with a lot of testing if I am correct.
I put acFormAdd and I make also where argument to open a specific record.
Ohh I was so stupid. :cautious:
thank you very much
 
Hi Gasman
how you resolve that problem?
Yes, but I cannot remember what I did. :(

OK, looking at the form I called the second form with
Code:
'Now check in form being called
DoCmd.OpenForm "frmSubmitterClient", , , "ClientID = " & Me.ClientID, , acDialog, Me.ClientID

and in the called form

Code:
Private Sub Form_Current()
If Me.NewRecord Then
    Me.ClientID = Me.OpenArgs
End If
End Sub

I think there are better ways of doing it, and I have seen one a while back, after I left that place of work, but cannot remember what it was.
 
Thank you for your explanation.
I will try also that if I will get in problems with "my" solution. 🤔
 
I *think* it was setting the parent key in the BeforeUpdate event of the form?
 
hmmm 🤔
I am opening the second form with on click event? I am opening with where argument like you say?


in my before update event i am using some validaton rule?
 
I open the form with both the where argument and openargs.
The where got me the existing records.
The openargs gave me the parent key when adding a new record.
 
Private Sub Form_Current()
If Me.NewRecord Then
Me.ClientID = Me.OpenArgs
End If
End Sub

Wrong. This code belongs in the Form's BeforeInsert event. You NEVER want to dirty a form before the user does.
Code:
Private Sub Form_BeforeInsert(Cancel As Integer)
    Me.ClientID = Me.OpenArgs
End Sub

There is NO reason for an If statement. The form's BeforeInsert event runs ONLY for new records and ONLY AFTER the user has typed a single character into any control. Therefore, the USER has signaled his intent to add data by typing something.

If you use this code in the Current event, you will be creating EMPTY records UNLESS you have proper validation code in the form's BeforeUpate event. Please test this yourself. No need to take my word for it. I'm just trying to keep you from adding bad data to the table. But, you should always have proper validation code in the form's BeforeUpdate event in any case, right:) So, if you have proper validation, you can get away with using the wrong event for this particular piece of code.
 
There you go @lacampeona :-)

I told you there was a better way of doing it, that I had seen since creating that DB.
I just got the event name wrong. :(
 
Hi yes
Thank you Pat and Gasman for cooperation. :love:
I check my events and my corection.

It is so nice to see good cooperation from you experts.
thank you 🤗
 

Users who are viewing this thread

Back
Top Bottom