1

我有一个类正在处理我与 Access 2003 数据库的连接。我想仅为 Access 07/10 .accdb 文件设置相同的内容。任何帮助表示赞赏!谢谢!

这是我的引用列表和类对象的副本

参考:

  • Microsoft Access 14.0 对象库
  • Microsoft DAO 3.6 对象库

连接类:

Option Explicit

Private Const DbFile = "\\server\folders\Report.mdb"
Dim OpenConn As DAO.Database
Dim ObjAccess As Object

Private Sub Class_Initialize()
    On Error Resume Next
    Set OpenConn = DAO.OpenDatabase(DbFile)
    If Err.Number = 3024 Then MsgBox "Check connection string in the VBA StaticClass object", vbOKOnly

    Set ObjAccess = CreateObject("Access.Application")
    ObjAccess.Visible = False
    ObjAccess.OpenCurrentDatabase (DbFile)
End Sub

Public Function runSQL(ByVal sql As String) As Recordset
    Set runSQL = OpenConn.OpenRecordset(sql)
End Function

Public Function runVolumeReport(ByVal inMacro As String)
    ObjAccess.DoCmd.RunMacro inMacro
End Function

Public Function closeResources()
    Set ObjAccess = Nothing
    OpenConn.Close
End Function
4

3 回答 3

1

中有一个问题Class_Initialize

On Error Resume Next
Set OpenConn = DAO.OpenDatabase(DbFile)
If Err.Number = 3024 Then MsgBox "Check connection string in the VBA StaticClass object", vbOKOnly

因为On Error Resume Next,除了 3024(“找不到文件”)之外的任何错误都将静默通过,并且 OpenConn 不会按照您的意愿设置。稍后当您尝试使用时OpenConn,您将触发另一个错误。而且,在评论中,您报告说您确实收到了此行的另一个错误:

Set runSQL = OpenConn.OpenRecordset(sql)

不幸的是,由于On Error Resume Next,我们不知道为什么没有设置OpenDatabase失败OpenConn。由于ObjAccess似乎作为 Access 应用程序对象工作,您可以尝试设置OpenConnObjAccess.CurrentDb.

Private Sub Class_Initialize()
    Set ObjAccess = CreateObject("Access.Application")
    ObjAccess.Visible = False
    ObjAccess.OpenCurrentDatabase DbFile
    Set OpenConn = ObjAccess.CurrentDb
End Sub

OpenConnOTOH,如果您像这样更改功能,则可以完全省去runSQL...

Public Function runSQL(ByVal sql As String) As Recordset
    'Set runSQL = OpenConn.OpenRecordset(sql) '
    Set runSQL = ObjAccess.CurrentDb.OpenRecordset(sql)
End Function
于 2012-08-29T15:39:08.487 回答
0

打开 accdb (SQL Server) 表的一种方法是:

Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
Dim strSQL As String

strSQL = "select SomeStuff from SomeTable"

cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandText = strSQL ' you can put in the SQL directly, 
                         ' but I find the string easier to manipulate away from the .CommandText
Set rs = cmd.Execute

我的参考资料(Access 2010):
参考文献列表
我认为您需要添加的关键是 Microsoft ActiveX Data Objects XX Library

于 2012-08-29T14:15:18.713 回答
0
Imports System.Data.OleDb

Public Class Form1
    Dim strSQL As String
    Dim ds As New DataSet
    Dim strConnection As String
    Dim DBconnection As New OleDbConnection
    Dim oledbAdapter As New OleDbDataAdapter
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\Informatica\April - Juni\Acces\db-games.accdb"
        DBconnection = New OleDbConnection(strConnection)
        strSQL = "SELECT * from tbl_games"

        Try
            DBconnection.Open()
            oledbAdapter = New OleDbDataAdapter(strSQL, DBconnection)
            oledbAdapter.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        DBconnection.Close()
    End Sub
End Class

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\Informatica\Acces\db_Games.accdb;Persist Security Info=False") Dim cmd As New OleDbCommand

    con.Open()

    cmd.Connection = con

    cmd.CommandText = "INSERT INTO tbl_gerne(Omschrijving) VALUES('adventure')"
    cmd.ExecuteNonQuery()

    con.Close()
于 2015-06-11T14:54:52.583 回答