Avatar billede Kim Neesgaard Seniormester
23. oktober 2008 - 13:14 Der er 6 kommentarer og
2 løsninger

Åbne forbindelse far Access til SQL Server

Jeg har tidligere åbnet en forbindelse fra Access til en Oracle database på følgende måde:

Public Conn As New ADODB.Connection
Conn.Open "Provider=MSDAORA;Data Source=sourcenavn", "user", "password"

og det gik fint nok.

Nu skal jeg i stedet åbne en forbindelse til en SQL server, hvor de samme tabeller ligger, men det kan jeg ikke få til at virke! Jeg har servernavn, database, brugenavn og password til SQL serveren, men hvordan skal syntaksen være?

Konkrete forslag modtages meget gerne!!
Avatar billede terry Ekspert
23. oktober 2008 - 16:30 #1
heres a link to an example which should help you
http://www.dreamincode.net/forums/showtopic24677.htm
Avatar billede Kim Neesgaard Seniormester
24. oktober 2008 - 10:13 #2
I have tried it and now it tells me that the server does not exist or access not allowed (another error than before). I have contacted our IT department as I can make connection with a Visual Studio project with the same information. I will let you know.
Avatar billede terry Ekspert
24. oktober 2008 - 10:23 #3
have you tried linking the tables using an ODBC data source? Or an Access data project (ADP)
Avatar billede Kim Neesgaard Seniormester
24. oktober 2008 - 10:52 #4
I do this:

Public Conn As New ADODB.Connection
Conn.Open _
"Provider = sqloledb;" & _
"Data Source={SQLServername};" & _
"Initial Catalog={DBname};" & _
"User ID={user};" & _
"Password={pw};"
Avatar billede terry Ekspert
24. oktober 2008 - 11:02 #5
You could try one of the other methods to see if you can connect.
Avatar billede Slettet bruger
24. oktober 2008 - 11:48 #6
Brug nedenstående function, således:

Call AttachDSNLessTable("TabelNavnAccess", "dbo.TabelnavnSQLserver", "Servernavn", "DatabasenavnPåServer", "BrugerNavn", "Password")


Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
    On Error GoTo AttachDSNLessTable_Err
    Dim td As TableDef
    Dim stConnect As String
   
    For Each td In CurrentDb.TableDefs
        If td.Name = stLocalTableName Then
            CurrentDb.TableDefs.Delete stLocalTableName
        End If
    Next
     
    If Len(stUsername) = 0 Then
        '//Use trusted authentication if stUsername is not supplied.
        stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
    Else
        '//WARNING: This will save the username and the password with the linked table information.
        stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
    End If
    Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
    CurrentDb.TableDefs.Append td
    AttachDSNLessTable = True
    Exit Function

AttachDSNLessTable_Err:
   
    AttachDSNLessTable = False
    MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description

End Function
Avatar billede Kim Neesgaard Seniormester
27. oktober 2008 - 10:17 #7
Denne virker:

Set Conn = CreateObject("ADODB.Connection")
Conn.Open _
"Provider=sqloledb;" & _
"Data Source=ServerID;" & _
"Initial Catalog=Databasenavn;" & _
"User Id=BrugerID;" & _
"Password=pw"
Avatar billede terry Ekspert
27. oktober 2008 - 12:16 #8
thanks
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