以前从未遇到过。我使用VBA将一些列值从SQL Server带到列表框中。异常列之一是布尔值,因此它在SQL中的值为0或1。但是,当我将结果以VBA形式加载到列表框中时,它显示为-1。我在下面附加了我的代码。任何见解都会很棒。
Sub Getattendhistory()
database_connect
Dim SQLstr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim Counter As Long
Dim list As Object
Set list = AttendHistory.Results
SQLstr = "select [ID],[createddate],[notseatedreason],[exception],[exceptionreason] from dbo.[attendancehistory] where [agentname] = '" & SearchForm.Results.Text & "'"
rs.Open SQLstr, appconn, adOpenStatic
If (rs.BOF And rs.EOF) Then
MsgBox "No Attendance History.Contact Command Center"
Exit Sub
End If
With list
Counter = 0
Do Until rs.EOF
.AddItem
.list(Counter, 0) = rs![ID]
.list(Counter, 1) = rs![CreatedDate]
If IsNull(rs![NotSeatedReason]) Then .list(Counter, 2) = "Seated"
If Not IsNull(rs![NotSeatedReason]) Then .list(Counter, 2) = rs![NotSeatedReason]
If IsNull(rs![Exception]) Then .list(Counter, 3) = ""
If Not IsNull(rs![Exception]) Then .list(Counter, 3) = rs![Exception]
If IsNull(rs![Exceptionreason]) Then .list(Counter, 4) = "N/A"
If Not IsNull(rs![Exceptionreason]) Then .list(Counter, 4) = rs![Exceptionreason]
Counter = Counter + 1
rs.MoveNext
Loop
End With
rs.Close
database_Disconnect
Set rs = Nothing
End Sub
答案 0 :(得分:2)
由于在VBA中(通常在VB中),True存储为-1,false存储为0。仅由于SQL Server将True表示为1,将False表示为0,所以记录集中实际返回的是Boolean True或Boolean False。 。因此,当您显示Exception时,True表示为-1。您可能想要做的是根据布尔值有条件地显示字符串“ True”或“ False”。
这里是VB语言参考的链接。这适用于VB.Net,但相同的值和原因也适用于VB6和VBA。 https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/boolean-data-type