Determine if a item from listbox is selected

fernando.rosales

Registered User.
Local time
Today, 13:30
Joined
Jun 11, 2014
Messages
27
Hello guys I am having trouble with a If statement. I am trying to save a record into a table when an item is Selected (highlighted blue) in a list box. Keep in mind that my listbox is set to multiple selections. I have the following code but sometimes it works and sometimes it doest.

Please help. The reason why I am trying to get this to work is because when a item is selected I will add it to the table and when an item is deselected I will delete it from a table. See my code and pictures I have attached :


Private Sub list_audits_Click()
Dim strsql As String
Dim list As String
Dim id As String
id = Me.User_ID.Value
If Me.list_audits.Selected(0) = 1 Then

With Me.list_audits
list = .Column(0)

strsql = "INSERT INTO tbl_certification (User_ID, Audit) "
strsql = strsql & "values ('" & id & "', '" & list & "') "
DoCmd.RunSQL (strsql)

End With
End If

End Sub
 

Attachments

  • 1.jpg
    1.jpg
    100.8 KB · Views: 146
  • 2.jpg
    2.jpg
    57.3 KB · Views: 144
  • 3.jpg
    3.jpg
    36.7 KB · Views: 156
Is the problem that your if statement never triggers? Might be that in the line:
Code:
Me.list_audits.Selected(0) = 1
the .Selected returns a boolean, in which case you need to use -1 to test for truth.
Code:
Me.list_audits.Selected(0) = -1

Of course if .Selected(0) is actually returning a number then I think you'll need to be more specific about the problem you have with the code.
 

Users who are viewing this thread

Back
Top Bottom