K
Kathy R.
Access 2007
Player_tbl
playerID (autonumber, primary key)
player_name
Player_Trans_frm
Record Source: SELECT Player_tbl.player_name, Player_tbl.playerID
Controls:
PlayerID
player_name
cboFindPlayer
Trans_sub (subform parent/child on PlayerID)
I have an unbound combobox on a form that I use to jump to a particular
record. The code works fine as long as the player_name is already in
the table when I open the form.
I am trying to use the NotInList property to update the combobox and
jump to the new record that was just added. The following code adds the
new name to the combobox and underlying table, but I can't figure out
how to move to the newly created record on the form. Even when I choose
the new name from the combobox the form does not move to the new record.
I presume I need to requery something somewhere, but I don't know
where, or how.
I've also seen where you can pop up a new form to add the info and then
close it, updating the original form, but because all I need to do is
add the name, I thought that would be a more complicated solution to
simple problem. Although if that is the way to do it, I'll go back and
revisit it.
Your help would be very much appreciated!
Kathy R.
-------------------------------------------------------------
Private Sub cboFindPlayer_NotInList(NewData As String, Response As Integer)
Dim strSQL As String
Dim intAnswer As Integer
Dim NewPlayerName As String
'Exit this sub if the combo box is cleared
If NewData = "" Then Exit Sub
NewPlayerName = "'" & NewData & "' is not currently in the player
list." & vbCr & vbCr
NewPlayerName = NewPlayerName & "Do you want to add this name?"
intAnswer = MsgBox(NewPlayerName, vbQuestion + vbYesNo, "Add New
Player?")
If intAnswer = vbYes Then
strSQL = "Insert Into Player_tbl ([player_name]) " & _
"values ('" & NewData & "');"
CurrentDb.Execute strSQL, dbFailOnError
Response = acDataErrAdded
Else
Response = acDataErrContinue
End If
End Sub
---------------------------------------------------
The code I use to move to the record upon choosing it in the combobox in
case you need to see it too.
Private Sub cboFindPlayer_AfterUpdate()
Dim rs As Recordset
If Me.Dirty = True Then Me.Dirty = False
Set rs = Me.RecordsetClone
If Not IsNull(Me!cboFindPlayer) Then
rs.FindFirst "PlayerID = " & cboFindPlayer
If rs.NoMatch Then
Exit Sub
Else
Me.Bookmark = rs.Bookmark
Me.player_name.SetFocus
End If
End If
rs.Close
Set rs = Nothing
End Sub
Player_tbl
playerID (autonumber, primary key)
player_name
Player_Trans_frm
Record Source: SELECT Player_tbl.player_name, Player_tbl.playerID
Controls:
PlayerID
player_name
cboFindPlayer
Trans_sub (subform parent/child on PlayerID)
I have an unbound combobox on a form that I use to jump to a particular
record. The code works fine as long as the player_name is already in
the table when I open the form.
I am trying to use the NotInList property to update the combobox and
jump to the new record that was just added. The following code adds the
new name to the combobox and underlying table, but I can't figure out
how to move to the newly created record on the form. Even when I choose
the new name from the combobox the form does not move to the new record.
I presume I need to requery something somewhere, but I don't know
where, or how.
I've also seen where you can pop up a new form to add the info and then
close it, updating the original form, but because all I need to do is
add the name, I thought that would be a more complicated solution to
simple problem. Although if that is the way to do it, I'll go back and
revisit it.
Your help would be very much appreciated!
Kathy R.
-------------------------------------------------------------
Private Sub cboFindPlayer_NotInList(NewData As String, Response As Integer)
Dim strSQL As String
Dim intAnswer As Integer
Dim NewPlayerName As String
'Exit this sub if the combo box is cleared
If NewData = "" Then Exit Sub
NewPlayerName = "'" & NewData & "' is not currently in the player
list." & vbCr & vbCr
NewPlayerName = NewPlayerName & "Do you want to add this name?"
intAnswer = MsgBox(NewPlayerName, vbQuestion + vbYesNo, "Add New
Player?")
If intAnswer = vbYes Then
strSQL = "Insert Into Player_tbl ([player_name]) " & _
"values ('" & NewData & "');"
CurrentDb.Execute strSQL, dbFailOnError
Response = acDataErrAdded
Else
Response = acDataErrContinue
End If
End Sub
---------------------------------------------------
The code I use to move to the record upon choosing it in the combobox in
case you need to see it too.
Private Sub cboFindPlayer_AfterUpdate()
Dim rs As Recordset
If Me.Dirty = True Then Me.Dirty = False
Set rs = Me.RecordsetClone
If Not IsNull(Me!cboFindPlayer) Then
rs.FindFirst "PlayerID = " & cboFindPlayer
If rs.NoMatch Then
Exit Sub
Else
Me.Bookmark = rs.Bookmark
Me.player_name.SetFocus
End If
End If
rs.Close
Set rs = Nothing
End Sub