Ben Strombeck
Ben Strombeck

Reputation: 1549

How can I fix my "database not found" ODBC connection error when re-loading my workbook?

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

Upvotes: 1

Views: 5331

Answers (1)

Paul Keister
Paul Keister

Reputation: 13077

You are using the custom reporting ODBC driver. This is provided by QuickBooks for reporting purposes only. You can only used this while the company file is open, and you must enter a password for the reporting user whenever you connect. That's the way Intuit designed it to work.

It would be possible to write VBA code to create and hold open the ODBC connection so that you didn't have to go through the log in process more than once per session. However, this is not just a snippet of code to copy; you'd have to have a global variable for the connection and manage it properly.

A simpler solution would be to use the QODBC driver instead of the custom report driver. I believe you get a copy of this bundled with QuickBooks Enterprise. I'm assuming you have the Enterprise edition, since you have custom reporting. QODBC takes some setup, but once it's set you should be able to avoid the login prompt and get a consistent connection to the database.

Upvotes: 1

Related Questions