如何让整数在 combox.value 中接收 null?
How to allow integer receive null in combox.value?
Private Sub SubmitButton1_Click()
Dim month As Integer
Dim fromm As Integer
Dim too As Integer
month = monthComboBox.Value
fromm = fromComboBox.Value
too = toComboBox.Value
'''''''''''''''''''''''''''''''''''''''
'''''''''''''
'''''''''''''
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
If (IsNull(fromComboBox.Value)) And (IsNull(toComboBox.Value)) = True Then
sSQLSting = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month between " & fromm & " and " & too & " group by officer "
Set rs = Conn.Execute(sSQLSting)
Else
sSQLSting = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month= " & month & " group by officer " '
Set rs = Conn.Execute(sSQLSting)
End If
enter image
案例:
这里有 3 个组合框。用户应该 select "by month" 或“按月”。这 3 个组合框选项里面都是相同的 .additem 1 .additem 2 .additem 3 ... .additem 12
我用If (IsNull(fromComboBox.Value)) And (IsNull(toComboBox.Value)) = True Then A SQL A else-SQL-B
现在,它在 month = monthComboBox.Value
fromm = fromComboBox.Value
too = toComboBox.Value
上显示数据类型不匹配错误
在我看来,由于用户应该输入 "by month" 或 "by month period" ,一个 combobox.value 应该是 "" (字符串数据类型)。因此,它发生数据类型不匹配。(变量存储combobox.value是整数)
如何解决?
顺便说一句,If (IsNull(fromComboBox.Value)) And (IsNull(toComboBox.Value)) = True Then A SQL A else-SQL-B
,这个条件似乎并不完全有效。有什么想法吗?
更新
1)我使用这些代码解决了 null 不能传递整数的问题。不知道它是否有效。
Dim month As Integer
Dim fromm As Integer
Dim too As Integer
If IsNumeric(monthComboBox.Value) Then
month = monthComboBox.Value
Else
month = 0
End If
''''
If IsNumeric(fromComboBox.Value) Then
fromm = fromComboBox.Value
Else
fromm = 0
End If
'''''
If IsNumeric(toComboBox.Value) Then
too = toComboBox.Value
Else
too = 0
End If
2) if 条件无效。结果:"Else statement " 对 monthcombobox 有效; "Then statement no effect . In other words , only "按月组合框工作。"by month period"
没有输出
If ((IsEmpty(fromComboBox.Value) Or IsNull(fromComboBox.Value)) = True And (IsEmpty(toComboBox.Value)) Or IsNull(toComboBox.Value)) = True Then
sSQLSting = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month between " & fromm & " and " & too & " group by officer "
Set rs = Conn.Execute(sSQLSting)
Else
sSQLSting = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month= " & month & " group by officer " '
Set rs = Conn.Execute(sSQLSting)
End If
要检查 ComboBox
对象值选择,请使用其 ListIndex
属性
此外,您的逻辑需要增强,让您捕捉并使用任何 "valid" 用户选择并拒绝任何无效选择
最后你的代码有一些未声明的 and/or 未使用的变量:你最好在你的模块的最顶部使用 Option Explicit
并强制你声明所有变量:以这个小的代价额外的工作,您将获得对代码的更多控制,并使后续的调试和维护工作更快
对于上面的所有代码,请使用以下代码
Option Explicit
Private Sub SubmitButton1_Click()
Dim month As Integer
Dim fromm As Integer
Dim too As Integer
Dim sSQLString As String
With Me
Select Case True
Case .monthComboBox.ListIndex <> -1 '<--| a valid "by month" choice has been made, then use it to build consequent SQL string
month = monthComboBox.value
sSQLString = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month= " & month & " group by officer "
Case .fromComboBox.ListIndex <> -1 And .toComboBox.ListIndex <> -1 '<--| a valid "by month period" choice has been made, then use it to build consequent SQL string
fromm = fromComboBox.value
too = toComboBox.value
sSQLString = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month between " & fromm & " and " & too & " group by officer "
Case Else '<--| no vaild "month" choice hase been made, then inform the user about it
MsgBox "invalid months data choice!", vbInformation + vbExclamation
End Select
End With
If sSQLString = "" Then Exit Sub '<--| exit if no SQL string has been initilalized
Dim DBPath As String, sconnect As String
Dim Conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
DBPath = ThisWorkbook.FullName
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
Set rs = Conn.Execute(sSQLString)
'Dim sSQLQry As String '<--| are you actually using them?
'Dim ReturnArray '<--| are you actually using them?
End Sub
Private Sub SubmitButton1_Click()
Dim month As Integer
Dim fromm As Integer
Dim too As Integer
month = monthComboBox.Value
fromm = fromComboBox.Value
too = toComboBox.Value
'''''''''''''''''''''''''''''''''''''''
'''''''''''''
'''''''''''''
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
If (IsNull(fromComboBox.Value)) And (IsNull(toComboBox.Value)) = True Then
sSQLSting = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month between " & fromm & " and " & too & " group by officer "
Set rs = Conn.Execute(sSQLSting)
Else
sSQLSting = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month= " & month & " group by officer " '
Set rs = Conn.Execute(sSQLSting)
End If
enter image 案例: 这里有 3 个组合框。用户应该 select "by month" 或“按月”。这 3 个组合框选项里面都是相同的 .additem 1 .additem 2 .additem 3 ... .additem 12
我用If (IsNull(fromComboBox.Value)) And (IsNull(toComboBox.Value)) = True Then A SQL A else-SQL-B
现在,它在 month = monthComboBox.Value
fromm = fromComboBox.Value
too = toComboBox.Value
在我看来,由于用户应该输入 "by month" 或 "by month period" ,一个 combobox.value 应该是 "" (字符串数据类型)。因此,它发生数据类型不匹配。(变量存储combobox.value是整数)
如何解决?
顺便说一句,If (IsNull(fromComboBox.Value)) And (IsNull(toComboBox.Value)) = True Then A SQL A else-SQL-B
,这个条件似乎并不完全有效。有什么想法吗?
更新
1)我使用这些代码解决了 null 不能传递整数的问题。不知道它是否有效。
Dim month As Integer
Dim fromm As Integer
Dim too As Integer
If IsNumeric(monthComboBox.Value) Then
month = monthComboBox.Value
Else
month = 0
End If
''''
If IsNumeric(fromComboBox.Value) Then
fromm = fromComboBox.Value
Else
fromm = 0
End If
'''''
If IsNumeric(toComboBox.Value) Then
too = toComboBox.Value
Else
too = 0
End If
2) if 条件无效。结果:"Else statement " 对 monthcombobox 有效; "Then statement no effect . In other words , only "按月组合框工作。"by month period"
没有输出If ((IsEmpty(fromComboBox.Value) Or IsNull(fromComboBox.Value)) = True And (IsEmpty(toComboBox.Value)) Or IsNull(toComboBox.Value)) = True Then
sSQLSting = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month between " & fromm & " and " & too & " group by officer "
Set rs = Conn.Execute(sSQLSting)
Else
sSQLSting = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month= " & month & " group by officer " '
Set rs = Conn.Execute(sSQLSting)
End If
要检查 ComboBox
对象值选择,请使用其 ListIndex
属性
此外,您的逻辑需要增强,让您捕捉并使用任何 "valid" 用户选择并拒绝任何无效选择
最后你的代码有一些未声明的 and/or 未使用的变量:你最好在你的模块的最顶部使用 Option Explicit
并强制你声明所有变量:以这个小的代价额外的工作,您将获得对代码的更多控制,并使后续的调试和维护工作更快
对于上面的所有代码,请使用以下代码
Option Explicit
Private Sub SubmitButton1_Click()
Dim month As Integer
Dim fromm As Integer
Dim too As Integer
Dim sSQLString As String
With Me
Select Case True
Case .monthComboBox.ListIndex <> -1 '<--| a valid "by month" choice has been made, then use it to build consequent SQL string
month = monthComboBox.value
sSQLString = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month= " & month & " group by officer "
Case .fromComboBox.ListIndex <> -1 And .toComboBox.ListIndex <> -1 '<--| a valid "by month period" choice has been made, then use it to build consequent SQL string
fromm = fromComboBox.value
too = toComboBox.value
sSQLString = "SELECT officer From [big$] where officer is not null and officer <> '' and officer <> ' ' and Month between " & fromm & " and " & too & " group by officer "
Case Else '<--| no vaild "month" choice hase been made, then inform the user about it
MsgBox "invalid months data choice!", vbInformation + vbExclamation
End Select
End With
If sSQLString = "" Then Exit Sub '<--| exit if no SQL string has been initilalized
Dim DBPath As String, sconnect As String
Dim Conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
DBPath = ThisWorkbook.FullName
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
Set rs = Conn.Execute(sSQLString)
'Dim sSQLQry As String '<--| are you actually using them?
'Dim ReturnArray '<--| are you actually using them?
End Sub