本人采用VB6.0+SQL2000编制数据库时,试运行中发现,当保存及删除时会提示如下:
1.[Micrsoft][ODBC 驱动程序管理器]未发现数据源名称并且未指定默认驱动程序.点击"确定"键后再提示如2.
2.对象'Refresh'的方法'IAdodc'失败.
关闭后再次查看可见到所写内容已填入SQL表中.当删除时也会提示类同的问题.
现附有问题的程式到www.access911.net/csdn 咨询VB问题.rar 226.16 KB 2009-2-12 16:17:57 

解决方案 »

  1.   

    '建议用ADO对象连接数据库:
    dim conn as new adodb.connection
    With conn
        If .State = adStateOpen Then .Close
        .ConnectionString = "driver=SQL Server;server=计算机或IP地址;uid=sa;pwd=sa的密码;database=数据库名"
        .CommandTimeout = 0
        .Open
    End With'增加记录:
    conn.execute " insert into 表名(字符型字段,数值型字段) values('"& 字符型变量 &"',"& 数值型变量 &")"
    '修改记录:
    conn.execute " update 表名 set ... where ... "
    '删除记录:
    conn.execute " delete from 表名 where ... "
      

  2.   

    建议不要使用ADODC控件来访问数据库,那种控件用太多系统会崩溃的!
      

  3.   

    ,数据库连接有问题,form_load里这样连一下吧    Dim Rs As ADODB.Recordset    Set Rs = New ADODB.Recordset
        With Rs
            Set .ActiveConnection = Cn
            .CursorLocation = adUseClient
            .CursorType = adOpenStatic
            .LockType = adLockReadOnly        .Open "select * from tb_APGL_Det order by Det_ID"
            If .RecordCount > 0 Then
                Set Adodc1.Recordset = Rs.Clone
                Adodc1.Recordset.Requery
                Set Dgr_Det.DataSource = rs
                rs.Requery            
            End If
            .Close
        End With
      

  4.   

    你的 DataGrid、rs1.Open、Execute 用到的 Connection 都是完全独立的对象,当然容易起冲突了。
    既然用了绑定就老老实实用 DataGrid 进行数据的增删改。
      

  5.   

    我还是不太明白,本人菜鸟一个,可否帮我修改及测试过的文件电邮给我, ,(因我不能从www.access911.net/csdn 这里下载。谢谢!
      

  6.   

    现将窗体代码贴出来如下:
    Dim rs1 As New adodb.Recordset
    Public blnAdd As Boolean
    Dim mybook
    Dim Mystr As String  '定义字符串变量Sub view_data()  '显示数据信息
        Dim i As Integer
        If Adodc1.Recordset.RecordCount > 0 Then
            For i = 0 To Text1.UBound
                Text1(i).Text = Dgr_Det.Columns(i)
            Next i
        End If
    End SubPrivate Sub Dgr_Det_Click()
     view_data
    End SubPrivate Sub Form_Load()
        Adodc1.ConnectionString = PublicStr
        Call Dgr_Title  '调用过程
       tlbState Toolbar1, False
        Call view_data  '调用过程
       '设置控件状态
      For i = 0 To Text1.UBound
           Text1(i).Enabled = False
       Next i
    End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        sql = ""
    End SubSub Dgr_Title()   '设置DataGrid控件的标题信息
        Dgr_Det.Columns(0).Caption = "部门编号"
        Dgr_Det.Columns(1).Caption = "部门名称"
        Dgr_Det.Columns(2).Caption = "主管"
        Dgr_Det.Columns(3).Caption = "副主管"
        Dgr_Det.Columns(4).Caption = "备注"
    End SubPrivate Sub Text1_GotFocus(Index As Integer)
        'Text1控件数组获得焦点
        Text1(Index).BackColor = &HFFFF80
        Text1(Index).SelStart = 0
        Text1(Index).SelLength = Len(Text1(Index))
    End SubPrivate Sub Text1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
        'Text1控件数据按回车键
        If KeyCode = vbKeyReturn And Index < 4 Then
            If Index = 4 Then Exit Sub
            Text1(Index + 1).SetFocus
        End If
    End SubPrivate Sub Text1_LostFocus(Index As Integer)
        'Text1控件数据失去焦点
        Text1(Index).BackColor = &HFFFFFF
    End SubPrivate Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
        Select Case Button.Key
        Case "add"   '添加
            blnAdd = True
            tlbState Toolbar1, True
            rs1.Open "select * from tb_APGL_Det order by Det_ID", cnn, adOpenStatic
            If rs1.RecordCount > 0 Then
                rs1.MoveLast
                Text1(0) = "D" + Format(Val(Right(Trim(rs1.Fields("Det_ID")), 3)) + 1, "###000")   '编号自动加1
            Else
                Text1(0) = "D001"
            End If
            rs1.Close
            For i = 1 To Text1.UBound
                Text1(i) = ""
                Text1(i).Locked = False
            Next i
            For i = 1 To Text1.UBound
                    Text1(i).Enabled = True
            Next i
            Text1(1).SetFocus
        Case "modify"   '修改
            If Adodc1.Recordset.RecordCount > 0 Then
                blnAdd = False
                tlbState Toolbar1, True
                For i = 1 To Text1.UBound
                    Text1(i).Locked = False
                Next i
                view_data
                mybook = Dgr_Det.Book
            For i = 1 To Text1.UBound
                    Text1(i).Locked = True
            Next i
            Else
                MsgBox "系统没有要修改的数据!", vbInformation
            End If
        Case "delete"    '删除
            If Adodc1.Recordset.RecordCount > 0 Then
                Dim rs2 As New adodb.Recordset
                rs2.Open "select * from tb_APGL_Staff where Staff_DetID='" + Adodc1.Recordset.Fields("Det_ID") + "'", cnn, adOpenKeyset
                If rs2.RecordCount > 0 Then
                    MsgBox "该信息正在使用,不能删除!", vbInformation
                    Exit Sub
                Else
                    Adodc1.Recordset.Delete
                    Adodc1.Refresh
                    For i = 0 To Text1.UBound
                        Text1(i).Text = ""
                    Next i
                    Call view_data
                    Call Dgr_Title
                End If
            Else
                MsgBox "系统没有要删除的数据!", vbInformation
            End If
        Case "save"   '保存
            On Error GoTo SaveErr
            If Text1(1).Text = "" Then
                MsgBox "系统不允许" & Label2 & "为空!", vbInformation
                Exit Sub
            End If
            If blnAdd = True Then    '添加新记录
                cnn.Execute ("insert into tb_APGL_Det(Det_ID,Det_Name,Det_Supervisor,Det_ViceSupervisor,Det_Res) values('" + Text1(0) + "','" + Text1(1) + "','" + Text1(2) + "','" + Text1(3) + "','" + Text1(4) + "')")
                '          Adodc1.Refresh
                '         Unload Me
                '          Frm_Jcxx_Sbzt.Show 1
                Adodc1.Refresh
                Set Dgr_Det.DataSource = Adodc1
                Call Dgr_Title
                tlbState Toolbar1, False
                For i = 1 To Text1.UBound
                    Text1(i).Locked = True
                Next i
            Else    '修改原有记录
                cnn.Execute ("update tb_APGL_Det set Det_Name ='" + Text1(1) + "',Det_Supervisor='" + Text1(2) + "',Det_ViceSupervisor='" + Text1(3) + "',Det_Res='" + Text1(4) + "' where Det_ID='" + Text1(0).Text + "'")
                '          Adodc1.Refresh
                '          Unload Me
                '          Frm_Jcxx_Sbzt.Show 1
                Adodc1.Refresh
                Set Dgr_Det.DataSource = Adodc1
                Call Dgr_Title
                Dgr_Det.Book = mybook
                tlbState Toolbar1, False
                For i = 1 To Text1.UBound
                    Text1(i).Locked = True
                Next i
            End If
            Exit Sub
    SaveErr:           '出错处理
            MsgBox Err.Description, vbInformation
        Case "cancel"  ' 取消
            tlbState Toolbar1, False
            For i = 1 To Text1.UBound
                Text1(i).Locked = True
            Next i
            Call view_data
        Case "find"    ' 查询
            On Error Resume Next
            Mystr = InputBox("请输入要查询的部门编号", "部门查询", "D001")
            rs1.Open "select * from tb_APGL_Det where Det_ID ='" + Mystr + "'", cnn, adOpenKeyset
            If rs1.RecordCount > 0 Then
                For i = 0 To rs1.Fields.Count - 1
                    If rs1.Fields(i) <> "" Then
                        Text1(i).Text = rs1.Fields(i)
                    Else
                        Text1(i).Text = ""
                    End If
                Next i
            End If
            rs1.Close
        Case "close"   '关闭
            Unload Me
        End Select
    End Sub公共模块代码:
    Public PublicStr As String
    Public sql As String
    Public Tb As String    '存储表名
    Public Province As String  '存储省份名称
    Public Sign As Boolean   '标识Public Function cnn() As adodb.Connection     '定义函数
        Set cnn = New adodb.Connection
        cnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=db_APGL"
        PublicStr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=db_APGL"
    End Function
    '定义设置Toolbar控件上按钮状态的函数
    Public Function tlbState(tlb As Toolbar, state As Boolean)
        With tlb
            If state = True Then
                .Buttons(1).Enabled = False
                .Buttons(2).Enabled = False
                .Buttons(3).Enabled = False
                .Buttons(6).Enabled = False
                .Buttons(4).Enabled = True
                .Buttons(5).Enabled = True
            Else
                .Buttons(1).Enabled = True
                .Buttons(2).Enabled = True
                .Buttons(3).Enabled = True
                .Buttons(6).Enabled = True
                .Buttons(4).Enabled = False
                .Buttons(5).Enabled = False
            End If
        End With
    End FunctionPublic Function ESQL(ByVal sql As String) As adodb.Recordset
        Dim cnn As adodb.Connection
        Dim rs As adodb.Recordset
        Set cnn = New adodb.Connection
        cnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=db_APGL"
        Set rs = New adodb.Recordset
        rs.Open Trim(sql), cnn, adOpenKeyset, adLockOptimistic
        Set ESQL = rs
    End Function
    'Public Function MyPath(expression As String, delimiter As String) As String    '取出文件夹后剩下的路径
    '    Dim vstr As Variant
    '    Dim str(10) As String
    '    Dim i As Integer
    '    For i = 0 To UBound(Split(expression, delimiter))
    '        vstr = Split(expression, delimiter)
    '        MyPath = vstr(0)
    '    Next i
    'End Function
    请高手们帮我改一下.