如何向ACCESS 数据库中增加一个字段

2025-01-01 05:42:33
推荐回答(3个)
回答1:

Public Function TransactSQL(ByVal sql As String) As ADODB.Recordset
Dim cont As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConnection As String
Dim strArray() As String
Set cont = New ADODB.Connection '创建连接
Set rs = New ADODB.Recordset
On Error GoTo transactsql_error
strConnection = "Provider = Microsoft.jet.oledb.4.0; data source=" & App.Path & "\cd1.mdb;"
strConnection = strConnection & "Persist Security Info=False;"
strArray = Split(sql) 'Split() 返回一个下标从零开始的一维数组
cont.Open strConnection '打开连接
If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
rs.Open Trim$(sql), cont, adOpenKeyset, adLockOptimistic
Set TransactSQL = rs
iflag = 1
Else
cont.Execute sql
iflag = 1
End If
transactsql_exit:
Set rs = Nothing
Set cont = Nothing
Exit Function
transactsql_error:
MsgBox "查询错误:" & Err.Description
iflag = 2
Resume transactsql_exit
End Function
上面这个是一个数据库的动态链接,这块代码在模块里放着.
Dim rs As New ADODB.Recordset
Dim sql As String
sql = "select * from Lendcx" 'Lendcx是表名
Set rs = TransactSQL(sql)
rs.AddNew '添加新纪录
rs.Fields(0) = Trim(Combo3.Text)
rs.Fields(1) = Trim(Combo1.Text)
rs.Fields(2) = Trim(DTPicker1.Value)
rs.Fields(3) = Trim(Combo2.Text)
rs.Fields(4) = Trim(Text2.Text)
rs.Fields(5) = Trim(text3.Text)
rs.Update
rs.Close
你可以参考一下看看!

回答2:

Public Function TransactSQL(ByVal sql AsString) As ADODB.Recordset
Dim cont As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConnection As String
Dim strArray() As String
Set cont = New ADODB.Connection '创建连接
Set rs = New ADODB.Recordset
On Error GoTo transactsql_error
strConnection = "Provider =Microsoft.jet.oledb.4.0; data source=" & App.Path &"\cd1.mdb;"
strConnection = strConnection &"Persist Security Info=False;"
strArray = Split(sql) 'Split() 返回一个下标从零开始的一维数组
cont.Open strConnection '打开连接
If StrComp(UCase$(strArray(0)),"select", vbTextCompare) = 0 Then
rs.Open Trim$(sql), cont, adOpenKeyset,adLockOptimistic
Set TransactSQL = rs
iflag = 1
Else
cont.Execute sql
iflag = 1
End If
transactsql_exit:
Set rs = Nothing
Set cont = Nothing
Exit Function
transactsql_error:
MsgBox "查询错误:" &Err.Description
iflag = 2
Resume transactsql_exit
End Function
上面这个是一个数据库的动态链接,这块代码在模块里放着.
Dim rs As New ADODB.Recordset
Dim sql As String
sql = "select * from Lendcx"'Lendcx是表名
Set rs = TransactSQL(sql)
rs.AddNew '添加新纪录
rs.Fields(0) = Trim(Combo3.Text)
rs.Fields(1) = Trim(Combo1.Text)
rs.Fields(2) = Trim(DTPicker1.Value)
rs.Fields(3) = Trim(Combo2.Text)
rs.Fields(4) = Trim(Text2.Text)
rs.Fields(5) = Trim(text3.Text)
rs.Update
rs.Close

回答3:

从我看过的教科书里都没写能加字段的,都是进ACCESS去设置的