我有一个包含名称列表的列表框。当您选择一个名字,然后单击一个按钮时,它将把该人标记为此处。单击按钮将调用一个子项,该子项用于在出勤表上运行更新查询并将座位列更改为true。然后,它还应该将该人的名字从列表框中移到第二个,仅显示已被标记为就座的人。我不断收到错误消息,似乎给变量(agent)提供了空值,但我不知道为什么。
为澄清起见:“出勤”是表单的名称,“座席名称”是列表框的名称。
Sub markseated()
Dim itemIndex As Long
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim SQLStr As String
Dim agent As Variable
Set agent = Attendance.Agentname.value
Dim rs As ADODB.Recordset
Set rs = Cn.Execute(SQLStr)
Server_Name = "SDL02-VM25"
Database_Name = "PIA"
SQLStr = "Update dbo.[Attendance]Set [Seated] = 'True' Where [AgentName] ='" & agent & "'"
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & vbNullString
rs.Open
Cn.Close
With Attendance.Agentname
For itemIndex = .ListCount - 1 To 0 Step -1
If .Selected(itemIndex) Then
Attendance.Seated.AddItem .list(itemIndex)
.RemoveItem itemIndex
Attendance.Agentname.ListIndex = -1
End If
Next itemIndex
End With
End Sub