Avatar billede micaud Mester
12. marts 2016 - 21:56 Der er 11 kommentarer og
1 løsning

Skift ODBC streng i makro

Hej.

Jeg har 4 forskellige regnskaber i NAV som jeg tilgår via ODBC i MS Access.

Jeg hiver diverse tabeller ud i Access via pass-through forespørgsler og laver så yderligere forespørgsler derfra, for at gøre data klar til Pivot tabeller i Excel.

Jeg er helt ny i pass-through, og jeg vil gerne have én Access database, hvor jeg kan lave alle mine pass-through til alle 4 regnskaber.

Hidtil har jeg kun kunnet bruge en ODBC adgang pr. database, så jeg har altså 4 databaser nu - en per regnskab.

Jeg har i dag lavet 2 pass-through forespørgsler med disse ODBC tilslutningsstrege:

1) ODBC;DSN=DynamicsSverige;DBQ=\\SBS\RedirectedFolders\MIVE\My Documents;CODEPAGE=1252;
2) ODBC;DSN=DynamicGroup88;DBQ=\\SBS\RedirectedFolders\MIVE\My Documents;CODEPAGE=1252;

De fungere fint nok, men jeg er nød til at lukke databasen ned, hvis jeg vil kører den anden - altså jeg kan ikke kører begge pass-through uden at lukke access mellem forespørgslerne.

Hjælp, hvordan får jeg access til at acceptere skifte ODBC-tilslutningsstreng via vba, således jeg kan kører udtræk fra alle mine regnskaber i samme makro/VBA?

Det skal naturligvis være automatisk det hele. Jeg kalder alle mine forspørgsler fra et VBA-modul, så kan jeg implementere et ODBCstreng skifte her, så vil det være perfekt.

Min pass-through ser således ud: SELECT * FROM Finanspost

Håber I forstår og kan hjælpe.

Tak.
Avatar billede micaud Mester
12. marts 2016 - 21:59 #1
Glemte lige at sige - kan man evt. blot have en pass-through forespørgsel, hvor ODBC-stregen ændres?

På den måde kan jeg jo nøjes med én forespørgsel, der kigger ned i samme pass-through blot med forskellige ODBC tilslutninger.

Hvis ja, så kan jeg virkelige optimere mine databaser betydelig. I dag har jeg jo alt gange 4...
Avatar billede terry Ekspert
13. marts 2016 - 10:38 #2
Its possible to alter ODBC connection string for your objects so that you only need one query, assuming that the fields etc are the same in each table.


you can find a number of examples on the web on how to re-link your ODBC tables and queries.

For example here.

http://stackoverflow.com/questions/564265/how-to-refresh-linked-tables-in-an-access-mdb-when-odbc-changes

and here for your queries
http://stackoverflow.com/questions/7332703/how-do-you-alter-the-odbc-connection-string-for-existing-ms-access-pass-through
Avatar billede micaud Mester
13. marts 2016 - 20:16 #3
Hi Terry.

Great I did hope you will answer as you help a lot of people in here.

I am not that good at VBA in Access so can you help me writing the correct sub or public funcktion which I believe I must enter into a module in visual basic?

How do I get this: 

ODBC;DSN=DynamicsSverige;DBQ=\\SBS\RedirectedFolders\MIVE\My Documents;CODEPAGE=1252;

into the module?

Thx. Michael.
Avatar billede micaud Mester
13. marts 2016 - 20:55 #4
Hi again.

I got this to work:

Sub ODBC()
Dim qdf As QueryDef

For Each qdf In CurrentDb.QueryDefs
    If qdf.Type = dbQSQLPassThrough Then
        qdf.Connect = "ODBC;DSN=DynamicGroup88;DBQ=\\SBS\RedirectedFolders\MIVE\My Documents;CODEPAGE=1252;"
    End If
Next
End Sub

Though I need to restart MSAcces every time I want to change the ODBC to another.

I have this Pass-Through SQL:

SELECT  *
FROM Finanspost
where løbenr_ > 330000

I need to add a column with the Country name as I use the same query know but how do I add a column name to a pass-through?

This dos not work:

SELECT  *, 'Denmark' AS Country
FROM Finanspost
where løbenr_ > 330000

And finally how do I make my WHERE statement dynamic? (Very needed due to a database of more than 300000 rows)

I want only new rows added to my local database but

SELECT TOP 10 * FROM FINANSPOST dos not work and I also tryed LIMIT 10 !!??

I would be a great help if you can help me out as I struggled for several years with to many queries for each 4 Countries.

Thx Michael
Avatar billede micaud Mester
14. marts 2016 - 10:45 #5
Thx Terry - I hope you will look at the two last questions also.
Avatar billede terry Ekspert
14. marts 2016 - 11:59 #6
Hi micaud, sorry for not getting back sooner, lots of other tasks keeping me busy :-)

It should not be necessary to restart Access after changing ODBC connection string. And although I've never had the need to use NAV I dont think that would make any difference.

To get the connection string into the Modulke I woul duse a Function rather than a SUB.

Public Function RelinkTables(ByVal conStr As String) As Boolean

...

End Function

Then call function like this

Dim bConnected As Boolean
   
    bConnected = RelinkTables("YourConnectionStringGoesHere")

  if bConnected  Then ....
Avatar billede terry Ekspert
14. marts 2016 - 12:06 #7
This dos not work:

SELECT  *, 'Denmark' AS Country
FROM Finanspost
where løbenr_ > 330000


? what is the problem?



I want only new rows added to my local database but

SELECT TOP 10 * FROM FINANSPOST dos not work and I also tryed LIMIT 10 !!??

Again, what is the problem? You say new rows added but what has TOP 10 to do with adding?
Avatar billede terry Ekspert
14. marts 2016 - 12:08 #8
dynamic where statements ...

You can change the SQL of a query using code.

Dim qd As QueryDef
Set qd = CurrentDb.QueryDefs("MyQuery")
qd.SQL = "SELECT Category FROM Categories WHERE ...."
Avatar billede micaud Mester
14. marts 2016 - 14:11 #9
I run it from a public function already.

If I change the code below from "Group88" to "Sverige" then it will nok connect to "Sverige" before I restart Access.


Public Function Grp88_DK()
Dim qdf As QueryDef

For Each qdf In CurrentDb.QueryDefs
    If qdf.Type = dbQSQLPassThrough Then
        qdf.Connect = "ODBC;DSN=DynamicGroup88;DBQ=\\SBS\RedirectedFolders\MIVE\My Documents;CODEPAGE=1252;"
    End If
Next
Avatar billede micaud Mester
14. marts 2016 - 14:29 #10
Terry changing the SQL by code is perfect !!!

Though.... how do I manage the " in this select ("Global dimension 1-kode") AS Afd_?

When I put it into the qd.SQL = "SELECT ("Global dimension 1-kode") AS Afd_ FROM Finanspost" it says:

"Expected: end or statment" because I have the ["] in ("Global dimension 1-kode") AS Afd_


Regarding the dynamic where > ???. Can I use a code to tell the WHERE to be > than the max Løbenr_ in my local table in the database??
Avatar billede terry Ekspert
14. marts 2016 - 16:31 #11
I'm guessing that its a passthrough query your trying to get working?

The syntax in the SQL must be NAV compliant and not Access, maybe try changing " to '


TOP X will give you as many records as it can find within the criteria. So it can return less than X. It may also return records which you already have in your local table.
So it would be an idea to use > SomeNumber rather than TOP X
Avatar billede terry Ekspert
14. marts 2016 - 16:32 #12
No need for () in SQL

SELECT "Global dimension 1-kode" AS Afd
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester