excel add-in with data from access
Hi
I'm posting this here, because i'm not sure where else and i have a gut feeling i will be doing this with VB.
My goal is the following:
I have an Access database (with four fields, of which the first one is the key) which I'd like to use in Excel.
The table contains about 1,5 million records, and the idea is the user in Excel should enter a number (which should represent the first field in the table), and by the click of a button, excel should pick up the three other fields of the record.
Now, I have searched a lot, and am not sure what's the best way to approach such a challenge.
I'd much appreciate any advice on the matter, as this project is of quite an importance to me.
Thanks very much in advance
pygmalion
pygmalion
Junior Poster in Training
71 posts since Dec 2006
Reputation Points: 12
Solved Threads: 1
Thank you very much for your answer. I did what you said and it worked fine. Now, when I try to tweak the code somewhat to get what I ultimately want, I start getting nasty errors.
This is the code I currently have.
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 24/04/2009 by User
'
Dim quote As Variant
quote = Range("A8").Text
With ActiveSheet.QueryTables.Add(Connection:=Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\db1.mdb;DefaultDir=C:\;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"), Destination:=Range("A9"))
.CommandText = Array("SELECT `CATERPILLAR-PRICES-2009 Query`.Field1, `CATERPILLAR-PRICES-2009 Query`.Field2, `CATERPILLAR-PRICES-2009 Query`.Field3, `CATERPILLAR-PRICES-2009 Query`.Field4" & Chr(13) & "" & Chr(10) & "FROM `CATERPILLAR-PRICES-2009 Query` `CATERPILLAR-PRICES-2009 Query`" & Chr(13) & "" & Chr(10) & "WHERE (`CATERPILLAR-PRICES-2009 Query`.Field1= & quote &)")
.Name = "Query from CatDB"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Range("E6").Select
End Sub
When executing this code, I get the following error (which seems to originate from the "ODBC;DSN=MS..."-line).Run-time error '13':
Type mismatch
What i'd like to achieve more is:
1) Arrange the results to come to different cells (not just one next to the other), plus without the column titles.
2) Excel should execute this on more than one entry (entries are listed in same column one below the other).
3) How to execute that macro through a toolbar button [although i'm sure to find this online explained somewhere]
Any help with this is very much appreciated. I've been working on this problem for over a week (tried many things, including straight from Visual Studio in C#, but to no avail), and for the first time I'm starting to see progress (thanks totimothybard!).
Thanks again in advance
pygmalion
pygmalion
Junior Poster in Training
71 posts since Dec 2006
Reputation Points: 12
Solved Threads: 1
Thanks for your answer. Here's the original code (created automatically, as you had suggested), which I then intermingled with other code I found in online examples. Maybe I should go back and depart only from this code?
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 27/04/2009 by User
'
'
With Selection.QueryTable
.Connection = Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\db1.mdb;DefaultDir=C:\;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
))
.CommandText = Array( _
"SELECT `CATERPILLAR-PRICES-2009 Query`.Field1, `CATERPILLAR-PRICES-2009 Query`.Field2, `CATERPILLAR-PRICES-2009 Query`.Field3, `CATERPILLAR-PRICES-2009 Query`.Field4" & Chr(13) & "" & Chr(10) & "FROM `CATERPILLAR-PRICES-2009 Que" _
, _
"ry` `CATERPILLAR-PRICES-2009 Query`" & Chr(13) & "" & Chr(10) & "WHERE (`CATERPILLAR-PRICES-2009 Query`.Field1='5N8380')" _
)
.Refresh BackgroundQuery:=False
End With
End Sub
About the other subjects:
1) I see. And how do I specify in the VBA code (above) the target sheet for the retrieving of data?
2) This sounds excellent, but you could you please go a little more in details, in how I would practically go about and do that. (My syntaxic knowledge in VBA is quite limited).
3) Thanks. I hope I can find some recource on my own.
Thanks again very much for all your support. Seriously, very much appreciated.
Regards
pygmalion
pygmalion
Junior Poster in Training
71 posts since Dec 2006
Reputation Points: 12
Solved Threads: 1
Thanks very much, it works like a charm.
Finally, I'm having difficulty with the option of being able to apply the query to many entries at the same time (which are just entered one under the other). I tried quite a few things (from just extra variables and changing the select-statement to include those, to loops) but it just doesn't seem to be working. I always somehow end up with the error statement:
Run-time error '1004'
Anyway, hope you can help me out with this last thing.
Thanks again very much for your help.
pygmalion
Junior Poster in Training
71 posts since Dec 2006
Reputation Points: 12
Solved Threads: 1
Thank you so much for the code, it works amazingly well. The way I arranged it is that user enters everything in a 3rd sheet, while the data is formatted as should be in the other two sheets (referenced twice the same for different languages).
One problem that does still seem to occur, is that I can only give five entries to generate (while I have entered a range of almost 35 entries - which I would like to put till at least 250). Any more than that, and Excel gives a Run-time error ('13' or '1004' - "Type mismatch"). It may be a bit hard to explain, so I uploaded the file to my server. Maybe you can have a look at it, and see what causes the trouble.
Also, if a different cell (than one of the entered entries) is selected while executing the Macro, a same error is given.
Thank you so much
pygmalion
[After some consideration, I decided to send it to you via private message - if anyone else is willing to help, I'd gladly send it to him/her as well]
pygmalion
Junior Poster in Training
71 posts since Dec 2006
Reputation Points: 12
Solved Threads: 1