create DSN not working

K

Keith G Hicks

Can anyone tell me why this fails? It works if I leave out the UID and PWD
lines but when those are in there it does nothing. It returns 0. I can
create the DSN manually in ODBC datasources using all the same parameters.


Option Compare Database
Option Explicit

Const ODBC_ADD_SYS_DSN = 4 'Add data source
Const ODBC_CONFIG_SYS_DSN = 5 'Configure (edit) data source
Const ODBC_REMOVE_SYS_DSN = 6 'Remove data source

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal _
hwndParent As Long, ByVal fRequest As Long, ByVal _
lpszDriver As String, ByVal lpszAttributes As String) As Long

Private Sub Command0_Click()
Call Build_SystemDSN
End Sub

Sub Build_SystemDSN()

Dim ret%, Driver$, Attributes$

Driver = "SQL Server" & Chr(0)
Attributes = "DSN=Keith Test 2" & Chr(0)
Attributes = Attributes & "DESCRIPTION=Keith Test for sqldb1" & Chr(0)
Attributes = Attributes & "SERVER=server" & Chr(0)
Attributes = Attributes & "DATABASE=sqldb1" & Chr(0)
Attributes = Attributes & "UID=sa" & Chr(0)
Attributes = Attributes & "PWD=abc123" & Chr(0)

ret = SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, Driver, Attributes)

'ret is equal to 1 on success and 0 if there is an error
If ret <> 1 Then
MsgBox "DSN Creation Failed"
End If

End Sub


Thanks,

Keith
 
D

Douglas J. Steele

Do you really need a DSN? Take a look at
http://www.accessmvp.com/djsteele/DSNLessLinks.html for one way to use
DSN-less connections.

If you're determined to have a DSN, take a look at
http://support.microsoft.com/?id=184608 for another way to create them.

Finally, I don't have any SQL Server DSNs on this machine, so I can't check,
but check the registry for a DSN that works. Does it actually contain
parameters named UID and PWD, or are they called something else?
 
K

Keith G Hicks

Yes, I do need a DSN. I actually need it for something in Delphi language to
connect to a 3rd party tool, not MS Access. But I know Access and found more
examples for DSN creation out there. Anyway, I decided to just write
directly to the registry instead and it works peachy. Thanks everyone for
the information.

Keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top