Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 在vbnet中加载的位置选择最大值(id)_Ms Access_Vba - Fatal编程技术网

Ms access 在vbnet中加载的位置选择最大值(id)

Ms access 在vbnet中加载的位置选择最大值(id),ms-access,vba,Ms Access,Vba,从表中获取最后一个id的值时遇到问题 由于加载整个列将在将来给我带来问题(DataTable有其局限性),因此我决定寻找另一种解决方案,但我没有运气让它工作 我有string命令,但不知道将返回值存储在哪里。 这是我的密码: Dim query As String = "SELECT MAX(id) AS LastId FROM Table1" Dim dtmain As New DataTable Try With sqlcmd

从表中获取最后一个
id
的值时遇到问题

由于加载整个列将在将来给我带来问题(
DataTable
有其局限性),因此我决定寻找另一种解决方案,但我没有运气让它工作

我有string命令,但不知道将返回值存储在哪里。 这是我的密码:

        Dim query As String = "SELECT MAX(id) AS LastId FROM Table1"
    Dim dtmain As New DataTable
    Try
        With sqlcmd
            .CommandText = query
            .Connection = sqlcon
        End With

        With sqladp
            .SelectCommand = sqlcmd
            .Fill(dtMain)
        End With
        MsgBox("last id = " & dtmain.Rows(0)("LastId"))
    Catch ex As Exception

    End Try
我什么也没得到,
msgbox
甚至都不会显示

注意:我想要表中最后一个
id
,而不是插入的
id

以下是我的参考和声明:

   Imports System.Data.OleDb
    Private conn As OleDbConnection
    Private adapter As OleDbDataAdapter
    Private cmdd As OleDbCommand

此外,如果您的数据源位于当前数据库中,我还在我的项目参考中添加了用于DDL和安全性的Microsoft ADO Ext 2.8

Dim dbConnect As Database
Dim rstRecordset As DAO.Recordset

Set dbConnect = Access.CurrentDb

Dim query As String = "SELECT MAX(id) AS LastId FROM Table1"

Set rstRecordset = dbConnect .OpenRecordset(query)
MsgBox("last id = " & rstRecordset.Fields(0).value & ""))

dbConnect.Close
Set dbConnect = Nothing
Set rstRecordset = Nothing
Dim cnnConn As ADODB.connection
Dim rstRecordset As ADODB.Recordset
Dim cmdCommand As ADODB.Command

' Open the connection.
Set cnnConn = New ADODB.connection
With cnnConn
    .ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0"
     .Properties("Jet OLEDB:Database Password") = "pass"
     .Open "D:\Databases\Database.mdb"
End With

' Set the command text.
Set cmdCommand = New ADODB.Command
Set cmdCommand.ActiveConnection = cnnConn
With cmdCommand
    .CommandText = "SELECT MAX(id) AS LastId FROM Table1"
    .CommandType = adCmdText
    .Execute
End With

' Open the recordset.
Set rstRecordset = New ADODB.Recordset
Set rstRecordset.ActiveConnection = cnnConn
rstRecordset.Open cmdCommand
MsgBox("last id = " & rstRecordset.Fields(0).value & ""))

' Close the connections and clean up.
cnnConn.Close
Set cmdCommand = Nothing
Set rstRecordset = Nothing
Set cnnConn = Nothing

如果数据源位于另一个Access数据库中:

Dim dbConnect As Database
Dim rstRecordset As DAO.Recordset

Set dbConnect = Access.CurrentDb

Dim query As String = "SELECT MAX(id) AS LastId FROM Table1"

Set rstRecordset = dbConnect .OpenRecordset(query)
MsgBox("last id = " & rstRecordset.Fields(0).value & ""))

dbConnect.Close
Set dbConnect = Nothing
Set rstRecordset = Nothing
Dim cnnConn As ADODB.connection
Dim rstRecordset As ADODB.Recordset
Dim cmdCommand As ADODB.Command

' Open the connection.
Set cnnConn = New ADODB.connection
With cnnConn
    .ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0"
     .Properties("Jet OLEDB:Database Password") = "pass"
     .Open "D:\Databases\Database.mdb"
End With

' Set the command text.
Set cmdCommand = New ADODB.Command
Set cmdCommand.ActiveConnection = cnnConn
With cmdCommand
    .CommandText = "SELECT MAX(id) AS LastId FROM Table1"
    .CommandType = adCmdText
    .Execute
End With

' Open the recordset.
Set rstRecordset = New ADODB.Recordset
Set rstRecordset.ActiveConnection = cnnConn
rstRecordset.Open cmdCommand
MsgBox("last id = " & rstRecordset.Fields(0).value & ""))

' Close the connections and clean up.
cnnConn.Close
Set cmdCommand = Nothing
Set rstRecordset = Nothing
Set cnnConn = Nothing

未定义错误1类型“DAO.Recordset”。未声明错误2“CurrentDB”。由于它的保护级别,它可能无法访问。“我试图通过添加“Microsoft ActiveX Data Objects 2.x Library”来修复错误,但没有修复第二个错误。”我将添加我的参考资料,可能会有所帮助。在我完全利用该功能之前,我似乎需要学习adodb。。。我现在就投赞成票:)