Perhaps findfirst using the ID field?Thanks MajP. Now if anybody can just show me how to get the new record selected please than my weekend is made.
Thanks.
I am equally lost, but I think something is getting lost in translation.This is just crazy. What are you doing?
We seem to not understand what you are asking, because in our mind the solutions provided from the very beginning selected the new record.Thanks MajP. Now if anybody can just show me how to get the new record selected please than my weekend is made.
Thanks.
@MajP your method fails if the subform has a Required field.You are over thinking this.
Dim BT As String 'The BookTitle
Dim BID As Long 'The Book ID number
Dim BGID As Long 'The grade, the primary key for 1A
Dim BSID As Long 'The Book Stream, the primary key for Grade1
Dim PFN As String 'The Person's Full Name
Dim strSQL As String 'The append query with variables
BT = Trim(Me.BookTitleBox.Value)
BID = Nz(Me.BookIDOption.Value, 0)
BGID = Nz(Me.BookGradeOption.Value, 0)
BSID = Nz(Me.BookStreamOption.Value, 0)
PFN = Trim(Me.SurNameBox.Value)
strSQL = "INSERT INTO tblBooks ( BookTitle, BookID, GradeID, StreamID ) " & vbCrLf & _
"SELECT " & BT & " AS BookTitle, " & BID & " AS BookID, " & BGID & " AS GradeID, " & BSID & " AS StreamID;"
CurrentDb.Execute strSQL, dbFailOnError
@MajP your method fails if the subform has a Required field.
Your code fails on Update and will never go further to fill the fields.
If it was me, I would append the record with sql and re-query the subform.
That definitely does not solve the problem.Once I got started on the append query I couldn't stop. Enjoy the code below, it will solve your problem.
Private Sub List9_Click()
If IsNull(Me.subFrmStudents.Form.Team) Then
MsgBox "Team Required"
Else
Me.Dirty = False
' InsertRS
InsertSQL
End If
End Sub
Public Sub InsertRS()
Dim frm As Access.Form
Dim rs As DAO.Recordset
Set frm = Me.subFrmStudents.Form
Set rs = frm.Recordset
frm.Recordset.AddNew
frm.Dirty = False
frm.FirstName = Me.List9.Column(1, Me.List9.ListIndex)
frm.LastName = Me.List9.Column(2, Me.List9.ListIndex)
End Sub
Public Sub InsertSQL()
Dim strSql As String
Dim first As String
Dim last As String
Dim frm As Access.Form
Set frm = Me.subFrmStudents.Form
first = Me.List9.Column(1, Me.List9.ListIndex)
first = "'" & first & "'"
last = Me.List9.Column(2, Me.List9.ListIndex)
last = "'" & last & "'"
' strSql = "insert into tblPersons2 (FirstName, LastName) values (" & first & ", " & last & ")"
strSql = "insert into tblPersons2 (FirstName, LastName, Team) values (" & first & ", " & last & ", 'Team D')"
MsgBox strSql
CurrentDb.Execute strSql, dbFailOnError
MsgBox "Before Requery"
frm.Requery
MsgBox "Before MoveLast"
frm.Recordset.MoveLast
End Sub
Now we can understand why you said you have struggled in the past. It's because you are not using ACCESS in the manner it was designed to be used. If you are not connecting Primary and Foreign Keys in your tables, it's no wonder you are having trouble. You apparently are not using the relational part of ACCESS at all. And you will continue to struggle with this if you don't re-design this application properly.Hello all. I highly appreciate all your input. What I am asking might be confusing and maybe you do not see my logic.
I like LarryE's comment. Thanks Larry
"This is just crazy. What are you doing?"
I have design a text Book System originally without a scanner. It has been implemented by a school where I teach.
Now I am trying to modifying the application to include a scanner. I know it would be a better idea using foreign keys to fill in the fields of the books and the learners automatically but then I have to redo my forms and reports. These reports I struggled for more than a year to achieve.
I could not find any resource to help me with my dynamic crosstab queries. I manage to create a form using dynamic crosstab queries and it produces quite nice reports. I need to change the form into a report and there I will need all your help again.
So is there anybody who are able to make the new added record get selected let me know if it is possible please.
If not I will try to see how I can use MajP solution to match my requirements.
Speak to you guys again. Great job all of you. Thanks
With Me.frmStudentBooksSubform.Form.Recordset
.AddNew
.Update
End With
Me.frmStudentBooksSubform.SetFocus
DoCmd.RunCommand acCmdRecordsGoToLast
I thought maybe there was a glimmer of hope. We will see.I am out of this, just becoming a joke now.
Private Sub List9_Click()
If IsNull(Me.subFrmStudents.Form.Team) Then
MsgBox "Team Required"
Else
Me.Dirty = False
Select Case Me.frameMethod
Case 1
InsertRS
Case 2
InsertSQL
Case 3
InsertForm
End Select
End If
End Sub
Public Sub InsertRS()
Dim frm As Access.Form
Dim rs As DAO.Recordset
Set frm = Me.subFrmStudents.Form
Set rs = frm.Recordset
'Not passing the required field
frm.Recordset.AddNew
frm.Dirty = False
frm.FirstName = Me.List9.Column(1, Me.List9.ListIndex)
frm.LastName = Me.List9.Column(2, Me.List9.ListIndex)
End Sub
Public Sub InsertSQL()
Dim strSql As String
Dim first As String
Dim last As String
Dim frm As Access.Form
Set frm = Me.subFrmStudents.Form
first = Me.List9.Column(1, Me.List9.ListIndex)
first = "'" & first & "'"
last = Me.List9.Column(2, Me.List9.ListIndex)
last = "'" & last & "'"
' strSql = "insert into tblPersons2 (FirstName, LastName) values (" & first & ", " & last & ")"
strSql = "insert into tblPersons2 (FirstName, LastName, Team) values (" & first & ", " & last & ", 'Team A')"
MsgBox strSql
CurrentDb.Execute strSql, dbFailOnError
MsgBox "Before Requery"
frm.Requery
MsgBox "Before MoveLast"
frm.Recordset.MoveLast
End Sub
Public Sub InsertForm()
Dim frm As Access.Form
Set frm = Me.subFrmStudents.Form
Me.subFrmStudents.SetFocus
DoCmd.RunCommand acCmdRecordsGoToNew
frm.FirstName = Me.List9.Column(1, Me.List9.ListIndex)
frm.LastName = Me.List9.Column(2, Me.List9.ListIndex)
frm.Team = "Team A"
End Sub
Look at the example. It's the same as what I suggested in #3.code that will do this automatically when a button is clicked
Thanks . I will look at the db. I appreciate everyone's effort to assist me.Look at the example. It's the same as what I suggested in #3.
Of course you have to know
- what the desired bound subform is called,
- where the button to execute the instruction is located and
- depends on how the clean reference from button to subform is to be formulated.
The subform should of course be expandable, i.e. not have any specially set up locks. There should also be no additional and conflicting codes.
In the example: The GotoNewRecord button sets the focus on a new record. The SubformRequery button returns the focus to the first record.
Use this example to prove to me that this doesn't work!