![]() |
#2
不说也罢2011-07-11 19:33
|

Private Sub dataQuery_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
REM 创建连接对象并打开连接
Con = New ADODB.Connection()
Con.ConnectionString = "Dbq=" & System.Windows.Forms.Application.StartupPath & "\钻孔情况表.mdb;Driver={Microsoft Access Driver (*.mdb)}"
Con.Open()
REM 创建记录集对象并获取表的数据
Rec = New ADODB.Recordset()
Rec.CursorType = ADODB.CursorTypeEnum.adOpenKeyset
Rec.LockType = ADODB.LockTypeEnum.adLockOptimistic
Rec.ActiveConnection = Con
If 数据库.BindingNavigator.BindingSource Is 数据库.钻孔数据表BindingSource Then
Rec.Open("钻孔数据表")
ElseIf 数据库.BindingNavigator.BindingSource Is 数据库.钻孔情况表BindingSource Then
Rec.Open("钻孔情况表")
ElseIf 数据库.BindingNavigator.BindingSource Is 数据库.标贯表BindingSource Then
Rec.Open("标贯表")
End If
REM 向第一个组合框中加入表中各字段的名称
CmbFields.Items.Clear()
CmbFields.Items.Add("字段名")
Dim iCount As Integer
For iCount = 0 To Rec.Fields.Count - 1 '错误:没有为命令对象设置命令
CmbFields.Items.Add(Rec.Fields.Item(iCount).Name)
Next
CmbFields.SelectedIndex = 0
End Sub
这是我的一段代码!问题好像就出在 数据库.BindingNavigator.BindingSource上 数据库是另外一个窗体,如果先启动的是数据库窗体,然后通过数据库窗体打开dataQuery就没有任何问题,但是如果把这两个窗体嵌入到主程序中,通过主程序打开数据库窗体,然后再打开dataQuery就会出现错误:没有为命令对象设置命令,我接触编程时间很多,对于很多东西都不是特别了解,不知我描述的是否清楚,望高手指点迷津,谢谢! REM 创建连接对象并打开连接
Con = New ADODB.Connection()
Con.ConnectionString = "Dbq=" & System.Windows.Forms.Application.StartupPath & "\钻孔情况表.mdb;Driver={Microsoft Access Driver (*.mdb)}"
Con.Open()
REM 创建记录集对象并获取表的数据
Rec = New ADODB.Recordset()
Rec.CursorType = ADODB.CursorTypeEnum.adOpenKeyset
Rec.LockType = ADODB.LockTypeEnum.adLockOptimistic
Rec.ActiveConnection = Con
If 数据库.BindingNavigator.BindingSource Is 数据库.钻孔数据表BindingSource Then
Rec.Open("钻孔数据表")
ElseIf 数据库.BindingNavigator.BindingSource Is 数据库.钻孔情况表BindingSource Then
Rec.Open("钻孔情况表")
ElseIf 数据库.BindingNavigator.BindingSource Is 数据库.标贯表BindingSource Then
Rec.Open("标贯表")
End If
REM 向第一个组合框中加入表中各字段的名称
CmbFields.Items.Clear()
CmbFields.Items.Add("字段名")
Dim iCount As Integer
For iCount = 0 To Rec.Fields.Count - 1 '错误:没有为命令对象设置命令
CmbFields.Items.Add(Rec.Fields.Item(iCount).Name)
Next
CmbFields.SelectedIndex = 0
End Sub