【问题标题】:How do I make my data source in the current directory?如何在当前目录中创建我的数据源?
【发布时间】:2018-01-16 06:24:42
【问题描述】:

我的数据库访问总是在不同的路径中,但在与我的工作簿相同的文件夹中。

Private Sub CommandButton14_Click()

  Dim cn As Object
    Dim rs As Object
    Dim strSql As String
    Dim strConnection As String
    Set cn = CreateObject("ADODB.Connection")

    'here I want to use current directory as path for my mdb
    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=D:\FicheMacro\PGDB.mdb"
    strSql = "SELECT Count(*) FROM AQ_DGE_MOD;"
    cn.Open strConnection
    Set rs = cn.Execute(strSql)
    MsgBox rs.Fields(0) & " rows in MyTable"
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

End Sub

【问题讨论】:

  • 我不熟悉 Connections,但this 可能会有所帮助。

标签: excel vba


【解决方案1】:

正如 cmets 中所说,我不熟悉连接。但类似于以下内容可能会起作用。

'insert this two lines in your code
Dim folderPath As String
folderPath = Application.ActiveWorkbook.Path

'change strConnection to the following
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & folderPath & "\PGDB.mdb"

【讨论】:

  • 很高兴为您提供帮助 :)!
猜你喜欢
  • 2016-07-29
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多