vb中做了画面,加了几个text控件,想把内容执行一个添加按钮后添加到ACCESS数据库里
能不能举个例子,我要看一下 谢谢!有5个text  一个添加按钮   数据库user.mdb中的一个mathe表

解决方案 »

  1.   

    Dim Con As ADODB.Connection
        Dim Sql As String
        Set Con = New ADODB.Connection
        Con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\WW_SOFT\ȨÏÞ.mdb;Persist Security Info=False"
        
        Sql = "Insert Into mathe Values(" _
            & "'" & Trim(Text1.Text) & "'," _
            & "'" & Trim(Text2.Text) & "'," _
            & "'" & Trim(Text3.Text) & "'," _
            & "'" & Trim(Text4.Text) & "'," _
            & "'" & Trim(Text5.Text) & "'" _
            & ")"
        Con.Execute Sql
        Con.Close
      

  2.   

    Private Sub Command2_Click()
        Dim Con As ADODB.Connection
        Dim Sql As String
        Set Con = New ADODB.Connection
        Con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c\user.mdb;Persist Security Info=False"
        
        Sql = "Insert Into mathe Values(" _
            & "'" & Trim(Text1.Text) & "'," _
            & "'" & Trim(Text2.Text) & "'," _
            & "'" & Trim(Text3.Text) & "'," _
            & "'" & Trim(Text4.Text) & "'," _
            & "'" & Trim(Text5.Text) & "'" _
            & ")"
        Con.Execute Sql
        Con.Close
    End Sub
      

  3.   

    '引用ADO(Microsoft ActiveX Data Objects 2.X Library)
    Private Sub Command1_Click()
        On Error GoTo err
        Dim cn As New ADODB.Connection
        '有密码的连接方式
        'cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\user.mdb;Jet OLEDB:DataBase password=12345;"
        '无密码的连接方式
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\user.mdb;Persist Security Info=False"
        cn.Open
        cn.Execute("insert into mathe values('" & Text1.Text & "'" & Text2.Text & "'" & Text3.Text & "'" & Text4.Text & "'" & Text5.Text & "')" 
        cn.Close
        Set cn=Nothing
        Exit Sub
    err:
        MsgBox err.Description
    End Sub
      

  4.   

    上面的有点错误,插入语句改为:
    cn.Execute("insert into mathe values('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "','" & Text4.Text & "','" & Text5.Text & "')"
      

  5.   

    faysky2() 我试了你的办法(你说的错误也改了),但按钮后出现“insert into语法错误”
    帮忙修正一下!