VB编写的程序中其中一个窗体代码看不懂,请大神帮帮忙(1)
接着上一段的代码PrivateSubFNDtoMSH()DimstrSQLAsStringstrSQL=""IfCheck1.Value=1ThenstrSQL="whe...
接着上一段的代码
Private Sub FNDtoMSH()
Dim strSQL As String
strSQL = ""
If Check1.Value = 1 Then
strSQL = " where 基站编号 ='" & Trim(Text1(0)) & "'"
End If
If Check3.Value = 1 And strSQL = "" Then
strSQL = " where 基站名称 like '%" & Trim(Text1(1)) & "%'"
ElseIf Check3.Value = 1 And strSQL <> "" Then
strSQL = strSQL & " and 基站名称 like '%" & Trim(Text1(1)) & "%'"
End If
If Check2.Value = 1 And strSQL = "" Then
strSQL = " where 基站类型 = '" & Trim(Combo1) & "'"
ElseIf Check2.Value = 1 And strSQL <> "" Then
strSQL = strSQL & " and 基站类型 = '" & Trim(Combo1) & "'"
End If
Dim rs As New ADODB.Recordset
If strSQL = "" Then
rs.Open "select * from 基础信息", Cnn
Else
rs.Open "select * from 基础信息 " & strSQL, Cnn
End If
Set MSH.DataSource = rs
rs.Close
Set rs = Nothing
MSH.ColWidth(0) = 1000
MSH.ColWidth(1) = 2000
MSH.ColWidth(2) = 1000
MSH.ColWidth(3) = 3000
MSH.ColWidth(6) = 1000
End Sub
请大神逐一分析,每段代码留着注释 展开
Private Sub FNDtoMSH()
Dim strSQL As String
strSQL = ""
If Check1.Value = 1 Then
strSQL = " where 基站编号 ='" & Trim(Text1(0)) & "'"
End If
If Check3.Value = 1 And strSQL = "" Then
strSQL = " where 基站名称 like '%" & Trim(Text1(1)) & "%'"
ElseIf Check3.Value = 1 And strSQL <> "" Then
strSQL = strSQL & " and 基站名称 like '%" & Trim(Text1(1)) & "%'"
End If
If Check2.Value = 1 And strSQL = "" Then
strSQL = " where 基站类型 = '" & Trim(Combo1) & "'"
ElseIf Check2.Value = 1 And strSQL <> "" Then
strSQL = strSQL & " and 基站类型 = '" & Trim(Combo1) & "'"
End If
Dim rs As New ADODB.Recordset
If strSQL = "" Then
rs.Open "select * from 基础信息", Cnn
Else
rs.Open "select * from 基础信息 " & strSQL, Cnn
End If
Set MSH.DataSource = rs
rs.Close
Set rs = Nothing
MSH.ColWidth(0) = 1000
MSH.ColWidth(1) = 2000
MSH.ColWidth(2) = 1000
MSH.ColWidth(3) = 3000
MSH.ColWidth(6) = 1000
End Sub
请大神逐一分析,每段代码留着注释 展开
1个回答
展开全部
Private Sub FNDtoMSH()
'定义一个字符串变量strSQL
Dim strSQL As String
’首先初始化字符串变量strSQL为空
strSQL = ""
'如果基站编号前面的那个勾选框选中的话,给strSQL赋值
If Check1.Value = 1 Then
strSQL = " where 基站编号 ='" & Trim(Text1(0)) & "'"
End If
'如果基站名称前面那个勾选框选中的话,同时基站编号没有勾选,给strSQL赋值
If Check3.Value = 1 And strSQL = "" Then
strSQL = " where 基站名称 like '%" & Trim(Text1(1)) & "%'"
'如果基站名称前面那个勾选框选中的话,同时基站编号勾选,给strSQL赋值
ElseIf Check3.Value = 1 And strSQL <> "" Then
strSQL = strSQL & " and 基站名称 like '%" & Trim(Text1(1)) & "%'"
End If
'如果基站类型前面的勾选框选中的话,且基站编号和基站名称均没有选中
If Check2.Value = 1 And strSQL = "" Then
strSQL = " where 基站类型 = '" & Trim(Combo1) & "'"
'如果选中基站类型且选中了其他的选项的话,
ElseIf Check2.Value = 1 And strSQL <> "" Then
strSQL = strSQL & " and 基站类型 = '" & Trim(Combo1) & "'"
End If
'以上是为了得到SQL语句的后半部分
'定义一个Adodb连接数据库
Dim rs As New ADODB.Recordset
'如果三个勾选框均没有选中的情况,则显示所有的基础信息
If strSQL = "" Then
rs.Open "select * from 基础信息", Cnn
'否则,就显示过滤后的基础信息
Else
rs.Open "select * from 基础信息 " & strSQL, Cnn
End If
'设置MSH的数据源是过滤结果rs
Set MSH.DataSource = rs
'关闭过滤结果
rs.Close
Set rs = Nothing
'以下是设置MSH的每一列的宽度大小
MSH.ColWidth(0) = 1000
MSH.ColWidth(1) = 2000
MSH.ColWidth(2) = 1000
MSH.ColWidth(3) = 3000
MSH.ColWidth(6) = 1000
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询