General discussion

Locked

Help with form code

By carljohnman ·
I created a command button that opens a second form in order passing the primary key and closing only if you save the record. I figured if a user makes a mistake entering data on form 1 they could probably use a command button on form 2 to take themback to the same record on form 1 and open it passing the primary just like I done on form 1. I reversed the form names because the logic seems the same but form 1 always defaults to the first primary key when the back command is clicked on form 2. The code for Form1 and Form2 is below, Can someone please help me I am losing my mind? Everything works but the primary key passing from form 2 to form 1. Thanks very much in advance to anyone that can help and I need to mention I have very little experience with code.

Form 1 Command Next Code:

Private Sub Command28_Click()
On Error GoTo Err_Command28_Click

Dim stDocName As String
Dim stLinkCriteria As String

If Me.Dirty Then
MsgBox "Please click on the Save Button", vbOKOnly
Else

stDocName = "Probatedb Test Form1"
DoCmd.OpenForm stDocName, acNormal, , , , , Me!
[Probatedb TestID]
DoCmd.Close acForm, Me.Name
End If

Exit_Command28_Click:
Exit Sub

Err_Command28_Click:
MsgBox Err.Description
Resume Exit_Command28_Click

End Sub

Form 2 Command Back Code:

Private Sub Command34_Click()
On Error GoTo Err_Command34_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Probatedb Test Information Form"
DoCmd.OpenForm stDocName, acNormal, , , , , Me!
[Probatedb TestID]
DoCmd.Close acForm, Me.Name
Exit_Command34_Click:
Exit Sub

Err_Command34_Click:
MsgBox Err.Description
Resume Exit_Command34_Click


End Sub

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Help with form code

by nikki96 In reply to Help with form code

There is no "passing" of data between forms if I recall correctly. When I needed to do that, I set a global variable in the first form to a value, then overwrote that value from the second form if needed.

So, in the first form, at the top, you have something like
Dim myPK as String
myPK = ""

Then, on the second form, if you need to change the value, you say something like
myForm1.myPrimaryKey = myNewPrimaryKey

Both forms must be live, but you said they were, so it shouldn't be a problem.

On the first form then to get the value you do
If myPrimaryKey = "" Then
' normal code, nothing was passed in
Else
'use the value of the new PK
End If

Does that help?

Nikki

Collapse -

Help with form code

by carljohnman In reply to Help with form code

Poster rated this answer

Collapse -

Help with form code

by carljohnman In reply to Help with form code

This question was closed by the author

Back to Software Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums