I have an excel spreadsheet with a column that contains an ID# in the form AA12. There is also an access database with this same information.
I'd like to design an "update" button, that will check the last ID# in the spreadsheet, connect to the database, and paste any new entries into the end of the spreadsheet. There's also other fields in the database (called Other for now) for that ID# that I'd like to paste into certain other columns.
I've figured out how to set up a data connection to the database, but this seems to paste as an active link, whereas I'd like to paste as text.
All I have so far is the below. If somebody can help point me in the right direction to
a) check spreadsheet for last entry ID#
b) import new database entries and paste below last ID# in spreadsheet
c) paste as text not active link
d) specify which spreadsheet column to paste certain database fields
I'd like to design an "update" button, that will check the last ID# in the spreadsheet, connect to the database, and paste any new entries into the end of the spreadsheet. There's also other fields in the database (called Other for now) for that ID# that I'd like to paste into certain other columns.
I've figured out how to set up a data connection to the database, but this seems to paste as an active link, whereas I'd like to paste as text.
All I have so far is the below. If somebody can help point me in the right direction to
a) check spreadsheet for last entry ID#
b) import new database entries and paste below last ID# in spreadsheet
c) paste as text not active link
d) specify which spreadsheet column to paste certain database fields
Code:
Private Sub CommandButton1_Click()
' Importdb Macro
'
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DBQ=C:\database.mdb;DefaultDir=C:\;Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS Access;MaxBufferSize=20" _
), Array( _
"48;MaxScanRows=8;PageTimeout=5;;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;" _
)), Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT Query1.FileCode, Query1.OtherCode, Query1.Other2Code, Query1.Other3Code" _
, "O" & Chr(13) & "" & Chr(10) & "FROM `C:\`.Query1 Query1")
.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_Query_from_db"
.Refresh BackgroundQuery:=False
End With
End Sub