如何在Access中通过Excel在一个子系统中查询和更新表

如何在Access中通过Excel在一个子系统中查询和更新表,excel,ms-access,vba,Excel,Ms Access,Vba,我在Excel中有一个带有四个文本框的用户表单。每个文本框对应Access中表1的一个字段 我想从Access中的表返回最大行ID值,将该值加1,并在userform的文本框中显示该值 在其他3个文本框中输入值后,我想将数据导出到Access中的Table1中 这一切能在同一个子例程中完成吗?请帮助我将sql语句合并到代码中的正确位置 多谢各位 Dim cnn As ADODB.Connection 'dim the ADO collection class Dim rst As ADODB.R

我在Excel中有一个带有四个文本框的用户表单。每个文本框对应Access中表1的一个字段

我想从Access中的表返回最大行ID值,将该值加1,并在userform的文本框中显示该值

在其他3个文本框中输入值后,我想将数据导出到Access中的Table1中

这一切能在同一个子例程中完成吗?请帮助我将sql语句合并到代码中的正确位置

多谢各位

Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rst As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath As String
Dim x As Long, i As Long
Dim PrimaryField As String

Dim MyTable As String
Dim GetLastPrimaryKey As Variant

PrimaryField = "ID"
MyTable = "Table1"


'Erro handler
On Error GoTo errHandler:

'dbPath = ActiveSheet.Range("H500").Value

dbPath = "H:\Annie\File.accdb"

Set cnn = New ADODB.Connection ' Initialise the collection class variable

'Connection class is equipped with a —method— named Open
'—-4 aguments—- ConnectionString, UserID, Password, Options
'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
sql = "SELECT MAX([" & PrimaryField & "]) FROM [" & MyTable & "];"

'two primary providers used in ADO SQLOLEDB —-Microsoft.JET.OLEDB.4.0 —-Microsoft.ACE.OLEDB.12.0
'Object Linking and Embedding, Database

'ADO library is equipped with a class named Recordset
Set rst = New ADODB.Recordset 'assign memory to the recordset
'ConnectionString Open '—-5 aguments—-

'Source, ActiveConnection, CursorType, LockType, Options

rst.Open Source:="Table1", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
Options:=adCmdTable

'rst.Open sql, cnn


GetLastPrimaryKey = rst.Fields(0).Value
MsgBox (GetLastPrimaryKey)
GetLastPrimaryKey = Arec1.Value
'you now have the recordset object
'alternative code
With rst
    .AddNew
    .Fields("ID").Value = Arec1
    .Fields("patient").Value = Arec2
    .Fields("test").Value = Arec3
    .Fields("CommentTxt").Value = Arec4
    .Update
End With


'clear the userform values
For x = 1 To 4
UserForm1.Controls("Arec" & x).Value = ""
Next
'add the next user ID
'Me.Arec1 = Sheet1.Range("J3").Value
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
'commuinicate with the user
MsgBox " The data has been successfully sent to the access database"
On Error GoTo 0
Exit Sub
errHandler:
Set rst = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdAdd"


End Sub

考虑打开一个新记录集。当然,请更改Excel用户表单文本框名称(此处的占位符):


请原谅,为什么不在MS Access中执行所有操作?为什么要在Excel中重新设计一个完整的用户界面和查询处理器,就像Access以本机方式执行这些操作一样。原始报表将转储到Excel中。此外,我与拒绝使用访问权限的人一起工作。他们害怕它啊,是的,Excel的流行性和易用性是它自己的缺点,因为人们会在阳光下用它做任何事情!如果你在Access中设计了一个好的UI,用户就不应该害怕,因为他们每天都在网站上使用命令按钮、下拉列表和文本框!另外,Access表单比Excel用户表单更具交互性。谢谢您的建议。我添加了您的代码,现在出现了“错误3001(参数类型错误、超出可接受范围或相互冲突”错误。您能帮助解决此错误吗?以下是我所做的更改:
code Set cnn=New ADODB.Connection'初始化集合类变量cnn.Open“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“&dbPath Set maxidst=New ADODB.Recordset sql=”从[”&MyTable&“]中选择MAX([”&PrimaryField&“])+1作为maxidlusone;“maxidst.Open sql,conn UserForm1.Arec1.Value=maxidst!maxidlusone Set maxidst=Nothing Set rst=New ADODB.Recordset”将内存分配给记录集rst。开源:=“Table1”,ActiveConnection:=cnn,u-CursorType:=adOpenDynamic,LockType:=ADLOCKOPTIMIZE,u-Options:=adCmdTable
您的更改有效吗?我相信前面的错误是由于UserForm控件引用引起的。正如我在回答中提到的,您需要将其更改为适合预期的文本框。我在Arec中添加了第五个,因为我认为其他四个在u中se已经(ID、patient、test、CommentTxt)。我已经创建了两个独立的例程:一个用于查询MaxId,另一个用于将数据发送到Access,它们工作得很好!感谢您的帮助。
Set maxIDrst = New ADODB.Recordset

sql = "SELECT MAX([" & PrimaryField & "]) + 1 as MaxIDPlusOne FROM [" & MyTable & "];"
maxIDrst.Open sql, conn

UserForm1.Controls("Arec5").Value = maxIDrst!MaxIDPlusOne
maxIDrst.Close
...
Set maxIDrst = nothing