open form and create new record and show with record id (1 Viewer)

megatronixs

Registered User.
Local time
Today, 04:26
Joined
Aug 17, 2012
Messages
719
Hi all,

I created a form where I want capture the complexity of a case. There are 11 questions to fill in with yes or no.
I want to open the form with the form connected to the review_id that is part of the form and is in the main form too.
if there was already a record created, then it would open with that one.
What I can't manage is to open the form, create new record to be filled in if there is no current record for that review_id.
The form I open has the following fields:
record_id (set as autonumber)
review_id (set as number)
question_1 to question_11 (set as text)

this is the code to open the form by clicking on a button:
Code:
Private Sub btn_complexity_check_1_Click()
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frm_complexity_check_l_1"
stLinkCriteria = "[Review_ID]=" & Me![Review_ID] 'listbox
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub

Any clue where I go wrong or miss?

Greetings.
 

static

Registered User.
Local time
Today, 03:26
Joined
Nov 2, 2015
Messages
823
If no record was found review_id will be null.
Setting a value automatically creates a record.

Code:
Private Sub btn_complexity_check_1_Click()
    DoCmd.OpenForm "frm_complexity_check_l_1", , , "[Review_ID]=" & review_id
    If IsNull(Forms!frm_complexity_check_l_1!review_id) Then _
			Forms!frm_complexity_check_l_1!review_id = review_id

End Sub
 

missinglinq

AWF VIP
Local time
Yesterday, 22:26
Joined
Jun 20, 2003
Messages
6,423
Here's some boilerplate code I did ages ago for a poster to do just that:

In the Primary Form:

Code:
Private Sub Go2SecondaryForm_Click()

 DoCmd.RunCommand acCmdSaveRecord

 If Nz(Me.RecordID,"") <> "" Then
  DoCmd.OpenForm "Secondary Form", , , , , , Me.RecordID
 Else
  MsgBox "A RecordID Must Be Entered First!"
 End If

End Sub

In the Secondary Form:

Code:
Private Sub Form_Load()

Dim rst As Recordset

If Nz(Me.OpenArgs,"") <> "" Then

 Set rst = Me.RecordsetClone
 
 rst.FindFirst "[RecordID] = " & Me.OpenArgs 
 
   If Not rst.NoMatch Then
      Me.Bookmark = rst.Bookmark
   Else
    DoCmd.GoToRecord , , acNewRec
    Me.RecordID = Me.OpenArgs
   End If

rst.Close
Set rst = Nothing

End If

End Sub

The above code assumes that RecordID is Numeric. If it is actually defined Text, replace the line

rst.FindFirst "[RecordID] = " & Me.OpenArgs

with

rst.FindFirst "[RecordID] = '" & Me.OpenArgs & "'"

You'll also need to replace Go2SecondaryForm with the actual name of your Command Button and SecondaryForm.

Linq ;0)>
 
Last edited:

megatronixs

Registered User.
Local time
Today, 04:26
Joined
Aug 17, 2012
Messages
719
Hi Static,
Your code works great, it just does what it needs in the correct way.
Misinlinq, I also will try your code to see how it works and learn from it.

Greetings.
 

Users who are viewing this thread

Top Bottom