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: