1

Using primarily the macro recorder, I created a VBA macro that sets up a Vendor list connection between Excel and a QuickBooks file.

Sub RefreshVendorList()
'
' RefreshVendorList Macro

'DatabaseName=3ae39a3bfa964f61a6f974654c1ddbe9;

Columns("C:E").Select
Selection.Delete Shift:=xlToLeft

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
    "ODBC;Driver={QB SQL Anywhere};UID=Purchasing;;ServerName=QB_data_engine_21;AutoStop=NO;" _
    ), Array("Integrated=NO;Debug=NO;DisableMultiRowFetch=NO")), Destination:= _
    Range("$C$1")).QueryTable
    .CommandText = Array( _
    "SELECT v_lst_vendor.name AS 'Vendor Name', v_lst_vendor_type.name AS 'Type', v_lst_vendor.is_hidden" & Chr(13) & "" & Chr(10) & "FROM QBReportAdminGroup.v_lst_vendor v_lst_vendor, QBReportAdminGroup.v_lst_vendor_type v_lst_vendo" _
    , _
    "r_type" & Chr(13) & "" & Chr(10) & "WHERE v_lst_vendor_type.id = v_lst_vendor.vendor_type_id AND ((v_lst_vendor.is_hidden=0) AND (v_lst_vendor_type.name='MBO'))" & Chr(13) & "" & Chr(10) & "ORDER BY v_lst_vendor.name, v_lst_vendor_type.name" _
    )
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "Table_PA_Vendor_List"
    .Refresh BackgroundQuery:=False
End With

'delete the "ishidden" column
Columns("E:E").Delete

End Sub

I attached the code to a button so the user can update the vendor list. With the QuickBooks file open, this snippet works most of the time. This is what I would like to change, and there are two way I think it could be accomplished:

Option 1. Every time the button is pressed (and the ODBC connection is re-created), it asks the user for the password. I would like to pass the password instead of asking the user. Changing .SavePassword to True doesn't do it.

Option 2. I think there is a better way to set up the ODBC connection so it doesn't have to be "re-created" by a macro just to refresh it. As long as QuickBooks and this Excel file stay open, I am able to use the built-in refresh button in Excel (under Data -> Refresh All). However, if I close the Excel file and the QuickBooks file, re-open them and try pressing refresh, I get the following error, which is the reason I created a macro to re-create the ODBC connection:

Database not found ODBC error

4

1 回答 1

1

您正在使用自定义报告 ODBC 驱动程序。这由 QuickBooks 提供,仅用于报告目的。您只能在公司文件打开时使用此功能,并且每次连接时都必须输入报告用户的密码。这就是 Intuit 设计它的工作方式。

可以编写 VBA 代码来创建并保持打开 ODBC 连接,这样您就不必在每个会话中多次完成登录过程。但是,这不仅仅是要复制的代码片段;您必须有一个用于连接的全局变量并正确管理它。

一个更简单的解决方案是使用QODBC 驱动程序而不是自定义报告驱动程序。我相信您会得到一份与 QuickBooks Enterprise 捆绑在一起的副本。我假设你有企业版,因为你有自定义报告。QODBC 需要一些设置,但是一旦设置好,您应该能够避免登录提示并获得与数据库的一致连接。

于 2012-05-11T05:21:20.467 回答