1

我对 Microsoft Access 很陌生。我需要编写 VB 代码来在按钮事件单击时插入数据库。插入应该从按钮所在的表单中获取信息。我知道您可以通过以下方式获取表单信息:

Form![tablename]![columnname]

但是我将如何将该信息插入到另一个表中?我看到了这篇文章如何将数据插入 Microsoft Access 数据库?但我认为它仅适用于 .NET,因为我找不到 Access 参考页面http://msdn.microsoft.com/en-us/library/office/aa296940(v=office.11​​) 上列出的 OleDbConnection 对象。 aspx

4

3 回答 3

4

您说的是 VB,但我认为您说的是 VBA(Access 使用的 VB)。

如果您尝试插入的表与您从中发出命令的表位于同一数据库中,则您不需要连接。它是内置的。执行查询的命令是CurrentDB.Execute {SQL statement}

所以,你会有这样的东西:

Dim strSQL as String
strSQL = "INSERT INTO TableName( FieldName)
strSQL = strSQL & "SELECT 'MyData'"
CurrentDB.Execute strSQL
于 2013-06-11T02:52:09.960 回答
4

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 btnAdd.Click Dim sql As String

    Dim con As New OleDb.OleDbConnection

    Dim cmd As New OleDb.OleDbCommand


    con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = E:\test.mdb"

    con.Open()

    sql = "INSERT INTO student(name, Address)  VALUES('" & Me.txtName.Text & "','" & Me.txtAddress.Text & "')"
    cmd = New OleDb.OleDbCommand(sql, con)
    cmd.ExecuteNonQuery()
    MsgBox("saved")

    txtName.Text = ""
    txtAddress.Text = ""
    con.Close()
于 2014-01-02T06:39:31.190 回答
0
Dim con As New OleDb.OleDbConnection

Dim cmd As New OleDb.OleDbCommand


con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = E:\test.mdb"

con.Open()

sql = "INSERT INTO student(name, Address)  VALUES('" & Me.txtName.Text & "','" & Me.txtAddress.Text & "')"
cmd = New OleDb.OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MsgBox("saved")

txtName.Text = ""
txtAddress.Text = ""
con.Close()
于 2016-02-21T07:14:27.647 回答