access数据库连接_从MS Access连接到不同的数据库

access数据库连接

这是一些我用来连接三种数据库的vba代码。

“ somefunctioncall”函数利用“ opendb”函数连接到数据库...

您将必须在后一个函数中输入参数,并取消注释“首选连接字符串”

您可能对此代码有些摆弄,但它应该可以工作...我通过谷歌搜索“连接字符串”使这些字符串脱离了网络...

这些数据库不必位于同一台PC上,只要您可以访问它们,它们就可以位于任何服务器上。

[2014-09-27:z:每个OP请求,随后是修改的代码块]

 
    Public Function opendb() As ADODB.Connection
            ' This function returns an ADODB.Connection object 
            ' which is required for accessing different types of databases
            '
            ' Her we declare three strings to access database other than MS Access files on your network
            ' as it was in my case
            Dim ORACLE_ConnString As String
            Dim SQL_ConnString As String
            Dim MSACCESS_ConnString As String
            '*****************************************************************
            ' This variable is used only when accessing a MS Access database
            ' NOTE: I always keep the form separate from the database when using Access for Database and front end forms
            Dim MSAdbPathAndName As String 
            MSAdbPathAndName = "C:\Reports\Database\msaccessdatabase.mdb"
            'Probably could use a MapPath function here ...
            '***************************************************************** 
            Dim DBN As String ' "DATABASENAME"
            Dim UID As String '"yourusername"
            Dim PWD As String '"yourPASSWORD"
            Dim DBS As String '"SERVERNAME" 
            'Here is where you want to set your parameters for connecting to the desired database
            DBS = "thisismywebservername"
            UID = "thisisme"
            PWD = "thisismypassword"
            DBN = "thisismydbname" 
            'The connection strings are different for each type of database and network setup
            ORACLE_ConnString = "Provider=msdaora;Data Source=" & DBN & _
                                ";User Id=" & UID & ";Password=" & PWD & ";"
            SQL_ConnString = "Provider=SQLOLEDB;DATA SOURCE=" & DBS & _
                                ";UID=" & UID & _
                                ";PWD=" & PWD & _
                                ";Initial Catalog=" & DBN & ";"
            MSACCESS_ConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
                                    MSAdbPathAndName & ";" 
            'Here we declare and set the ADODB.Connection
            opendb = New ADODB.Connection 
            'And finally you must decide which type of connection you are going to use... only one :) 
            '    *************** preferred connection string ***********
            '    UNCOMMENT the ONE you need: if you want to connect to MS SQL then SQL_ConnString is the one you want
            '    *******************************************************  
            '    opendb.Open ORACLE_ConnString
            opendb.Open(SQL_ConnString)
            '    opendb.Open MSACCESS_ConnString 
        End Function
        Public Function stringReturningFunctionCall() As String
            ' The connection object is required for accessing the database and must be declared
            Dim objConn As ADODB.Connection
            ' The  recordset object is required for storing the queried dataset and must be declared
            Dim recSet As ADODB.Recordset
            'This functioncal return a string, we declare and set it to be empty
            Dim stringToReturn As String
            stringToReturn = ""
            'setting the connection object from a function that returns an ADODB.Connection 
            objConn = opendb
            'setting the Recordset object using the Connection object to excute the SQL statement
            recSet = objConn.Execute("SELECT [field1],[field2],[field3] FROM [sometable] order by [somefield] ")
            Do While Not rs.EOF ' run through the recordset until at the end
                'add your own code here
                stringToReturn = someStringAppendingFunction(stringToReturn, recSet(0) & _
                                                                ";" & recSet("field2") & _
                                                                ";" & recSet("field3"), ";")
                recSet.MoveNext()
            Loop
            'clean up time
            objConn.Close()
            objConn = Nothing
            recSet = Nothing
            'return the result
            stringReturningFunctionCall = stringToReturn
        End Function

翻译自: https://bytes.com/topic/access/insights/917619-connecting-different-databases-ms-access

access数据库连接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值