主题:一个循环与变量的问题(已修改 附带源码与界面)
我的程序中间有一段需要循环 就是提取2次数据
界面图片在附件内
原始代码如下
Option Explicit
Private Sub Command1_Click()
Dim ConStr As String
Dim cn As New ADODB.Connection
Set cn = New ADODB.Connection
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\bf.Mdb"
cn.Open ConStr
Dim n As Integer
n = 1
If Combo2.Text = "111" And Text5.Text > 55 Then
MsgBox "被保人超龄!", vbOKOnly, "输入错误"
Exit Sub
End If
If Combo1.Text <> "" And Combo2.Text <> "" And Combo3.Text <> "" Then
Dim rs1 As New ADODB.Recordset
Set rs1 = New ADODB.Recordset
rs1.Open "Select * from " + Combo2.Text + " where age = " + Trim(Trim(Text5.Text)) + " and sex = '" + Combo1.Text + "' and year = " + Trim(Trim(Combo3.Text)) + "", cn, adOpenKeyset, adLockPessimistic
Text1.Text = rs1.Fields(5) * Text2.Text
rs1.Close
Set rs1 = Nothing
Else: Text1.Text = 0
End If
If Combo1.Text <> "" And Combo4.Text <> "" And Combo5.Text <> "" Then
Dim rs2 As New ADODB.Recordset
Set rs2 = New ADODB.Recordset
rs2.Open "Select * from " + Combo5.Text + " where age = " + Trim(Trim(Text5.Text)) + " and sex = '" + Combo1.Text + "' and year = " + Trim(Trim(Combo4.Text)) + "", cn, adOpenKeyset, adLockPessimistic
Text4.Text = rs2.Fields(5) * Text3.Text
rs2.Close
Set rs2 = Nothing
Else: Text4.Text = 0
End If
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Command3_Click()
Combo1.Text = ""
Combo2.Text = ""
Combo3.Text = ""
Combo4.Text = ""
Combo5.Text = ""
Text1.Text = 0
Text2.Text = 0
Text3.Text = 0
Text4.Text = 0
Text5.Text = 0
End Sub
Private Sub Form_Load()
Combo1.AddItem "男"
Combo1.AddItem "女"
Combo2.AddItem ""
Combo2.AddItem "111"
Combo2.AddItem "222"
Combo2.AddItem "333"
Combo2.AddItem "444"
Combo3.AddItem 15
Combo3.AddItem 20
Combo3.AddItem 30
Combo5.AddItem ""
Combo5.AddItem "111"
Combo5.AddItem "222"
Combo5.AddItem "333"
Combo5.AddItem "444"
Combo4.AddItem 15
Combo4.AddItem 20
Combo4.AddItem 30
End Sub
我循环的时候 需要更改Combo控件
第一次循环用Combo11 Combo21 text11 text21
第二次循环用Combo21 Combo22 text21 text22
想使用Combo+变量+1这样控制 不知道能不能实现
界面图片在附件内
原始代码如下
Option Explicit
Private Sub Command1_Click()
Dim ConStr As String
Dim cn As New ADODB.Connection
Set cn = New ADODB.Connection
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\bf.Mdb"
cn.Open ConStr
Dim n As Integer
n = 1
If Combo2.Text = "111" And Text5.Text > 55 Then
MsgBox "被保人超龄!", vbOKOnly, "输入错误"
Exit Sub
End If
If Combo1.Text <> "" And Combo2.Text <> "" And Combo3.Text <> "" Then
Dim rs1 As New ADODB.Recordset
Set rs1 = New ADODB.Recordset
rs1.Open "Select * from " + Combo2.Text + " where age = " + Trim(Trim(Text5.Text)) + " and sex = '" + Combo1.Text + "' and year = " + Trim(Trim(Combo3.Text)) + "", cn, adOpenKeyset, adLockPessimistic
Text1.Text = rs1.Fields(5) * Text2.Text
rs1.Close
Set rs1 = Nothing
Else: Text1.Text = 0
End If
If Combo1.Text <> "" And Combo4.Text <> "" And Combo5.Text <> "" Then
Dim rs2 As New ADODB.Recordset
Set rs2 = New ADODB.Recordset
rs2.Open "Select * from " + Combo5.Text + " where age = " + Trim(Trim(Text5.Text)) + " and sex = '" + Combo1.Text + "' and year = " + Trim(Trim(Combo4.Text)) + "", cn, adOpenKeyset, adLockPessimistic
Text4.Text = rs2.Fields(5) * Text3.Text
rs2.Close
Set rs2 = Nothing
Else: Text4.Text = 0
End If
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Command3_Click()
Combo1.Text = ""
Combo2.Text = ""
Combo3.Text = ""
Combo4.Text = ""
Combo5.Text = ""
Text1.Text = 0
Text2.Text = 0
Text3.Text = 0
Text4.Text = 0
Text5.Text = 0
End Sub
Private Sub Form_Load()
Combo1.AddItem "男"
Combo1.AddItem "女"
Combo2.AddItem ""
Combo2.AddItem "111"
Combo2.AddItem "222"
Combo2.AddItem "333"
Combo2.AddItem "444"
Combo3.AddItem 15
Combo3.AddItem 20
Combo3.AddItem 30
Combo5.AddItem ""
Combo5.AddItem "111"
Combo5.AddItem "222"
Combo5.AddItem "333"
Combo5.AddItem "444"
Combo4.AddItem 15
Combo4.AddItem 20
Combo4.AddItem 30
End Sub
我循环的时候 需要更改Combo控件
第一次循环用Combo11 Combo21 text11 text21
第二次循环用Combo21 Combo22 text21 text22
想使用Combo+变量+1这样控制 不知道能不能实现