Hi all,
I have a form with 1 unbound listbox as drop down list (entypolst), an unbound text box(entypotxt) and a command button. The list box reads items from a table. I want to change a value (text) on listbox, input a text on textbox so pressing the button add a new record in a table (Table1 fields Category,Product) showing in my form as subform (SFTable1) in datasheet view. For that reason a made the following code:
I take the error msg for 0 items selected in listbox and exit the sub. I can’t understand the problem.
I appreciate any help
Thanks in advance
I have a form with 1 unbound listbox as drop down list (entypolst), an unbound text box(entypotxt) and a command button. The list box reads items from a table. I want to change a value (text) on listbox, input a text on textbox so pressing the button add a new record in a table (Table1 fields Category,Product) showing in my form as subform (SFTable1) in datasheet view. For that reason a made the following code:
Code:
Private Sub AddBtn_Click()
Dim ans As Integer
Dim strSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim ctl As Control
Dim varItem As Variant
On Error GoTo ErrorHandler
Set db = CurrentDb()
If Me.entypolst.ItemsSelected.Count = 0 Then
MsgBox "0 items selected !"
Exit Sub
End If
Set rs = db.OpenRecordset("Table1", dbOpenDynaset, dbAppendOnly)
Me.entypolst.SetFocus
Me.entypolst.Dropdown
For Each varItem In Me.entypolst.ItemsSelected
rs.AddNew
rs![Category] = Me.entypolst.Column(0, varItem)
rs![Product] = Me.entypotxt
rs.Update
Next varItem
ans = Me.entypolst.ItemsSelected.Count
MsgBox Str(ans)
[SFTable1].Requery
MsgBox Str(ans)
ExitHandler:
Set rs = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
Select Case Err
Case Else
MsgBox Err.Description
DoCmd.Hourglass False
Resume ExitHandler
End Select
End Sub
I take the error msg for 0 items selected in listbox and exit the sub. I can’t understand the problem.
I appreciate any help
Thanks in advance