Hello everyone,
I am trying to create an odbc using VB code. I can get it to work with the following code if I leave the UserID and Password attributes out. Does anyone know what I am doing wrong. I looked everywhere on the internet for the answer and all I found is code that will create and odbc using the NT login (Trusted connection). I want an ODBC Created using the SQL Server connection with userid = "JACK" and Password = "SPRAT". I have tried UID, Login ID, etc for the attribute but none has worked. Any help would be appreciated.
'Write this code in General Declaration of form
'**************************************
'Windows API/Global Declarations for :Cr
' eating System and File DSN
'**************************************
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const ODBC_ADD_DSN = 4
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
Private Declare Function GetActiveWindow Lib "user32.dll" () As Long
Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (phenv&) As Integer
Private Declare Function SQLAllocConnect Lib "ODBC32.DLL" (ByVal henv&, hDBC&) As Integer
Private Declare Function SQLCreateDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal lpszDriver As String)
Private Declare Function SQLDriverConnect Lib "ODBC32.DLL" (ByVal hDBC As Long, ByVal hwnd As Long, _
ByVal szCSIn As String, ByVal cbCSIn As Long, ByVal szCSOut As String, _
ByVal cbCSMax As Long, cbCSOut As Long, ByVal f As Long) As Long
Private Declare Function SQLAllocStmt Lib "ODBC32.DLL" (ByVal hDBC As Long, HStmt As Long) As Long
Public Function CreateDSN(ByVal pstrServer As String, ByVal pstrDescription As String, ByVal pstrDSN As String, ByVal pstrDataBase As String) As Long
On Error Resume Next
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=" & pstrServer & Chr(0)
strAttributes = strAttributes & "DESCRIPTION=" & pstrDescription & Chr(0)
strAttributes = strAttributes & "DSN=" & pstrDSN & Chr(0)
strAttributes = strAttributes & "DATABASE=" & pstrDataBase & Chr(0)
strAttributes = strAttributes & "Trusted_Connection=No" & Chr$(0)
strAttributes = strAttributes & "UID=sa" & Chr$(0)
strAttributes = strAttributes & "PWD=password " & Chr$(0)
'Calls API to create DSN
CreateDSN = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
End Function
'Write this code in General Declaration of form
'**************************************
'Windows API/Global Declarations for :Cr
' eating System and File DSN
'**************************************
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const ODBC_ADD_DSN = 4
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
Private Declare Function GetActiveWindow Lib "user32.dll" () As Long
Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (phenv&) As Integer
Private Declare Function SQLAllocConnect Lib "ODBC32.DLL" (ByVal henv&, hDBC&) As Integer
Private Declare Function SQLCreateDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal lpszDriver As String)
Private Declare Function SQLDriverConnect Lib "ODBC32.DLL" (ByVal hDBC As Long, ByVal hwnd As Long, _
ByVal szCSIn As String, ByVal cbCSIn As Long, ByVal szCSOut As String, _
ByVal cbCSMax As Long, cbCSOut As Long, ByVal f As Long) As Long
Private Declare Function SQLAllocStmt Lib "ODBC32.DLL" (ByVal hDBC As Long, HStmt As Long) As Long
Public Function CreateDSN(ByVal pstrServer As String, ByVal pstrDescription As String, ByVal pstrDSN As String, ByVal pstrDataBase As String) As Long
On Error Resume Next
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=" & pstrServer & Chr(0)
strAttributes = strAttributes & "DESCRIPTION=" & pstrDescription & Chr(0)
strAttributes = strAttributes & "DSN=" & pstrDSN & Chr(0)
strAttributes = strAttributes & "DATABASE=" & pstrDataBase & Chr(0)
strAttributes = strAttributes & "Trusted_Connection=No" & Chr$(0)
strAttributes = strAttributes & "UID=sa" & Chr$(0)
strAttributes = strAttributes & "PWD=password " & Chr$(0)
'Calls API to create DSN
CreateDSN = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
End Function
I am trying to create an odbc using VB code. I can get it to work with the following code if I leave the UserID and Password attributes out. Does anyone know what I am doing wrong. I looked everywhere on the internet for the answer and all I found is code that will create and odbc using the NT login (Trusted connection). I want an ODBC Created using the SQL Server connection with userid = "JACK" and Password = "SPRAT". I have tried UID, Login ID, etc for the attribute but none has worked. Any help would be appreciated.
'Write this code in General Declaration of form
'**************************************
'Windows API/Global Declarations for :Cr
' eating System and File DSN
'**************************************
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const ODBC_ADD_DSN = 4
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
Private Declare Function GetActiveWindow Lib "user32.dll" () As Long
Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (phenv&) As Integer
Private Declare Function SQLAllocConnect Lib "ODBC32.DLL" (ByVal henv&, hDBC&) As Integer
Private Declare Function SQLCreateDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal lpszDriver As String)
Private Declare Function SQLDriverConnect Lib "ODBC32.DLL" (ByVal hDBC As Long, ByVal hwnd As Long, _
ByVal szCSIn As String, ByVal cbCSIn As Long, ByVal szCSOut As String, _
ByVal cbCSMax As Long, cbCSOut As Long, ByVal f As Long) As Long
Private Declare Function SQLAllocStmt Lib "ODBC32.DLL" (ByVal hDBC As Long, HStmt As Long) As Long
Public Function CreateDSN(ByVal pstrServer As String, ByVal pstrDescription As String, ByVal pstrDSN As String, ByVal pstrDataBase As String) As Long
On Error Resume Next
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=" & pstrServer & Chr(0)
strAttributes = strAttributes & "DESCRIPTION=" & pstrDescription & Chr(0)
strAttributes = strAttributes & "DSN=" & pstrDSN & Chr(0)
strAttributes = strAttributes & "DATABASE=" & pstrDataBase & Chr(0)
strAttributes = strAttributes & "Trusted_Connection=No" & Chr$(0)
strAttributes = strAttributes & "UID=sa" & Chr$(0)
strAttributes = strAttributes & "PWD=password " & Chr$(0)
'Calls API to create DSN
CreateDSN = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
End Function
'Write this code in General Declaration of form
'**************************************
'Windows API/Global Declarations for :Cr
' eating System and File DSN
'**************************************
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const ODBC_ADD_DSN = 4
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
Private Declare Function GetActiveWindow Lib "user32.dll" () As Long
Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (phenv&) As Integer
Private Declare Function SQLAllocConnect Lib "ODBC32.DLL" (ByVal henv&, hDBC&) As Integer
Private Declare Function SQLCreateDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal lpszDriver As String)
Private Declare Function SQLDriverConnect Lib "ODBC32.DLL" (ByVal hDBC As Long, ByVal hwnd As Long, _
ByVal szCSIn As String, ByVal cbCSIn As Long, ByVal szCSOut As String, _
ByVal cbCSMax As Long, cbCSOut As Long, ByVal f As Long) As Long
Private Declare Function SQLAllocStmt Lib "ODBC32.DLL" (ByVal hDBC As Long, HStmt As Long) As Long
Public Function CreateDSN(ByVal pstrServer As String, ByVal pstrDescription As String, ByVal pstrDSN As String, ByVal pstrDataBase As String) As Long
On Error Resume Next
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=" & pstrServer & Chr(0)
strAttributes = strAttributes & "DESCRIPTION=" & pstrDescription & Chr(0)
strAttributes = strAttributes & "DSN=" & pstrDSN & Chr(0)
strAttributes = strAttributes & "DATABASE=" & pstrDataBase & Chr(0)
strAttributes = strAttributes & "Trusted_Connection=No" & Chr$(0)
strAttributes = strAttributes & "UID=sa" & Chr$(0)
strAttributes = strAttributes & "PWD=password " & Chr$(0)
'Calls API to create DSN
CreateDSN = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
End Function