vb中操作ACCESS数据库 怎么用变量代替表名 实现查询

dim table1 as string
table1="mytable"
rs.Open "select * from " & table1 & " where question='" + strQuestion + "'", cnn, adOpenDynamic, adLockOptimistic
然后通过rs.addnew 向表中添加数据
一直不成功。请各位大侠指教,回答的好再加分。

Option Explicit
Dim conn As New Connection

Private Sub Command1_Click()
Dim strSQL As String

On Error GoTo ErrHandle
If conn.State = 0 Then

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\mytestdb1.mdb;Persist Security Info=False"

conn.Open
End If
strSQL = "CREATE TABLE " & Trim$(Text1) & "(id integer CONSTRAINT index1 UNIQUE,name TEXT,sex TEXT,question TEXT)"
conn.Execute strSQL
MsgBox "CREATE TABLE OK!", , "OK"
Exit Sub
ErrHandle:
Err.Raise Err.Number, "CREATE TABLE", Err.Description
End Sub

Private Sub Command2_Click()
Dim rs As New Recordset
Dim table1 As String
Dim strQuestion As String

On Error GoTo ErrHandle
If conn.State = 0 Then

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\mytestdb1.mdb;Persist Security Info=False"

conn.Open
End If
table1 = Text1
strQuestion = "What is his ege?"
rs.Open "select * from " & table1 & " where question='" + strQuestion + "'", conn, adOpenDynamic, adLockOptimistic
rs.AddNew
rs("id") = 1
rs("name") = "jack"
rs("sex") = "boy"
rs("question") = strQuestion
rs.Update
MsgBox "UPDATA TABLE OK!", , "OK"
Exit Sub
ErrHandle:
Err.Raise Err.Number, "UPDATE TABLE", Err.Description
End Sub

Private Sub Form_Load()
Text1 = "mytable"
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-04
什么?
相似回答