Option Explicit
Dim Mycn As New ADODB.Connection '数据库连接对象,用于打开数据库
Dim Myrs1 As New ADODB.Recordset '记录集对象,用于存入记录的
Dim Myrs2 As New ADODB.Recordset '记录集对象,用于存入记录的
Private Sub Command1_Click()
Dim cnstring As String '用于存放连接字符串的
cnstring = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=dytd;PassWord=dytd;Data Source=dytd"
Mycn.Open cnstring
Mycn.CursorLocation = adUseClient
If Option1.Value = True Then
If Text1.Text = "" Then
MsgBox "请正确填写申请书编号!", vbOKOnly + vbExclamation, "信息提示"
Exit Sub
Else
Myrs1.Open "select * from DJ_CSDJ_TDDJSQS where DCT_BH='" & Trim(Text1) & "'", Mycn
If Myrs1.BOF And Myrs1.EOF Then
MsgBox "不存在申请书编号!!!", vbOKOnly + vbExclamation, "信息提示"
Text1 = ""
Text1.SetFocus
Myrs1.Close
Set Myrs1 = Nothing
Mycn.Close
Exit Sub
End If
MSHFlexGrid1.Clear
TableInit
MSHFlexGrid1.Rows = MSHFlexGrid1.Rows + 1
MSHFlexGrid1.Row = MSHFlexGrid1.Rows - 1
MSHFlexGrid1.Col = 0
MSHFlexGrid1.Text = Myrs1.Fields("DCT_BH") & ""
MSHFlexGrid1.Col = 1
.....................
Myrs1.Close
Set Myrs1 = Nothing
Else
Mycn.Close '关闭连接对象
Set Mycn = Nothing
Myrs1.Close
Set Myrs1 = Nothing
Exit Sub
Mycn.Close
End If
Mycn.Close
End Sub
改成这样测试通过!
但是新问题有来了在后面加
If Option2.Value = True Then
If Text2.Text = "" Then
MsgBox "请正确填写土地使用者!", vbOKOnly + vbExclamation, "信息提示"
Exit Sub
Else
Myrs1.Open "select * from DJ_CSDJ_TDDJSQS where DCT_TDSYZ='" & Trim(Text2) & "'", Mycn
If Myrs1.BOF And Myrs1.EOF Then
MsgBox "不存在土地使用者!!!", vbOKOnly + vbExclamation, "信息提示"
Text2 = ""
Text2.SetFocus
Myrs1.Close
Set Myrs1 = Nothing
Mycn.Close
Exit Sub
End If
MSHFlexGrid1.Clear
TableInit
MSHFlexGrid1.Rows = MSHFlexGrid1.Rows + 1
MSHFlexGrid1.Row = MSHFlexGrid1.Rows - 1
MSHFlexGrid1.Col = 0
MSHFlexGrid1.Text = Myrs1.Fields("DCT_BH") & ""
MSHFlexGrid1.Col = 1
.....................
Myrs1.Close
Set Myrs1 = Nothing
Else
Mycn.Close '关闭连接对象
Set Mycn = Nothing
Myrs1.Close
Set Myrs1 = Nothing
Exit Sub
Mycn.Close
End If
Mycn.Close
End Sub
有问题呢????!!!
Dim Mycn As New ADODB.Connection '数据库连接对象,用于打开数据库
Dim Myrs1 As New ADODB.Recordset '记录集对象,用于存入记录的
Dim Myrs2 As New ADODB.Recordset '记录集对象,用于存入记录的
Private Sub Command1_Click()
Dim cnstring As String '用于存放连接字符串的
cnstring = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=dytd;PassWord=dytd;Data Source=dytd"
Mycn.Open cnstring
Mycn.CursorLocation = adUseClient
If Option1.Value = True Then
If Text1.Text = "" Then
MsgBox "请正确填写申请书编号!", vbOKOnly + vbExclamation, "信息提示"
Exit Sub
Else
Myrs1.Open "select * from DJ_CSDJ_TDDJSQS where DCT_BH='" & Trim(Text1) & "'", Mycn
If Myrs1.BOF And Myrs1.EOF Then
MsgBox "不存在申请书编号!!!", vbOKOnly + vbExclamation, "信息提示"
Text1 = ""
Text1.SetFocus
Myrs1.Close
Set Myrs1 = Nothing
Mycn.Close
Exit Sub
End If
MSHFlexGrid1.Clear
TableInit
MSHFlexGrid1.Rows = MSHFlexGrid1.Rows + 1
MSHFlexGrid1.Row = MSHFlexGrid1.Rows - 1
MSHFlexGrid1.Col = 0
MSHFlexGrid1.Text = Myrs1.Fields("DCT_BH") & ""
MSHFlexGrid1.Col = 1
.....................
Myrs1.Close
Set Myrs1 = Nothing
Else
Mycn.Close '关闭连接对象
Set Mycn = Nothing
Myrs1.Close
Set Myrs1 = Nothing
Exit Sub
Mycn.Close
End If
Mycn.Close
End Sub
改成这样测试通过!
但是新问题有来了在后面加
If Option2.Value = True Then
If Text2.Text = "" Then
MsgBox "请正确填写土地使用者!", vbOKOnly + vbExclamation, "信息提示"
Exit Sub
Else
Myrs1.Open "select * from DJ_CSDJ_TDDJSQS where DCT_TDSYZ='" & Trim(Text2) & "'", Mycn
If Myrs1.BOF And Myrs1.EOF Then
MsgBox "不存在土地使用者!!!", vbOKOnly + vbExclamation, "信息提示"
Text2 = ""
Text2.SetFocus
Myrs1.Close
Set Myrs1 = Nothing
Mycn.Close
Exit Sub
End If
MSHFlexGrid1.Clear
TableInit
MSHFlexGrid1.Rows = MSHFlexGrid1.Rows + 1
MSHFlexGrid1.Row = MSHFlexGrid1.Rows - 1
MSHFlexGrid1.Col = 0
MSHFlexGrid1.Text = Myrs1.Fields("DCT_BH") & ""
MSHFlexGrid1.Col = 1
.....................
Myrs1.Close
Set Myrs1 = Nothing
Else
Mycn.Close '关闭连接对象
Set Mycn = Nothing
Myrs1.Close
Set Myrs1 = Nothing
Exit Sub
Mycn.Close
End If
Mycn.Close
End Sub
有问题呢????!!!
你的出错我知道哪儿错了。
Option Explicit
Dim Mycn As New ADODB.Connection '数据库连接对象,用于打开数据库
Dim Myrs1 As New ADODB.Recordset '记录集对象,用于存入记录的
Dim Myrs2 As New ADODB.Recordset '记录集对象,用于存入记录的
Private Sub Command1_Click()
Dim cnstring As String '用于存放连接字符串的
cnstring = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=dytd;PassWord=dytd;Data Source=dytd"
Mycn.Open cnstring
Mycn.CursorLocation = adUseClient
If Option1.Value = True Then
If Text1.Text = "" Then
MsgBox "请正确填写申请书编号!", vbOKOnly + vbExclamation, "信息提示"
Exit Sub
Else
Myrs1.Open "select * from DJ_CSDJ_TDDJSQS where DCT_BH='" & Trim(Text1) & "'", Mycn
If Myrs1.BOF And Myrs1.EOF Then
MsgBox "不存在申请书编号!!!", vbOKOnly + vbExclamation, "信息提示"
Text1 = ""
Text1.SetFocus
Myrs1.Close
Set Myrs1 = Nothing
Mycn.Close
Set Mycn=nothing
Exit Sub
End If
MSHFlexGrid1.Clear
TableInit
MSHFlexGrid1.Rows = MSHFlexGrid1.Rows + 1
MSHFlexGrid1.Row = MSHFlexGrid1.Rows - 1
MSHFlexGrid1.Col = 0
MSHFlexGrid1.Text = Myrs1.Fields("DCT_BH") & ""
MSHFlexGrid1.Col = 1
.....................
Myrs1.Close
Set Myrs1 = Nothing
Else
Myrs1.Close
Set Myrs1 = Nothing
Exit Sub
End If
End If
If Option2.Value = True Then
If Text2.Text = "" Then
MsgBox "请正确填写土地使用者!", vbOKOnly + vbExclamation, "信息提示"
Exit Sub
Else
Myrs1.Open "select * from DJ_CSDJ_TDDJSQS where DCT_TDSYZ='" & Trim(Text2) & "'", Mycn
If Myrs1.BOF And Myrs1.EOF Then
MsgBox "不存在土地使用者!!!", vbOKOnly + vbExclamation, "信息提示"
Text2 = ""
Text2.SetFocus
Myrs1.Close
Set Myrs1 = Nothing
Mycn.Close
Exit Sub
End If
MSHFlexGrid1.Clear
TableInit
MSHFlexGrid1.Rows = MSHFlexGrid1.Rows + 1
MSHFlexGrid1.Row = MSHFlexGrid1.Rows - 1
MSHFlexGrid1.Col = 0
MSHFlexGrid1.Text = Myrs1.Fields("DCT_BH") & ""
MSHFlexGrid1.Col = 1
.....................
Myrs1.Close
Set Myrs1 = Nothing
Else
Mycn.Close '关闭连接对象
Set Mycn = Nothing
Myrs1.Close
Set Myrs1 = Nothing
Exit Sub
End If
Mycn.Close
End Sub主要是修改了option1中的连接对象部分,就是mycn.close等语句。你再好好看看。
如果你后面还有option3的话,那么也按option1部分的修改。
也就是连接对象的关闭要在最后关闭,也就是说在整个command1命令执行要结束的时候才关闭它。
将
Dim Myrs1 As New ADODB.Recordset '记录集对象,用于存入记录的
Dim Myrs2 As New ADODB.Recordset '记录集对象,用于存入记录的
两句中的New关键字去掉,然后在每次Open之前加上下面这一句
Set myrs1=new adodb.recordset 即可
Dim Mycn As New ADODB.Connection '数据库连接对象,用于打开数据库Dim cnstring As String '用于存放连接字符串的
cnstring = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=dytd;PassWord=dytd;Data Source=dytd"
Mycn.Open cnstring
Mycn.CursorLocation = adUseClient
这几句可以独立出来做成一个函数,把Connection对象申明为全局变量,建立数据库的连接的函数只要在程序开始时执行一次就可以了,这样可以简化代码,提高效率!