Article ID: 126940
Article Last Modified on 10/20/2003
Option Explicit
Const ODBC_ADD_DSN = 1 ' Add a new data source.
Const ODBC_CONFIG_DSN = 2 ' Configure (edit) existing data source.
Const ODBC_REMOVE_DSN = 3 ' Remove existing data source.
' Enter the following three lines as one, single line:
Declare Function SQLConfigDataSource Lib "odbcinst.dll"
(ByVal hwnd as Integer, ByVal fRefresh as Integer,
ByVal szDriver as String, ByVal szAttributes as String) As Integer
' Enter the following two lines as one, single line of code:
Sub RegisterODBCDatabase (dsn As String, driver As String,
silent As Integer, attributes As String)
Dim ret As Integer
On Error GoTo errorhandler
RegisterDatabase dsn, driver, silent, attributes
Exit Sub
errorhandler:
If Err = 3146 Then ' ODBC Call Failed.
Dim temp As String
Dim spot As Integer
While InStr(attributes, Chr(13)) ' Replace Carriage returns
spot = InStr(attributes, Chr(13)) ' with nulls.
Mid(attributes, spot, 1) = Chr(0)
Wend
attributes = attributes & Chr(0) & Chr(0) ' End of attribute section.
temp = "DSN=" & dsn & Chr(0) & attributes
ret = SQLConfigDataSource(0, ODBC_ADD_DSN, driver, temp)
' ret is equal to 1 on success and 0 if there is an error.
If ret <> 1 Then
MsgBox "SQLConfigDataSource call failed"
Else
MsgBox " SQLConfigDataSource call succeeded!"
End If
End If
Exit Sub
End Sub
Sub Command1_Click ()
Dim Att As String, MyDb As Database
Att = "Description = SQL Server on server Clinton" & Chr$(13)
Att = Att & "OemToAnsi=No" & Chr$(13) ' Build keywords string.
Att = Att & "Network=DBNMP3" & Chr$(13)
Att = Att & "Address=\\CLINTON\PIPE\SQL\QUERY" & Chr$(13)
Att = Att & "Database=Pubs"
' Update ODBC.INI.
RegisterDatabase "Clinton", "SQL Server", True, Att
End Sub
Additional query words: 3.00
Keywords: kbbug KB126940