2012-08-28 210 views
1

我有一個類正在處理我與Access 2003數據庫的連接。我只想爲Access 07/10 .accdb文件設置相同的功能。任何幫助表示讚賞!謝謝!Excel VBA連接到Access 2010

這是我引用的列表和類對象的副本

參考文獻:

  • 的Microsoft Access 14.0對象庫
  • Microsoft DAO 3.6對象庫

ConnectionClass:

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 
打開ACCDB
+0

我也'昏暗OpenConn作爲對象'而不是'DAO.Database'。這允許獨立於版本的代碼(但刪除了在OpenConn或DAO之後鍵入'.'時出現的可愛彈出窗口) – SeanC

+0

當我從.mdb切換到.accdb時,它提供了一個無法識別的錯誤。我會盡力在以後找出確切的錯誤。 – JKK

+0

我打開了文件,繼續並將其翻轉到.accdb。它在runSQL函數上失敗,給出運行時錯誤91,對象變量或未設置塊變量。函數runVolumeReport運行良好,但是可以啓動數據庫中的宏。只是不喜歡我試圖從Access中取出記錄集的方式,當我將它切換到.accdb格式時 – JKK

回答

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應用程序對象,因此您可以嘗試將OpenConn設置爲ObjAccess.CurrentDb

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

OTOH,您可以用OpenConn分配完全,如果你改變你的runSQL功能是這樣的...

Public Function runSQL(ByVal sql As String) As Recordset 
    'Set runSQL = OpenConn.OpenRecordset(sql) ' 
    Set runSQL = ObjAccess.CurrentDb.OpenRecordset(sql) 
End Function 
+0

太好了,謝謝! – JKK

0

一種方法(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 

我引用(訪問2010):
list of references
我認爲關鍵的一年,你將需要添加將是微軟ActiveX數據對象XX庫

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 

昏暗CON作爲新的OleDbConnection(「供應商=微軟。 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() 
+0

嗨,彼得,歡迎來到Stack Overflow。請花些時間看看[答案],並考慮在帖子中添加一些支持性文字,以解釋您的代碼的功能。 – TZHX