¤ I have written a program that uses ODBC connections. Can
¤ anyone tell me how to have it create the System DSN for
¤ an access database on load? or in the deployment package?
As Bill mentioned you should avoid using ODBC with Access. The Jet OLEDB Provider is the preferred
method.
However, I posted this a week ago if you should still happen to need it.
The following is a conversion from VB 6.0 code in MS KB article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;171146
'Constant Declaration
Private Const ODBC_ADD_DSN As Short = 1 ' Add data source
Private Const ODBC_CONFIG_DSN As Short = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN As Short = 3 ' Remove data source
Private Const vbAPINull As Integer = 0 ' NULL Pointer
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Integer, ByVal
fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs)
Handles Command1.Click
Dim intRet As Integer
Dim strDriver As String
Dim strAttributes As String
'Set the driver to SQL Server because it is most common.
strDriver = "SQL Server"
'Set the attributes delimited by null.
'See driver documentation for a complete
'list of supported attributes.
strAttributes = "SERVER=SomeServer" & Chr(0)
strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr(0)
strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr(0)
strAttributes = strAttributes & "DATABASE=pubs" & Chr(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
If intRet <> 0 Then
MsgBox("DSN Created")
Else
MsgBox("Create Failed")
End If
End Sub
Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs)
Handles Command2.Click
Dim intRet As Integer
Dim strDriver As String
Dim strAttributes As String
'Set the driver to SQL Server because most common.
strDriver = "SQL Server"
'Set the attributes delimited by null.
'See driver documentation for a complete list of attributes.
strAttributes = "DSN=DSN_TEMP" & Chr(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, strDriver, strAttributes)
If intRet <> 0 Then
MsgBox("DSN Deleted")
Else
MsgBox("Delete Failed")
End If
End Sub
Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)