Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox Code adding blank record

Status
Not open for further replies.

avagodro

Technical User
Aug 12, 2005
83
US
I have the following code on a button control on a subform:

Dim db As Database: Set db = CurrentDb
Dim frm As Form
Dim ctl As Control
Dim varItem As Variant
Dim SQLstr As String

Set frm = Forms![frmQuestionaire]![frmQuestions]![frmResponses Subform].Form
Set ctl = frm!ListRspns

For Each varItem In Me.ListRspns.ItemsSelected
SQLstr = "INSERT INTO tblResponses(ResponseDate, QstnID, AssociateRef, Rspns) " _
& "SELECT #" & Me.ResponseDate & "#, " & Me.QstnID & ", ' " & Me.AssociateRef & " ', " _
& "'" & ctl.ItemData(varItem) & "';"
Debug.Print SQLstr
db.Execute SQLstr
CurrentDb.TableDefs.Refresh
DoEvents
Next varItem
DoCmd.GoToRecord , , acNewRec

Forms![frmQuestionaire]![frmQuestions].Form![Command38].SetFocus
SendKeys "~"

[Command38] is a button control on the main form that is for the next question in the series.
My problem is that whenever the selections are saved using the code above, Access adds the items selected, but also adds a blank record.
Any suggestions? Thoughts?
 
What happens if you remove the [tt]DoCmd.GoToRecord , , acNewRec[/tt] ?

What's the sendkeys for?

Roy-Vidar
 
My mistake. That actually wasn't on the original code. I was doing some testing and forgot to remove that. Even without it, it still adds the blank record. The blank record is ALWAYS at the end.
 
It is something to do with the moving to the next question on the main form after the saving code is run. If I remove
Forms![frmQuestionaire]![frmQuestions].Form![Command38].SetFocus
SendKeys "~"

I don't get the extra record.
 
I found the problem. I had mistakenly had a control source on the listbox, which wasn't needed since the code is doing the saving.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top