http://www.csdn.net/expert/topic/709/709642.xml?temp=.5447962http://www.csdn.net/expert/topic/709/709234.xml?temp=.891781

解决方案 »

  1.   

    Private Const ODBC_ADD_DSN = 1
    Private Const ODBC_CONFIG_DSN = 2
    Private Const ODBC_REMOVE_DSN = 3
    Private Const ODBC_ADD_SYS_DSN = 4
    Private Const ODBC_REMOVE_SYS_DSN = 6
    Private Const vbAPINull As Long = 0&
    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
    Public Sub CreateDSN(sDSN As String)
    On Error Resume Next
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String
    sDriver = "SQl server"
    sAttributes = "DSN=" & sDSN & Chr$(0)
    sAttributes = sAttributes & "Server=(local)" & Chr$(0)
    sAttributes = sAttributes & "Database=webstation" & Chr$(0)
    nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, sDriver, sAttributes)
    End Sub
    Public Sub DeleteDSN(sDSN As String)
    On Error Resume Next
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String
    sDriver = "SQl server"
    sAttributes = sAttributes & "DSN=" & sDSN & Chr$(0)
    nRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_SYS_DSN, sDriver, sAttributes)
    End Sub
    Private Sub Command1_Click()
    CreateDSN "Test"
    End Sub
    Private Sub Command2_Click()
    DeleteDSN "Test"
    End Sub
      

  2.   

    Dim sODBCDsn As ODBCTool.Dsn
    Dim sAns As Boolean
    Set sODBCDsn = New ODBCTool.Dsn
    sAns = sODBCDsn.CreateDSN("HR", "Sql server", "", App.Path & "\humanResource.mdb", "", "", "", False, "dbq=" & App.Path & "\Humanresource.mdb")
    If sAns = False Then
        MsgBox "建立数据源失败! 
        Exit Sub
    endif要在references下把ODBC的那个选中