Connecting Tech Pros Worldwide Forums | Help | Site Map

MS Access 2002: Go to newly added record from form2 to form 1

Kaur
Guest
 
Posts: n/a
#1: Mar 16 '06
Hi,

I would appriceate any help to correct the code error that I am getting
for the onclick event of a cmd button. I have two forms. Main Form
"frmQuestion" and form 2 "SfrmQuestion". FrmQuestion shows the details
about Question and by clicking on one for the cmd button on this form
opens up SfrmQuestions that lets me add new questions. The frmQuestion
Form remains opened at the backend. My problem is when I save the
question in srmQuestion I want to go to the same added record in
frmQuestion by clicking on saveRecord cmd button which closes the
sfrmquestion Form. This is the code I have in sfrmQuestion's cmdbutton
cmdSaveRecord. I keep on getting an error

Private Sub cmdSaveRecord_Click()
DoCmd.Save
'Dim rs As DAO.Recordset

With Forms![frmQuestion]
..Requery
Set rs = Forms![frmQuestion].RecordsetClone

rs.FindFirst "QuestionID = " & Me.QuestionID
If rs.NoMatch Then
MsgBox "Not found."
Else
rs.Bookmark = Me.Bookmark
End If
Set rs = Nothing
End With

DoCmd.Close



End Sub




Anthony England
Guest
 
Posts: n/a
#2: Mar 16 '06

re: MS Access 2002: Go to newly added record from form2 to form 1



"Kaur" <kaurm@saic.com> wrote in message
news:1142515649.929533.251580@j52g2000cwj.googlegr oups.com...[color=blue]
> Hi,
>
> I would appriceate any help to correct the code error that I am getting
> for the onclick event of a cmd button. I have two forms. Main Form
> "frmQuestion" and form 2 "SfrmQuestion". FrmQuestion shows the details
> about Question and by clicking on one for the cmd button on this form
> opens up SfrmQuestions that lets me add new questions. The frmQuestion
> Form remains opened at the backend. My problem is when I save the
> question in srmQuestion I want to go to the same added record in
> frmQuestion by clicking on saveRecord cmd button which closes the
> sfrmquestion Form. This is the code I have in sfrmQuestion's cmdbutton
> cmdSaveRecord. I keep on getting an error
>
> Private Sub cmdSaveRecord_Click()
> DoCmd.Save
> 'Dim rs As DAO.Recordset
>
> With Forms![frmQuestion]
> .Requery
> Set rs = Forms![frmQuestion].RecordsetClone
>
> rs.FindFirst "QuestionID = " & Me.QuestionID
> If rs.NoMatch Then
> MsgBox "Not found."
> Else
> rs.Bookmark = Me.Bookmark
> End If
> Set rs = Nothing
> End With
>
> DoCmd.Close
>
>
>
> End Sub[/color]



You don't say what the error is, but if a sub may generate an error then you
should put some basic error handling in. I am 95% sure I can see what
you're doing but wonder why the form is named "SfrmQuestion" hopefully this
has absolutely nothing to do with subforms and is simply some type of popup
data-entry form.
If so, then you could code like this:


Private Sub cmdSaveRecord_Click()

On Error GoTo Err_Handler

Dim strFind As String
Dim lngQID As Long

' Save this record if it needs it
If Me.Dirty = True Then
Me.Dirty = False
End If

' Get the current QuestionID
lngQID = Nz(Me!QuestionID, 0)

' Check we have a valid ID
If lngQID > 0 Then
DoCmd.Close acForm, Me.Name
Else
Exit Sub
End If

With Forms!frmQuestion
.Requery
strFind = "QuestionID=" & CStr(lngQID)
.RecordsetClone.FindFirst strFind
If .RecordsetClone.NoMatch Then
MsgBox "Cannot locate record"
Else
.Bookmark = .RecordsetClone.Bookmark
End If
.SetFocus
End With

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Kaur
Guest
 
Posts: n/a
#3: Mar 16 '06

re: MS Access 2002: Go to newly added record from form2 to form 1


Thanks for such a quick response. I updated the code. Now I am getting
the error: Searching records using the list box can not find the form
frmQuestion referred to in a macro expression or visual basic code."
May be I did not give enough details of the forms.
Form 1 is a search form to search for all the surveys in db. It has an
unbound list box that displays all the surveys. Clicking on one of the
surveys opens up 2nd form.
This form "frmQuestions" has all the questions related to the survey.
This form has an unbound list box that displays all the relevant
questions. Underneath the list box is Question text in combo box,
question no as it appers in the survey (a text box). This form has a
subForm that displays the subquestions associated with each Question.
The form "frmQuestion" has a cmd button which opens the Form3
"frmAddQuestions".
This form gets the survey key from tblSurvey and has a subform called
sfrmAddQuestions where users does the data entry for a question. I
created this type of form so that I can still add the question for the
relevant Survey.[color=blue]
>From here users choose the questions from a combo box and if the[/color]
question is not in the combo box then it allows the users to add the
question. This form has cmdSaveRecord cmd button where I added the
code and still getting the error.

Any help would be appreciated.

Anthony England
Guest
 
Posts: n/a
#4: Mar 16 '06

re: MS Access 2002: Go to newly added record from form2 to form 1



"Kaur" <kaurm@saic.com> wrote in message
news:1142520463.184963.234240@p10g2000cwp.googlegr oups.com...[color=blue]
> Thanks for such a quick response. I updated the code. Now I am getting
> the error: Searching records using the list box can not find the form
> frmQuestion referred to in a macro expression or visual basic code."
> May be I did not give enough details of the forms.
> Form 1 is a search form to search for all the surveys in db. It has an
> unbound list box that displays all the surveys. Clicking on one of the
> surveys opens up 2nd form.
> This form "frmQuestions" has all the questions related to the survey.
> This form has an unbound list box that displays all the relevant
> questions. Underneath the list box is Question text in combo box,
> question no as it appers in the survey (a text box). This form has a
> subForm that displays the subquestions associated with each Question.
> The form "frmQuestion" has a cmd button which opens the Form3
> "frmAddQuestions".
> This form gets the survey key from tblSurvey and has a subform called
> sfrmAddQuestions where users does the data entry for a question. I
> created this type of form so that I can still add the question for the
> relevant Survey.[color=green]
>>From here users choose the questions from a combo box and if the[/color]
> question is not in the combo box then it allows the users to add the
> question. This form has cmdSaveRecord cmd button where I added the
> code and still getting the error.
>
> Any help would be appreciated.[/color]



My head is starting to ache with all that! I re-read twice and still don't
quite get it. Perhaps someone else can step in, but in any case, use the
bulk of my code with the structure and error handling. You could also use
the following function to check whether you have the expected form open,
before you call Forms!MyForm ... blah,blah,blah

Public Function IsFormLoaded(strName As String)
IsFormLoaded = (SysCmd(acSysCmdGetObjectState, acForm, strName) <> 0)
End Function






Private Sub cmdSaveRecord_Click()

On Error GoTo Err_Handler

Dim strFind As String
Dim lngQID As Long

' Save this record if it needs it
If Me.Dirty = True Then
Me.Dirty = False
End If

' Get the current QuestionID
lngQID = Nz(Me!QuestionID, 0)

' Check we have a valid ID
If lngQID > 0 Then
DoCmd.Close acForm, Me.Name
Else
Exit Sub
End If

MsgBox "Question ID=" & Cstr(lngQID) & _
vbCrLf & "Now what?"


Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Closed Thread